Bug 452219 - memset used with constant zero length parameter
Summary: memset used with constant zero length parameter
Keywords:
Status: CLOSED WONTFIX
Alias: None
Product: Fedora
Classification: Fedora
Component: gcc
Version: 9
Hardware: All
OS: Linux
low
low
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact: Fedora Extras Quality Assurance
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2008-06-20 10:29 UTC by Harald Hoyer
Modified: 2008-06-20 11:26 UTC (History)
0 users

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2008-06-20 11:07:23 UTC
Type: ---
Embargoed:


Attachments (Terms of Use)

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 :)


Note You need to log in before you can comment on or make changes to this bug.