aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/c-c++-common/analyzer/out-of-bounds-diagram-8.c
blob: 4d1f5227049cd9df06a7f5b6fdc267ad0537d34c (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
/* { dg-additional-options "-fdiagnostics-text-art-charset=unicode" } */
/* { dg-skip-if "requires hosted libstdc++ for stdlib size_t" { ! hostedlib } } */

#include <stdlib.h>
#include <stdint.h>

/* Gap of 4, then an overflow of 4.  */

void test2 (size_t size)
{
  int32_t *buf = (int32_t *) __builtin_malloc (size * sizeof(int32_t)); /* { dg-message "\\(1\\) capacity: 'size \\* 4' bytes" "" { target c } } */
  /* { dg-message "\\(1\\) capacity: '\\(size \\* 4\\)' bytes" "" { target c++ } .-1 } */
  if (!buf) return;

  buf[size + 1] = 42; /* { dg-warning "heap-based buffer overflow" } */
  __builtin_free (buf);
}

/* { dg-begin-multiline-output "" }

                                                 ┌───────────────────────┐
                                                 │write of '(int32_t) 42'│
                                                 └───────────────────────┘


                                                             v
  ┌───────────────────────────────┐              ┌───────────────────────┐
  │buffer allocated on heap at (1)│              │   after valid range   │
  └───────────────────────────────┘              └───────────────────────┘
  ├───────────────┬───────────────┤├─────┬──────┤├───────────┬───────────┤
                  │                      │                   │
    ╭─────────────┴────────────╮     ╭───┴───╮     ╭─────────┴─────────╮
    │capacity: 'size * 4' bytes│     │4 bytes│     │overflow of 4 bytes│
    ╰──────────────────────────╯     ╰───────╯     ╰───────────────────╯

   { dg-end-multiline-output "" } */