public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/101150] New: null pointer dereference false positive disappears when compiling an additional function
@ 2021-06-21 11:48 adl at gnu dot org
  2021-06-22  0:11 ` [Bug tree-optimization/101150] " pinskia at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: adl at gnu dot org @ 2021-06-21 11:48 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101150
           Summary: null pointer dereference false positive disappears
                    when compiling an additional function
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: adl at gnu dot org
  Target Milestone: ---

Created attachment 51042
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51042&action=edit
source code, plus the three preprocessed versions

I'm observing the following behavior with gcc-snapshot on Debian
unstable as well as when using "x86-64 gcc (trunk)" and "x86-64 gcc
(11.1)" on Godbolt.  This false positive is not reported by gcc 10.

% g++ --version | sed 1q
g++ (Debian 20210527-1) 12.0.0 20210527 (experimental) [master revision
262e75d22c3:7bb6b9b2f47:9d3a953ec4d2695e9a6bfa5f22655e2aea47a973]

% cat foo.cc
#include <vector>

#ifdef FOO
void foo(const std::vector<int>& other)
{
  std::vector<int> v;
  std::size_t sz = other.size();
  v.resize(sz);
  int i = 0;
  for (int o: other)
    v[i++] = o;
}
#endif

#ifdef BAR
void bar(const std::vector<int>& other)
{
  std::vector<int> v;
  unsigned sz = other.size();
  v.resize(sz);
  int i = 0;
  for (int o: other)
    v[i++] = o;
}
#endif

% g++ -O3 -Wnull-dereference -c foo.cc -DBAR
% g++ -O3 -Wnull-dereference -c foo.cc -DFOO -DBAR
% g++ -O3 -Wnull-dereference -c foo.cc -DFOO
In function 'void foo(const std::vector<int>&)':
cc1plus: warning: potential null pointer dereference [-Wnull-dereference]

The two functions differ only by the type of sz, and the warning
occurs only if foo() is compiled but bar() is not.


I *believe* the warning comes from the fact that if sz is 0, the data
pointer of v will still be nullptr after resize(), and that would
render v[i++]=o invalid.  However if sz is 0, the loop will not do
any iteration, so that's a false positive.

However I can't explain
- why changing size_t into unsigned makes the warning go away,
- why compiling the two functions makes the warning go away.

I was expecting the diagnostics about foo() to be independent of the presence
of bar(), and I was expecting to get the same diagnostics for both functions
(preferably none, but I understand it's only a "potential" issue)

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

* [Bug tree-optimization/101150] null pointer dereference false positive disappears when compiling an additional function
  2021-06-21 11:48 [Bug c++/101150] New: null pointer dereference false positive disappears when compiling an additional function adl at gnu dot org
@ 2021-06-22  0:11 ` pinskia at gcc dot gnu.org
  2021-07-28  7:07 ` [Bug tree-optimization/101150] [11/12 Regression] " rguenth at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-06-22  0:11 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
   Target Milestone|---                         |11.2
             Status|UNCONFIRMED                 |NEW
           Keywords|                            |missed-optimization
          Component|c++                         |tree-optimization
   Last reconfirmed|                            |2021-06-22

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
There seems to be some missing optimizations (jump threading) here.

First one:

  long int _12;

  _12 = _9 - _15;
  if (_12 != 0)
    goto <bb 4>; [33.00%]
  else
    goto <bb 3>; [67.00%] // this should just jump to bb 15 because _12 == 0
and there for _9 == _15

  <bb 3> [local count: 79134772]:
  if (_9 != _15) /// This should always be false coming from bb 2 because _12
== 0 there for _9 == _15
    goto <bb 11>; [89.00%]
  else
    goto <bb 15>; [11.00%]

...

  <bb 11> [local count: 104641928]:
  # _143 = PHI <_18(8), _9(3)>
  # _118 = PHI <_19(8), _15(3)>
  # v$_M_start_84 = PHI <_104(8), 0B(3)>
  # prephitmp_123 = PHI <_12(8), _12(3)>
  _133 = (unsigned long) _143;
  _110 = (unsigned long) _118;
  _125 = _133 - _110;

....
  <bb 15> [local count: 117575200]:
  return;

Second one:
...

  <bb 4> [local count: 19488414]:
  _64 = (long unsigned int) _12;
  if (_64 > 9223372036854775804)
    goto <bb 5>; [0.04%]
  else
    goto <bb 6>; [99.96%]

....

  <bb 6> [local count: 9740309]:
  if (_12 != 0) // This should always be true as _12 should always != 0 on
coming into this bb because we can only come via the if statement in bb 4 on
the true edge of the conditional.
    goto <bb 7>; [100.00%]
  else
    goto <bb 16>; [0.00%] /// NOTE BB 16 is where the null pointer write it
located

--------- CUT -------
The reason why if you have both foo and bar defined, there is a heuristics of
the inlining of vector::_M_default_append which causes the difference, nothing
shocking really.

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

* [Bug tree-optimization/101150] [11/12 Regression] null pointer dereference false positive disappears when compiling an additional function
  2021-06-21 11:48 [Bug c++/101150] New: null pointer dereference false positive disappears when compiling an additional function adl at gnu dot org
  2021-06-22  0:11 ` [Bug tree-optimization/101150] " pinskia at gcc dot gnu.org
