09

Beware, Compilers are Allowed to Remove
Memset Calls!

#SecureCoding #C/C++
When a C program handles sensitive data, like secret keys, it is reasonable to clear the buffer before freeing it, to avoid leaking the secret if the memory is later re-used. One might use memset to clear a buffer, but a modern compiler knows the semantics of memset and can optimize it away. Even a custom clear function in the same C file can be optimized away by a clever compiler. Instead, use library functions like memset_s or explicit_bzero. Crypto libraries like OpenSSL often also have their own versions too, like OPENSSL_cleanse.
Icon with a waving hand

Get in touch

sigma star gmbh
Eduard-Bodem-Gasse 6, 1st floor
6020 Innsbruck | Austria

sigma star gmbh logo