From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 05EF2382DDDF; Mon, 17 Jul 2023 00:54:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 05EF2382DDDF DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1689555289; bh=v2Y9MtQ+JwNGtwDiNg5PYbtywvP1/kz6AtHOH3m79KE=; h=From:To:Subject:Date:From; b=T/TsjgCjUE+gIpByn1UKXhytO1iyAR1StZAC8Bi/83/PzlRKqtoDwDLPvbGxYQF56 sdTu/DzMPh3w1cfnux1unMhvXBbzJcU3teNHR70G7n5ow95pY6uJtXvS//qrGP5yh7 xG+04InA9ER7UpzHsAn2E9i4GJluU4RHam1PLzyE= From: "biggs at biggs dot xyz" 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 13.1.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: biggs at biggs dot xyz X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone attachments.created Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D110694 Bug ID: 110694 Summary: False Positive -Werror=3Dfree-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=3D55559&action=3Dedit Source File Observed on Trunk and 13.1.1 Consider: ```c #include typedef struct S S; struct S { int* i; }; static S* s_constructor(void) { S* s =3D malloc(sizeof(*s)); if (s) s->i =3D 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 =3D s_constructor(); s_destructor(s); s =3D s_constructor(); if (s) s_destructor2(s); s =3D (void*) 0; return EXIT_SUCCESS; } ``` Compiling with gcc -Wall -Werror produces the error: : In function 's_destructor2': :23:5: error: 'free' called on unallocated object 's' [-Werror=3Dfree-nonheap-object] 23 | free(s); | ^~~~~~~ :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 fl= ag. 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_destructo= r(S* s).=