public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/54202] New: Overeager warning about freeing non-heap objects
@ 2012-08-08 13:36 thiago at kde dot org
  2012-08-08 14:00 ` [Bug c/54202] " rguenth at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: thiago at kde dot org @ 2012-08-08 13:36 UTC (permalink / raw)
  To: gcc-bugs

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.


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2023-08-25 11:46 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-08-08 13:36 [Bug c/54202] New: Overeager warning about freeing non-heap objects thiago at kde dot org
2012-08-08 14:00 ` [Bug c/54202] " rguenth at gcc dot gnu.org
2012-08-08 14:22 ` thiago at kde dot org
2012-08-08 14:36 ` rguenth at gcc dot gnu.org
2012-08-08 14:53 ` thiago at kde dot org
2012-08-08 19:01 ` pinskia at gcc dot gnu.org
2020-11-03 19:50 ` [Bug middle-end/54202] " msebor at gcc dot gnu.org
2021-05-17 12:09 ` dangelog at gmail dot com
2021-06-16 13:19 ` mserdarsanli at gmail dot com
2021-06-16 16:35 ` msebor at gcc dot gnu.org
2023-08-25 11:46 ` charlechaud at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).