public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/104082] New: Wdangling-pointer: 2 * false positive ?
@ 2022-01-18  8:37 dcb314 at hotmail dot com
  2022-01-18  8:44 ` [Bug c/104082] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: dcb314 at hotmail dot com @ 2022-01-18  8:37 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 104082
           Summary: Wdangling-pointer: 2 * false positive ?
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dcb314 at hotmail dot com
  Target Milestone: ---

For this C++ code:

$ more bug790.cc
int *metricp;
void s88() {
  int distance;
  metricp = &distance;
}
$ 

Does this with recent gcc trunk:

$ /home/dcb/gcc/results/bin/gcc -c -O2 -D_FORTIFY_SOURCE=2 -Wall bug790.cc
bug790.cc: In function ‘void s88()’:
bug790.cc:4:11: warning: storing the address of local variable ‘distance’ in
‘metricp’ [-Wdangling-pointer=]
    4 |   metricp = &distance;
      |   ~~~~~~~~^~~~~~~~~~~
bug790.cc:3:7: note: ‘distance’ declared here
    3 |   int distance;
      |       ^~~~~~~~
bug790.cc:1:6: note: ‘metricp’ declared here
    1 | int *metricp;
      |      ^~~~~~~
bug790.cc:4:11: warning: storing the address of local variable ‘distance’ in
‘metricp’ [-Wdangling-pointer=]
    4 |   metricp = &distance;
      |   ~~~~~~~~^~~~~~~~~~~
bug790.cc:3:7: note: ‘distance’ declared here
    3 |   int distance;
      |       ^~~~~~~~
bug790.cc:1:6: note: ‘metricp’ declared here
    1 | int *metricp;
      |      ^~~~~~~
$

I've no idea why the warning is generated, the code looks legal to me
and I've no idea why the warning is duplicated.

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

* [Bug c/104082] Wdangling-pointer: 2 * false positive ?
  2022-01-18  8:37 [Bug c/104082] New: Wdangling-pointer: 2 * false positive ? dcb314 at hotmail dot com
@ 2022-01-18  8:44 ` pinskia at gcc dot gnu.org
  2022-01-18  9:00 ` [Bug tree-optimization/104082] " dcb314 at hotmail dot com
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-18  8:44 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
>I've no idea why the warning is generated, the code looks legal to me

The warning is generated even though the code is valid, if someone deferences
metricp after the call to s88, the code becomes undefined. It is designed to
warn about that case.
Why the warning is duplicated I have no idea.

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

* [Bug tree-optimization/104082] Wdangling-pointer: 2 * false positive ?
  2022-01-18  8:37 [Bug c/104082] New: Wdangling-pointer: 2 * false positive ? dcb314 at hotmail dot com
  2022-01-18  8:44 ` [Bug c/104082] " pinskia at gcc dot gnu.org
@ 2022-01-18  9:00 ` dcb314 at hotmail dot com
  2022-01-18  9:35 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: dcb314 at hotmail dot com @ 2022-01-18  9:00 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from David Binderman <dcb314 at hotmail dot com> ---
(In reply to Andrew Pinski from comment #1)
> >I've no idea why the warning is generated, the code looks legal to me
> 
> The warning is generated even though the code is valid, if someone
> deferences metricp after the call to s88, the code becomes undefined. 

A warning for valid code is IMHO really bad.

It would be a better warning if it only happened when the pointer
is dereferenced.

As is, I've got to find every occurrence of this warning and manually
check for a problem. I think 90% of the time, it going to be legal code.

> Why the warning is duplicated I have no idea.

Righto.

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

* [Bug tree-optimization/104082] Wdangling-pointer: 2 * false positive ?
  2022-01-18  8:37 [Bug c/104082] New: Wdangling-pointer: 2 * false positive ? dcb314 at hotmail dot com
  2022-01-18  8:44 ` [Bug c/104082] " pinskia at gcc dot gnu.org
  2022-01-18  9:00 ` [Bug tree-optimization/104082] " dcb314 at hotmail dot com
@ 2022-01-18  9:35 ` rguenth at gcc dot gnu.org
  2022-01-18 11:02 ` dcb314 at hotmail dot com
  2022-01-18 16:49 ` msebor at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-01-18  9:35 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
                 CC|                            |msebor at gcc dot gnu.org
   Last reconfirmed|                            |2022-01-18
             Status|UNCONFIRMED                 |NEW

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
It's a style warning as much as a correctness one (like most warnings are).
A good programmers style workaround would be

