From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id C37203858D28; Wed, 3 May 2023 07:54:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C37203858D28 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1683100449; bh=DcvXraekROT8A6OvoSGGgWaHJR9sxTgrlwJK9/F9NdI=; h=From:To:Subject:Date:From; b=EWlnBfcNzBDm9IKwMVBzteAonW8sC5AlIZD9Ao0o40jwfPSL4M3wLCxbXgpkrz1Sr IU+ZdRVtqLMesji0UluYW/R0K7Ina+PTnsWo3ISCcgboAxWI+jJN1X58I6X9y1KMge bX64F3OG7Oa7xw7JG2ahj6uueas7FU2k4MfMelgw= From: "vries at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/109708] New: [c, doc] wdangling-pointer example broken Date: Wed, 03 May 2023 07:54:09 +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: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: vries at gcc dot gnu.org 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 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D109708 Bug ID: 109708 Summary: [c, doc] wdangling-pointer example broken Product: gcc Version: unknown Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: vries at gcc dot gnu.org Target Milestone: --- I ran into a Wdangling-pointer warning and decided to read the docs and try= out an example. The first one listed is: ... int f (int c1, int c2, x) { char *p =3D strchr ((char[]){ c1, c2 }, c3); // warning: dangling pointer to a compound literal return p ? *p : 'x'; } ... It's not a complete example, x is missing a declared type and c3 is undecla= red.=20 After fixing that (and adding the implicit "#include "), we have = an example that compiles: ... #include int f (int c1, int c2, int c3) { char *p =3D strchr ((char[]){ c1, c2 }, c3); return p ? *p : 'x'; } ... but no warning, not at O0, O1, O2 or O3: ... $ gcc test.c -Wdangling-pointer=3D1 -c $ ... After reading the description of the warning, I managed to come up with: ... char f (char c1, char c2) { char *p; { p =3D (char[]) { c1, c2 }; }=20 return *p; } ... which does manage to trigger the warning for O0-O3: ... $ gcc test.c -Wdangling-pointer=3D1 -c test.c: In function =E2=80=98f=E2=80=99: test.c:10:10: warning: using dangling pointer =E2=80=98p=E2=80=99 to an unn= amed temporary [-Wdangling-pointer=3D] 10 | return *p; | ^~ test.c:7:18: note: unnamed temporary defined here 7 | p =3D (char[]) { c1, c2 }; | ^ $ ... It might be worth mentioning that it's a C example, when using g++ we have: ... $ g++ test.c -Wdangling-pointer=3D1 -c test.c: In function =E2=80=98char f(char, char)=E2=80=99: test.c:7:18: error: taking address of temporary array 7 | p =3D (char[]) { c1, c2 }; | ^~~~~~~~~~ ... BTW, note that the warning can be fixed by doing: ... char f (char c1, char c2) { char *p; + char c; { p =3D (char[]) { c1, c2 }; + c =3D *p; } - return *p; + return c; } ...=