by smegel · 12y ago · 0 comments
#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_endprocYou have zero guarantees it will work in another compiler or even between releases of the same one.
Never take a compiler behaviour for the standard. That's the beauty of standards.
memset(key,sizeof(char),32);
Note that you are not clearing 32 bytes with NUL (byte value 0). You are clearing 32 bytes with byte value 1 == (int)(sizeof(char).That's why memset that works 8 bytes at a time fills memory with 72340172838076673 == 0x0101010101010101