Skip to content

Clang misses detecting the dangling pointer warning found with GCC #83719

@mushenoy

Description

@mushenoy

Clang seem to miss detecting the dangling pointer warning (Wdangling-pointer) found with GCC

Sample Program:

#include <stdio.h>
void test (int **p)
{
  int x = 7;
  *p = &x;
}
int main ()
{
  int *xp = NULL;
  test (&xp);

  printf ("dereference of xp = %d\n", *xp);
  return 0;
}

Compilation using clang:
No warnings reported

# clang -Wall -c dangling.c
#

Compilation using gcc:

# gcc -Wall -c  dangling.c
dangling.c: In function test:
dangling.c:5:6: warning: storing the address of local variable x in *p [-Wdangling-pointer=]
    5 |   *p = &x;
      |   ~~~^~~~
dangling.c:4:7: note: x declared here
    4 |   int x = 7;
      |       ^
dangling.c:2:18: note: p declared here
    2 | void test (int **p)
      |

Compiler versions:

clang version 16.0.6 
gcc (GCC) 13.2.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerfalse-negativeWarning doesn't fire when it should

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions