aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/analyzer/pr101962.c
blob: b878aad9cf15f1c625fb60a27552e37414668157 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "analyzer-decls.h"

#define NULL ((void *)0)

/* Verify that the analyzer makes the simplifying assumption that we don't
   hit NULL when incrementing pointers to non-NULL memory regions.  */

static int * __attribute__((noinline))
maybe_inc_int_ptr (int *ptr)
{
  if (!ptr)
    return NULL;
  return ++ptr;
}

int
test_1 (void)
{
  int stack; /* { dg-message "region created on stack here" } */
  int *a = &stack;
  a = maybe_inc_int_ptr (a);
  a = maybe_inc_int_ptr (a);
  __analyzer_eval (a == NULL); /* { dg-warning "FALSE" } */
  __analyzer_eval (a != NULL); /* { dg-warning "TRUE" } */
  return *a; /* { dg-line test_1 } */

  /* { dg-warning "stack-based buffer over-read" "warning" { target *-*-* } test_1 } */
}

static const char * __attribute__((noinline))
maybe_inc_char_ptr (const char *ptr)
{
  if (!ptr)
    return NULL;
  return ++ptr;
}

char
test_s (void)
{
  const char *msg = "hello world";
  const char *a = msg;
  __analyzer_eval (*a == 'h'); /* { dg-warning "TRUE" } */
  a = maybe_inc_char_ptr (a);
  __analyzer_eval (*a == 'e'); /* { dg-warning "TRUE" } */
  a = maybe_inc_char_ptr (a);
  __analyzer_eval (*a == 'l'); /* { dg-warning "TRUE" } */
  a = maybe_inc_char_ptr (a);
  __analyzer_eval (*a == 'l'); /* { dg-warning "TRUE" } */
  a = maybe_inc_char_ptr (a);
  __analyzer_eval (*a == 'o'); /* { dg-warning "TRUE" } */
}