public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/109708] New: [c, doc] wdangling-pointer example broken
@ 2023-05-03  7:54 vries at gcc dot gnu.org
  2023-05-03  8:04 ` [Bug c/109708] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: vries at gcc dot gnu.org @ 2023-05-03  7:54 UTC (permalink / raw)
  To: gcc-bugs

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;
}
...

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/109708] [c, doc] wdangling-pointer example broken
  2023-05-03  7:54 [Bug c/109708] New: [c, doc] wdangling-pointer example broken vries at gcc dot gnu.org
@ 2023-05-03  8:04 ` pinskia at gcc dot gnu.org
  2024-01-20 22:39 ` sandra at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-03  8:04 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109708

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Warning-Options.html#index-Wdangling-pointer

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/109708] [c, doc] wdangling-pointer example broken
  2023-05-03  7:54 [Bug c/109708] New: [c, doc] wdangling-pointer example broken vries at gcc dot gnu.org
  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
  3 siblings, 0 replies; 5+ messages in thread
From: sandra at gcc dot gnu.org @ 2024-01-20 22:39 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109708

--- Comment #2 from sandra at gcc dot gnu.org ---
I was wondering if some subsequent patch might have caused the first example to
regress rather than this being a documentation bug, but it did not give a
diagnostic at the time the -Wdangling-pointer support was committed, either. 
I'll fiddle with the example a bit to make it better illustrate the difference
between level 1 and level 2, as well as making it work.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/109708] [c, doc] wdangling-pointer example broken
  2023-05-03  7:54 [Bug c/109708] New: [c, doc] wdangling-pointer example broken vries at gcc dot gnu.org
  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
  3 siblings, 0 replies; 5+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-01-21  3:57 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109708

--- Comment #3 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Sandra Loosemore <sandra@gcc.gnu.org>:

https://gcc.gnu.org/g:29f931e39f2be86b454a8264b1cd42e4ca3cdcd7

commit r14-8314-g29f931e39f2be86b454a8264b1cd42e4ca3cdcd7
Author: Sandra Loosemore <sandra@codesourcery.com>
Date:   Sun Jan 21 02:36:19 2024 +0000

    Clean up examples for -Wdangling-pointer [PR109708]

    gcc/ChangeLog
            PR c/109708
            * doc/invoke.texi (Warning Options): Fix broken example and
            clean up/reorganize the others.  Also describe what the short-form
            options mean.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c/109708] [c, doc] wdangling-pointer example broken
  2023-05-03  7:54 [Bug c/109708] New: [c, doc] wdangling-pointer example broken vries at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2024-01-21  3:57 ` cvs-commit at gcc dot gnu.org
@ 2024-01-21  4:00 ` sandra at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: sandra at gcc dot gnu.org @ 2024-01-21  4:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109708

sandra at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |FIXED

--- Comment #4 from sandra at gcc dot gnu.org ---
Fixed.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-01-21  4:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-03  7:54 [Bug c/109708] New: [c, doc] wdangling-pointer example broken vries at gcc dot gnu.org
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

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