Bug 452219

Summary: memset used with constant zero length parameter
Product: [Fedora] Fedora Reporter: Harald Hoyer <harald>
Component: gccAssignee: Jakub Jelinek <jakub>
Status: CLOSED WONTFIX QA Contact: Fedora Extras Quality Assurance <extras-qa>
Severity: low Docs Contact:
Priority: low    
Version: 9   
Target Milestone: ---   
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2008-06-20 11:07:23 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Harald Hoyer 2008-06-20 10:29:07 UTC
$ 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
$

Comment 1 Harald Hoyer 2008-06-20 11:07:23 UTC
ok, -O2 seems to create the size==0 case.

Comment 2 Jakub Jelinek 2008-06-20 11:09:55 UTC
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.

Comment 3 Harald Hoyer 2008-06-20 11:26:43 UTC
this code snippet was from cdrtools.. I shall not rewrite it :)