$ cat tt.c #include <stdlib.h> #include <stdio.h> #include <string.h> void * e_malloc(size) size_t size; { void *pt = NULL; if ((size > 0) && ((pt = malloc(size)) == NULL)) { fprintf(stderr, "Not enough memory\n"); return NULL; } memset(pt, 0, size); return pt; } $ gcc -c -o tt.o -O2 -Wall -Wp,-D_FORTIFY_SOURCE=2 tt.c In function ‘memset’, inlined from ‘e_malloc’ at tt.c:16: /usr/include/bits/string3.h:82: warning: call to ‘__warn_memset_zero_len’ declared with attribute warning: memset used with constant zero length parameter; this could be due to transposed parameters $ gcc -c -o tt.o -Wall -Wp,-D_FORTIFY_SOURCE=2 tt.c $
ok, -O2 seems to create the size==0 case.
I'm afraid there is nothing we can do about this. If-conversion merges the two size != 0 checks and so at the __warn_memset_zero_len () check size will be constant 0, as the if (size > 0) else branch jumps directly to it. To avoid the warning, just stick the memset under if (size > 0), i.e. if (size > 0) { if ((pt = malloc(size)) == NULL) { fprintf(...); return NULL; } memset(pt, 0, size); } return pt; or better yet just use calloc, that's more efficient anyway in many cases.
this code snippet was from cdrtools.. I shall not rewrite it :)