public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "biggs at biggs dot xyz" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/110694] New: False Positive -Werror=free-nonheap-object
Date: Mon, 17 Jul 2023 00:54:48 +0000	[thread overview]
Message-ID: <bug-110694-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110694

            Bug ID: 110694
           Summary: False Positive -Werror=free-nonheap-object
           Product: gcc
           Version: 13.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: biggs at biggs dot xyz
  Target Milestone: ---

Created attachment 55559
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55559&action=edit
Source File

Observed on Trunk and 13.1.1
Consider:

```c
#include <stdlib.h>

typedef struct S S;

struct S {
    int* i;
};

static S* s_constructor(void) {
    S* s = malloc(sizeof(*s));
    if (s) s->i = calloc(1, sizeof(*(s->i)));
    return s;
}

static void s_destructor(S* s) {
    if (!s) return;
    free(s->i);
    free(s);
}

static void s_destructor2(S s[static 1]) {
    free(s->i);
    free(s);
}

int main(void) {
    S* s = s_constructor();
    s_destructor(s);

    s = s_constructor();
    if (s) s_destructor2(s);
    s = (void*) 0;

    return EXIT_SUCCESS;
}
```

Compiling with gcc -Wall -Werror produces the error:
<source>: In function 's_destructor2':
<source>:23:5: error: 'free' called on unallocated object 's'
[-Werror=free-nonheap-object]
   23 |     free(s);
      |     ^~~~~~~
<source>:21:29: note: declared here
   21 | static void s_destructor2(S s[static 1]) {
      |                           ~~^~~~~~~~~~~
cc1: all warnings being treated as errors

However, there is no error when compiled with -O1 or higher optimization flag.

I don't think s_destructor2(S s[static 1]) should emit an error in the first
place though. The function definition should be compatible with s_destructor(S*
s).

             reply	other threads:[~2023-07-17  0:54 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-17  0:54 biggs at biggs dot xyz [this message]
2023-07-17  0:59 ` [Bug middle-end/110694] [11/12/13/14 Regression] " pinskia at gcc dot gnu.org
2023-11-01 19:07 ` roman.zilka at gmail dot com
2024-03-07 23:28 ` law at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-110694-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).