aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/Wuse-after-free.c
blob: 9862de5c0a9c179040e2be692bf2f25b02294733 (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
/* PR middle-end/104069 - -Werror=use-after-free false positive on
   elfutils-0.186
   { dg-do compile }
   { dg-options "-Wall" } */

typedef __SIZE_TYPE__ size_t;

extern void* realloc (void *, size_t);

void* __libdw_unzstd (size_t todo)
{
  void *sb = 0;

  for ( ; ; )
    {
      // Ran only once.
      if (!sb)
	{
	  char *b = realloc (sb, todo);
	  if (!b)
	    break;

	  sb = b;
	}

      todo -= 1;
      if (todo == 0)
	break;
    }

  // Shrink buffer: leave only one byte for simplicity.
  char *b = realloc (sb, 1);
  if (b)
    sb = b;
  else
    {
      // Realloc failed mysteriously, leave 'sb' untouched.
    }

  return sb;        // { dg-bogus "-Wuse-after-free" }
}