[go: nahoru, domu]

Bug 110694 - [11/12/13/14/15 Regression] False Positive -Werror=free-nonheap-object
Summary: [11/12/13/14/15 Regression] False Positive -Werror=free-nonheap-object
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 13.1.1
: P2 normal
Target Milestone: 11.5
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: Wfree-nonheap-object
  Show dependency treegraph
 
Reported: 2023-07-17 00:54 UTC by biggs
Modified: 2024-05-09 21:31 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work: 10.5.0
Known to fail: 11.1.0
Last reconfirmed: 2023-07-17 00:00:00


Attachments
Source File (247 bytes, text/plain)
2023-07-17 00:54 UTC, biggs
Details

Note You need to log in before you can comment on or make changes to this bug.
Description biggs 2023-07-17 00:54:48 UTC
Created attachment 55559 [details]
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).
Comment 1 Andrew Pinski 2023-07-17 00:59:45 UTC
Confirmed.
Comment 2 Roman Žilka 2023-11-01 19:07:46 UTC
I can see it on 13.2.1 20230826. The warning triggers even without any -W* args. The manpage implies that shouldn't be the case.