Bug 95589 - missing warning initializing a reference with a dereferenced null
Summary: missing warning initializing a reference with a dereferenced null
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: Wnull-dereference
  Show dependency treegraph
 
Reported: 2020-06-08 19:25 UTC by Martin Sebor
Modified: 2024-01-01 02:51 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-08-13 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2020-06-08 19:25:21 UTC
Initializing a reference by dereferenced null pointer is not diagnosed but should be because such a reference is invalid:

$ cat t.C && gcc -O2 -S -Wall -Wextra -Wnull-dereference -fdump-tree-optimized=/dev/stdout t.C
void f (const int&);
void g ()
{
  int *p = 0;
  f (*p);
}

;; Function g (_Z1gv, funcdef_no=0, decl_uid=2330, cgraph_uid=1, symbol_order=0)

g ()
{
  <bb 2> [local count: 1073741824]:
  f (0B); [tail call]
  return;

}

Clang doesn't diagnose it either unless --analyze is used:

$ clang -S -Wall -Wextra --analyze t.C
t.C:5:3: warning: Forming reference to null pointer
  f (*p);
  ^~~~~~
1 warning generated.
Comment 1 Martin Sebor 2020-06-08 19:29:50 UTC
I was about to classify this as an enhancement but after reading the description of -Wnull-dereference in the manual ("compiler detects paths that trigger erroneous or undefined behavior due to dereferencing a null pointer") I'm inclined to view it as a bug.  The code is clearly in error, GCC just doesn't detect it (even though the "doesn't detect it" part could be used to argue the warning works strictly as documented).
Comment 2 Eric Gallager 2021-08-13 08:13:26 UTC
Confirmed.