public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "vries at gcc dot gnu.org" <gcc-bugzilla@gcc.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 [thread overview] Message-ID: <bug-109708-4@http.gcc.gnu.org/bugzilla/> (raw) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109708 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 = 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 undeclared. After fixing that (and adding the implicit "#include <string.h>"), we have an example that compiles: ... #include <string.h> int f (int c1, int c2, int c3) { char *p = strchr ((char[]){ c1, c2 }, c3); return p ? *p : 'x'; } ... but no warning, not at O0, O1, O2 or O3: ... $ gcc test.c -Wdangling-pointer=1 -c $ ... After reading the description of the warning, I managed to come up with: ... char f (char c1, char c2) { char *p; { p = (char[]) { c1, c2 }; } return *p; } ... which does manage to trigger the warning for O0-O3: ... $ gcc test.c -Wdangling-pointer=1 -c test.c: In function ‘f’: test.c:10:10: warning: using dangling pointer ‘p’ to an unnamed temporary [-Wdangling-pointer=] 10 | return *p; | ^~ test.c:7:18: note: unnamed temporary defined here 7 | p = (char[]) { c1, c2 }; | ^ $ ... It might be worth mentioning that it's a C example, when using g++ we have: ... $ g++ test.c -Wdangling-pointer=1 -c test.c: In function ‘char f(char, char)’: test.c:7:18: error: taking address of temporary array 7 | p = (char[]) { c1, c2 }; | ^~~~~~~~~~ ... BTW, note that the warning can be fixed by doing: ... char f (char c1, char c2) { char *p; + char c; { p = (char[]) { c1, c2 }; + c = *p; } - return *p; + return c; } ...
next reply other threads:[~2023-05-03 7:54 UTC|newest] Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top 2023-05-03 7:54 vries at gcc dot gnu.org [this message] 2023-05-03 8:04 ` [Bug c/109708] " pinskia at gcc dot gnu.org 2024-01-20 22:39 ` sandra at gcc dot gnu.org 2024-01-21 3:57 ` cvs-commit at gcc dot gnu.org 2024-01-21 4:00 ` sandra at gcc dot gnu.org
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-109708-4@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).