int *metricp;
void s88() {
  int distance;
  metricp = &distance;
  metricp = NULL;
}

but I realize that's tedious and wasted cycles in the end (when all bogus
users are identified and fixed).

I do question -Wdangling-pointer being included in -Wall, it's at most
-Wextra category, if at all.

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

* [Bug tree-optimization/104082] Wdangling-pointer: 2 * false positive ?
  2022-01-18  8:37 [Bug c/104082] New: Wdangling-pointer: 2 * false positive ? dcb314 at hotmail dot com
                   ` (2 preceding siblings ...)
  2022-01-18  9:35 ` rguenth at gcc dot gnu.org
@ 2022-01-18 11:02 ` dcb314 at hotmail dot com
  2022-01-18 16:49 ` msebor at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: dcb314 at hotmail dot com @ 2022-01-18 11:02 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from David Binderman <dcb314 at hotmail dot com> ---
I just tried compiling a recent linux kernel with the new gcc trunk
and the warning appeared seven times.

Only one of them was a proper bug, which I have reported.
The rest were false positives. 

So this warning occasionally produces something useful, so it can't 
be simply temporarily ignored until it works properly.

I suggest it gets taken out of -Wall and -Wextra until it works a bit better.

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

* [Bug tree-optimization/104082] Wdangling-pointer: 2 * false positive ?
  2022-01-18  8:37 [Bug c/104082] New: Wdangling-pointer: 2 * false positive ? dcb314 at hotmail dot com
                   ` (3 preceding siblings ...)
  2022-01-18 11:02 ` dcb314 at hotmail dot com
@ 2022-01-18 16:49 ` msebor at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: msebor at gcc dot gnu.org @ 2022-01-18 16:49 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Blocks|                            |104077
             Status|NEW                         |RESOLVED

--- Comment #5 from Martin Sebor <msebor at gcc dot gnu.org> ---
This form of the warning implements a commonly requested feature missing from
-Wreturn-local-addr (also included in -Wall): allowing the address of a local
variable to escape the scope of the function.  Both warnings are issued for
strictly valid code.   See for example pr26671, pr49974, pr54301, pr82520,
pr84544, pr90905 for related requests (the initial implementation is too
simplistic to fully  handle all these but eventually it should).  A case very
similar to the one in comment #0 is documented in the manual:

  In the following function the store of the address of the local variable x in
the escaped pointer *p also triggers the warning.

  void g (int **p)
  {
    int x = 7;
    *p = &x;   // warning: storing the address of a local variable in *p
  }

The duplication is (of course) a bug.

Since the warning works as intended and documented (there's no false positive)
I'm going to resolve this report as invalid.  If you want to request a separate
level to control this aspect please open an enhancement request (I'm open to
implementing it).  If you want to remove this level from -Wall, please do the
same (though I don't support that request).


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104077
[Bug 104077] bogus/missing -Wdangling-pointer

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

end of thread, other threads:[~2022-01-18 16:49 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-18  8:37 [Bug c/104082] New: Wdangling-pointer: 2 * false positive ? dcb314 at hotmail dot com
2022-01-18  8:44 ` [Bug c/104082] " pinskia at gcc dot gnu.org
2022-01-18  9:00 ` [Bug tree-optimization/104082] " dcb314 at hotmail dot com
2022-01-18  9:35 ` rguenth at gcc dot gnu.org
2022-01-18 11:02 ` dcb314 at hotmail dot com
2022-01-18 16:49 ` msebor 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).