When I allocate the key on the heap, the memset is carried (heavily optimized and inlined). When I allocate key on the stack, it disappears. Using gcc -03:
#include <string.h>
void doSecure(void)
{
/*char key[32];*/
char *key = (char*) malloc(sizeof(char)*32);
memset(key,sizeof(char),32);
}
int main(void)
{
doSecure();
return 0;
}
-- key on stack
main:
.LFB13:
.cfi_startproc
xorl %eax, %eax
ret
.cfi_endproc
-- key on heap
main:
.LFB13:
.cfi_startproc
subq $8, %rsp
.cfi_def_cfa_offset 16
movl $32, %edi
call malloc
movabsq $72340172838076673, %rdx
movq %rdx, (%rax)
movq %rdx, 8(%rax)
movq %rdx, 16(%rax)
movq %rdx, 24(%rax)
xorl %eax, %eax
addq $8, %rsp
.cfi_def_cfa_offset 8
ret
.cfi_endproc