[Bug c/54202] New: Overeager warning about freeing non-heap objects

thiago at kde dot org gcc-bugzilla@gcc.gnu.org
Wed Aug 8 13:36:00 GMT 2012


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54202

             Bug #: 54202
           Summary: Overeager warning about freeing non-heap objects
    Classification: Unclassified
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: thiago@kde.org


GCC 4.7 has a warning about freeing non-heap objects that is way too eager.

Compiling the following code in C or C++:
======
#include <stdlib.h>

typedef struct Data
{ 
    int refcount;
} Data; 
extern const Data shared_null;

Data *allocate()
{
    return (Data *)(&shared_null);
} 

void dispose(Data *d)
{
    if (d->refcount == 0)
        free(d);
}

void f()
{
    Data *d = allocate();
    dispose(d);
}
====

Produces the following warning:

test.c: In function 'f'
test.c:17:13: warning: attempt to free a non-heap object 'shared_null'
[-Wfree-nonheap-object]

The warning is overeager because it says "attempt to free" without indicating
that it's only a possibility. GCC cannot prove that the call to free() will
happen with that particular pointer, as the value of shared_null.refcount is
not known.

The warning should either:
 a) be modified to indicate it's only a possibility and the compiler can't
prove it;
 b) be issued only when the compiler is sure that the free will happen on
non-heap objects.

Or both, by having two warnings: one for when it's sure and one for when it
isn't.



More information about the Gcc-bugs mailing list