Skip to content

Commit df2b78d

Browse files
committed
analyzer: fix NULL deref false positives [PR94851]
PR analyzer/94851 reports various false "NULL dereference" diagnostics. The first case (comment #1) affects GCC 10.2 but no longer affects trunk; I believe it was fixed by the state rewrite of r11-2694-g808f4dfeb3a95f50f15e71148e5c1067f90a126d. The patch adds a regression test for this case. The other cases (comment gcc-mirror#3 and comment gcc-mirror#4) still affect trunk. In both cases, the && in a conditional is optimized to bitwise & _1 = p_4 != 0B; _2 = p_4 != q_6(D); _3 = _1 & _2; and the analyzer fails to fold this for the case where one (or both) of the conditionals is false, and thus erroneously considers the path where "p" is non-NULL despite being passed a NULL value. Fix this by implementing folding for this case. gcc/analyzer/ChangeLog: PR analyzer/94851 * region-model-manager.cc (region_model_manager::maybe_fold_binop): Fold bitwise "& 0" to 0. gcc/testsuite/ChangeLog: PR analyzer/94851 * gcc.dg/analyzer/pr94851-1.c: New test. * gcc.dg/analyzer/pr94851-3.c: New test. * gcc.dg/analyzer/pr94851-4.c: New test.
1 parent c199723 commit df2b78d

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

gcc/analyzer/region-model-manager.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,12 @@ region_model_manager::maybe_fold_binop (tree type, enum tree_code op,
451451
if (cst1 && integer_onep (cst1))
452452
return arg0;
453453
break;
454+
case BIT_AND_EXPR:
455+
if (cst1)
456+
if (zerop (cst1) && INTEGRAL_TYPE_P (type))
457+
/* "(ARG0 & 0)" -> "0". */
458+
return get_or_create_constant_svalue (build_int_cst (type, 0));
459+
break;
454460
case TRUTH_ANDIF_EXPR:
455461
case TRUTH_AND_EXPR:
456462
if (cst1)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/* { dg-additional-options "-O2" } */
2+
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
6+
typedef struct AMARK {
7+
struct AMARK *m_next;
8+
char m_name;
9+
} AMARK;
10+
11+
struct buf {
12+
AMARK *b_amark;
13+
};
14+
15+
struct buf *curbp;
16+
17+
int pamark(void) {
18+
int c;
19+
AMARK *p = curbp->b_amark;
20+
AMARK *last = curbp->b_amark;
21+
22+
c = getchar();
23+
24+
while (p != (AMARK *)NULL && p->m_name != (char)c) {
25+
last = p;
26+
p = p->m_next;
27+
}
28+
29+
if (p != (AMARK *)NULL) {
30+
printf("over writing mark %c\n", c);
31+
} else {
32+
if ((p = (AMARK *)malloc(sizeof(AMARK))) == (AMARK *)NULL)
33+
return 0;
34+
35+
p->m_next = (AMARK *)NULL;
36+
37+
if (curbp->b_amark == (AMARK *)NULL)
38+
curbp->b_amark = p;
39+
else
40+
last->m_next = p;
41+
}
42+
43+
p->m_name = (char)c;
44+
45+
return 1;
46+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* { dg-additional-options "-O1" } */
2+
3+
struct List {
4+
struct List *next;
5+
};
6+
7+
void foo(struct List *p, struct List *q)
8+
{
9+
while (p && p != q){
10+
p = p->next;
11+
}
12+
}
13+
14+
int main()
15+
{
16+
struct List x = {0};
17+
foo(0, &x);
18+
return 0;
19+
}
20+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* { dg-additional-options "-O2" } */
2+
3+
#include <stdlib.h>
4+
5+
struct List {
6+
struct List *next;
7+
};
8+
9+
void foo(struct List *p, struct List *q)
10+
{
11+
while (p && p != q){
12+
struct List *next = p->next;
13+
free(p);
14+
p = next;
15+
}
16+
}
17+
18+
int main()
19+
{
20+
struct List x = {0};
21+
foo(NULL, &x);
22+
return 0;
23+
}
24+

0 commit comments

Comments
 (0)