From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 5CF48393BC2F; Mon, 3 May 2021 18:52:00 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5CF48393BC2F From: "lavr at ncbi dot nlm.nih.gov" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/100403] New: Bogus "function may return address of local variable" warning Date: Mon, 03 May 2021 18:52:00 +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: 10.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: lavr at ncbi dot nlm.nih.gov 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 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 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 03 May 2021 18:52:00 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D100403 Bug ID: 100403 Summary: Bogus "function may return address of local variable" warning Product: gcc Version: 10.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: lavr at ncbi dot nlm.nih.gov Target Milestone: --- $ gcc --version gcc (GCC) 10.2.0 $ cat test.c #include #include #define RECLEN 128 struct R { int head; int code; char text[1]; }; const char* fun(int n, const struct R* p) { union { struct R r; char rec[RECLEN]; } x; const char* err =3D 0; memset(&x, 0, sizeof(x)); if (p) memcpy(&x.r, p, sizeof(*p)); else err =3D "Invalid argument"; if (!err) { static const char magic[] =3D "MAGIC"; const char* msg; if (memcmp(x.rec, magic, sizeof(magic)-1) =3D=3D 0) msg =3D x.rec; else if (x.r.text[0]) msg =3D x.r.text; else msg =3D "Error w/code"; if (msg) printf("%s\n", msg); if (x.rec <=3D msg && msg < x.rec + sizeof(x)) err =3D "Error detected"; else err =3D msg; } else printf("%s\n", err); return err; } $ gcc -Wall -O2 -c test.c test.c: In function =E2=80=98fun=E2=80=99: test.c:45:12: warning: function may return address of local variable [-Wreturn-local-addr] 45 | return err; | ^~~ test.c:19:7: note: declared here 19 | } x; | ^ test.c:19:7: note: declared here Noted that does not matter whether "sizeof(x)" or "sizeof(x.rec)" is used at the end of the "if()" statement on line 39.=