aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/analyzer/malloc-2.c
blob: bb93c53d3e0f155be989ed25cfa61d781eb0687a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* Tests for precision-of-wording within malloc warnings.  */

typedef __SIZE_TYPE__ size_t;
extern void *malloc(size_t);
extern void free(void *);
extern char *strcpy(char *__restrict __dest, const char *__restrict __src)
    __attribute__((__nothrow__, __leaf__)) __attribute__((__nonnull__(1, 2)));

void test_1 (void)
{
  void *p = malloc (1024); /* { dg-message "\\(1\\) this call could return NULL" } */
  strcpy ((char *)p, "hello world"); /* { dg-warning "use of possibly-NULL 'p' where non-null expected" "warning" } */
  /* { dg-message "\\(2\\) argument 1 \\('p'\\) from \\(1\\) could be NULL where non-null expected" "event" { target *-*-* } .-1 } */
  free (p);
}

int *test_2 (void)
{
  int *i = malloc (sizeof (int)); /* { dg-message "\\(1\\) this call could return NULL" } */
  *i = 42; /* { dg-warning "dereference of possibly-NULL 'i'" "warning" } */
  /* { dg-message "\\(2\\) 'i' could be NULL: unchecked value from \\(1\\)" "event" { target *-*-* } .-1 } */
  return i; 
}