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).