This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Optimising away memset() calls?
- From: Sandy Harris <sandyinchina at gmail dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Tue, 7 Oct 2014 22:15:01 -0400
- Subject: Optimising away memset() calls?
- Authentication-results: sourceware.org; auth=none
There is discussion in Linux mailing lists threads about code along these lines:
some_function()
{
char temp[N] ;
...
do something that puts sensitive data in temp[]
....
memset( temp, 0, N ) ;
}
The claim is that gcc may optimise away the memset() call since that
memory will not be referenced again.
The threads are:
https://lkml.org/lkml/2014/8/25/497
http://marc.info/?l=linux-crypto-vger&m=141247858212197&w=2
The second one has links to other discussion on the web as well.
There are various solutions to this. Linux now has memzero_explicit(),
Open SSH has bzero_explicit(), C11 has memset_s(). Here's Apple's man
page:
https://developer.apple.com/library/mac/documentation/Darwin/Reference/Manpages/man3/memset_s.3.html
As I see it, though, and wrote in one thread:
" A real fix would make memset() do the right thing reliably; if the
" programmer puts in memset( x, 0, nbytes) then the memory should
" be cleared, no ifs or buts. I do not know or care if that means
" changes in the compiler or in the library code or even both, but
" the fix should make the standard library code work right, not
" require adding a new function and expecting everyone to use it.
It seemed worth tossing this out for comment here.