Bug 452219 - memset used with constant zero length parameter
memset used with constant zero length parameter
Product: Fedora
Classification: Fedora
Component: gcc (Show other bugs)
All Linux
low Severity low
: ---
: ---
Assigned To: Jakub Jelinek
Fedora Extras Quality Assurance
Depends On:
  Show dependency treegraph
Reported: 2008-06-20 06:29 EDT by Harald Hoyer
Modified: 2008-06-20 07:26 EDT (History)
0 users

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Last Closed: 2008-06-20 07:07:23 EDT
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Attachments (Terms of Use)

  None (edit)
Description Harald Hoyer 2008-06-20 06:29:07 EDT
$ cat tt.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

void *
        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 07:07:23 EDT
ok, -O2 seems to create the size==0 case.
Comment 2 Jakub Jelinek 2008-06-20 07:09:55 EDT
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) {
    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 07:26:43 EDT
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.