aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/analyzer/realloc-5.c
blob: f65f2c6ca2519acab49dc60bb4e597ada9c0e01e (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
/* { dg-additional-options "-fno-analyzer-suppress-followups" } */

#include "analyzer-decls.h"

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__));
extern void *memset (void *__ptr, int __value, size_t __size);

/* realloc where the region shrinks on success_with_move.  */

void test_1 ()
{
  char *p = malloc (16);
  if (!p)
    return;
  memset (p, 1, 16);

  char *q = realloc (p, 8);
  if (!q)
    {
      free (p);
      return;
    }
  else if (p != q)
    {
      __analyzer_dump_capacity (q); /* { dg-warning "capacity: '\\(\[^\n\r\]*\\)8'" } */
      __analyzer_eval (q[8] == 1); /* { dg-line eval } */
    
      /* { dg-warning "UNKNOWN" "warning" { target *-*-* } eval } */
      /* { dg-warning "heap-based buffer over-read" "warning" { target *-*-* } eval } */
    }

  free (q);
}