Debian Codegraph @

gcc/testsuite/c-c++-common/analyzer/pr101962.c

raw
1 #include "../../gcc.dg/analyzer/analyzer-decls.h"
2
3
4 /* Verify that the analyzer makes the simplifying assumption that we don't
5 hit NULL when incrementing pointers to non-NULL memory regions. */
6
7 static int * __attribute__((noinline))
8 maybe_inc_int_ptr (int *ptr)
9 {
10 if (!ptr)
11 return NULL;
12 return ++ptr;
13 }
14
15 int
16 test_1 (void)
17 {
18 int stack;
19 int *a = &stack;
20 a = maybe_inc_int_ptr (a);
21 a = maybe_inc_int_ptr (a);
22 __analyzer_eval (a == NULL); /* { dg-warning "FALSE" } */
23 __analyzer_eval (a != NULL); /* { dg-warning "TRUE" } */
24 return *a; /* { dg-line test_1 } */
25
26 /* { dg-warning "stack-based buffer over-read" "warning" { target *-*-* } test_1 } */
27 }
28
29 static const char * __attribute__((noinline))
30 maybe_inc_char_ptr (const char *ptr)
31 {
32 if (!ptr)
33 return NULL;
34 return ++ptr;
35 }
36
37 void
38 test_s (void)
39 {
40 const char *msg = "hello world";
41 const char *a = msg;
42 __analyzer_eval (*a == 'h'); /* { dg-warning "TRUE" } */
43 a = maybe_inc_char_ptr (a);
44 __analyzer_eval (*a == 'e'); /* { dg-warning "TRUE" } */
45 a = maybe_inc_char_ptr (a);
46 __analyzer_eval (*a == 'l'); /* { dg-warning "TRUE" } */
47 a = maybe_inc_char_ptr (a);
48 __analyzer_eval (*a == 'l'); /* { dg-warning "TRUE" } */
49 a = maybe_inc_char_ptr (a);
50 __analyzer_eval (*a == 'o'); /* { dg-warning "TRUE" } */
51 }