@ 2021-07-28  7:07 ` rguenth at gcc dot gnu.org
  2022-01-20 12:55 ` rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu.org @ 2021-07-28  7:07 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|11.2                        |11.3

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 11.2 is being released, retargeting bugs to GCC 11.3

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

* [Bug tree-optimization/101150] [11/12 Regression] null pointer dereference false positive disappears when compiling an additional function
  2021-06-21 11:48 [Bug c++/101150] New: null pointer dereference false positive disappears when compiling an additional function adl at gnu dot org
  2021-06-22  0:11 ` [Bug tree-optimization/101150] " pinskia at gcc dot gnu.org
  2021-07-28  7:07 ` [Bug tree-optimization/101150] [11/12 Regression] " rguenth at gcc dot gnu.org
@ 2022-01-20 12:55 ` rguenth at gcc dot gnu.org
  2022-04-21  7:49 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-01-20 12:55 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|2021-06-22 00:00:00         |2022-1-20
      Known to work|                            |10.3.1
           Priority|P3                          |P2

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
Re-confirmed on trunk.

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

* [Bug tree-optimization/101150] [11/12 Regression] null pointer dereference false positive disappears when compiling an additional function
  2021-06-21 11:48 [Bug c++/101150] New: null pointer dereference false positive disappears when compiling an additional function adl at gnu dot org
                   ` (2 preceding siblings ...)
  2022-01-20 12:55 ` rguenth at gcc dot gnu.org
@ 2022-04-21  7:49 ` rguenth at gcc dot gnu.org
  2023-03-23 18:05 ` [Bug tree-optimization/101150] [11/12/13 " pinskia at gcc dot gnu.org
  2023-05-29 10:05 ` [Bug tree-optimization/101150] [11/12 " jakub at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-04-21  7:49 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|11.3                        |11.4

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 11.3 is being released, retargeting bugs to GCC 11.4.

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

* [Bug tree-optimization/101150] [11/12/13 Regression] null pointer dereference false positive disappears when compiling an additional function
  2021-06-21 11:48 [Bug c++/101150] New: null pointer dereference false positive disappears when compiling an additional function adl at gnu dot org
                   ` (3 preceding siblings ...)
  2022-04-21  7:49 ` rguenth at gcc dot gnu.org
@ 2023-03-23 18:05 ` pinskia at gcc dot gnu.org
  2023-05-29 10:05 ` [Bug tree-optimization/101150] [11/12 " jakub at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-03-23 18:05 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |needs-bisection
      Known to work|                            |13.0

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
On the trunk, the warning is gone so is the missed optimization mentioned in
comment #1.

We do have another missed optimization though:
  _12 = _9 - _15;
  _7 = _12 /[ex] 4;
  _18 = (long unsigned int) _7;
  _120 = _18 != 0;
  _54 = _12 != 0;
  _127 = _54 & _120;
  if (_127 != 0)
    goto <bb 3>; [16.50%]
  else
    goto <bb 9>; [83.50%]
...
  <bb 9> [local count: 98623186]:
  if (_9 != _15)
    goto <bb 10>; [89.00%]
  else
    goto <bb 23>; [11.00%]

Only one of those comparisons is actually needed. and then another missing jump
threading because _9 be equal _15 on the goto <bb 9> branch.

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

* [Bug tree-optimization/101150] [11/12 Regression] null pointer dereference false positive disappears when compiling an additional function
  2021-06-21 11:48 [Bug c++/101150] New: null pointer dereference false positive disappears when compiling an additional function adl at gnu dot org
                   ` (4 preceding siblings ...)
  2023-03-23 18:05 ` [Bug tree-optimization/101150] [11/12/13 " pinskia at gcc dot gnu.org
@ 2023-05-29 10:05 ` jakub at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: jakub at gcc dot gnu.org @ 2023-05-29 10:05 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|11.4                        |11.5

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
GCC 11.4 is being released, retargeting bugs to GCC 11.5.

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

end of thread, other threads:[~2023-05-29 10:05 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-21 11:48 [Bug c++/101150] New: null pointer dereference false positive disappears when compiling an additional function adl at gnu dot org
2021-06-22  0:11 ` [Bug tree-optimization/101150] " pinskia at gcc dot gnu.org
2021-07-28  7:07 ` [Bug tree-optimization/101150] [11/12 Regression] " rguenth at gcc dot gnu.org
2022-01-20 12:55 ` rguenth at gcc dot gnu.org
2022-04-21  7:49 ` rguenth at gcc dot gnu.org
2023-03-23 18:05 ` [Bug tree-optimization/101150] [11/12/13 " pinskia at gcc dot gnu.org
2023-05-29 10:05 ` [Bug tree-optimization/101150] [11/12 " jakub 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).