aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/analyzer/realloc-1.c
blob: 9951e118efeb5d50a78adb413474b3bda8a17659 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* { dg-additional-options "-Wno-free-nonheap-object" } */

typedef __SIZE_TYPE__ size_t;

#define NULL ((void *)0)

extern void *malloc (size_t __size)
  __attribute__ ((__nothrow__ , __leaf__))
  __attribute__ ((__malloc__))
  __attribute__ ((__alloc_size__ (1)));
extern void *realloc (void *__ptr, size_t __size)
  __attribute__ ((__nothrow__ , __leaf__))
  __attribute__ ((__warn_unused_result__))
  __attribute__ ((__alloc_size__ (2)));
extern void free (void *__ptr)
  __attribute__ ((__nothrow__ , __leaf__));

void *test_1 (void *ptr)
{
  return realloc (ptr, 1024);
}

void *test_2 (void *ptr)
{
  void *p = malloc (1024); /* { dg-message "allocated here" } */
  p = realloc (p, 4096); /* { dg-message "when 'realloc' fails" } */
  free (p);
} /* { dg-warning "leak of 'p'" } */ // ideally this would be on the realloc stmt

void *test_3 (void *ptr)
{
  void *p = malloc (1024);
  void *q = realloc (p, 4096);
  if (q)
    free (q);
  else
    free (p);
}

void *test_4 (void)
{
  return realloc (NULL, 1024);
}

int *test_5 (int *p)
{
  *p = 42;
  int *q = realloc (p, sizeof(int) * 4); /* { dg-message "when 'realloc' fails" } */
  *q = 43; /* { dg-warning "dereference of NULL 'q'" } */
  return q;
}

void test_6 (size_t sz)
{
  void *p = realloc (NULL, sz);
} /* { dg-warning "leak of 'p'" } */

/* The analyzer should complain about realloc of non-heap.  */

void *test_7 (size_t sz)
{
  char buf[100]; /* { dg-message "region created on stack here" } */
  void *p = realloc (&buf, sz); /* { dg-warning "'realloc' of '&buf' which points to memory on the stack" } */
  return p;  
}

/* Mismatched allocator.  */

struct foo
{
  int m_int;
};

extern void foo_release (struct foo *);
extern struct foo *foo_acquire (void)
  __attribute__ ((malloc (foo_release)));

void test_8 (void)
{
  struct foo *p = foo_acquire ();
  void *q = realloc (p, 1024); /* { dg-warning "'p' should have been deallocated with 'foo_release' but was deallocated with 'realloc'" } */
}

/* We should complain about realloc on a freed pointer.  */

void test_9 (void *p)
{
  free (p);
  void *q = realloc (p, 1024); /* { dg-warning "double-'free' of 'p'" } */
}

void test_10 (char *s, int n)
{
  __builtin_realloc(s, n); /* { dg-warning "ignoring return value of '__builtin_realloc' declared with attribute 'warn_unused_result'" } */
} /* { dg-warning "leak" } */