aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/Warray-bounds-56.c
blob: 04c26a659adc0b225e11923432ff83cae55cf493 (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
/* PR middle-end/91582 - missing heap overflow detection for strcpy

   The -Warray-bounds instances here probably should be replaced by
   -Wstringop-overflow when it detects these overflows (see also
   the xfails in Wstringop-overflow-25.c).

   { dg-do compile }
   { dg-options "-O2 -Wall -Wno-stringop-overflow -ftrack-macro-expansion=0" } */

#include "range.h"

#define INT_MAX     __INT_MAX__
#define INT_MIN     (-INT_MAX - 1)

#define ATTR(...)   __attribute__ ((__VA_ARGS__))
#define NOIPA       ATTR (noipa)

extern void* malloc (size_t);
extern char* strcpy (char*, const char*);

void sink (void*);

#define S36 "0123456789abcdefghijklmnopqrstuvwxyz"
#define S(N) (S36 + sizeof S36 - N - 1)

struct Flex
{
  char n, ax[];
};

extern struct Flex fx;
struct Flex f1 = { 1, { 1 } };
struct Flex f2 = { 2, { 1, 2 } };
struct Flex f3 = { 3, { 1, 2, 3 } };

#define T(src, f) do {				\
    char *s = src;				\
    char *d = f.ax;				\
    strcpy (d, s);				\
    sink (&f);					\
  } while (0)

NOIPA void test_strcpy_flexarray (void)
{
  T (S (0), fx);                // { dg-bogus "\\\[-Warray-bounds" "pr92815" }
  T (S (9), fx);                // { dg-bogus "\\\[-Warray-bounds" "pr92815" }

  T (S (0), f1);
  T (S (1), f1);                // { dg-warning "\\\[-Warray-bounds" }

  T (S (0), f2);
  T (S (1), f2);
  T (S (2), f2);                // { dg-warning "\\\[-Warray-bounds" }

  T (S (0), f3);
  T (S (2), f3);
  T (S (3), f3);                // { dg-warning "\\\[-Warray-bounds" }
  T (S (9), f3);                // { dg-warning "\\\[-Warray-bounds" }
}

#undef T
#define T(T, src, n) do {			\
    char *s = src;				\
    typedef struct { T n, ax[]; } Flex;		\
    Flex *p = (Flex*)malloc (sizeof *p + n);	\
    char *d = (char*)p->ax;			\
    strcpy (d, s);				\
    sink (p);					\
  } while (0)

NOIPA void test_strcpy_malloc_flexarray (void)
{
  size_t r_0_1 = UR (0, 1);

  T (char, S (0), r_0_1);
  T (char, S (1), r_0_1);       // { dg-warning "\\\[-Warray-bounds" }

  size_t r_1_2 = UR (1, 2);

  T (char, S (0), r_1_2);
  T (char, S (1), r_1_2);
  T (char, S (2), r_1_2);       // { dg-warning "\\\[-Warray-bounds" }

  size_t r_2_3 = UR (2, 3);

  T (char, S (0), r_2_3);
  T (char, S (2), r_2_3);
  T (char, S (3), r_2_3);       // { dg-warning "\\\[-Warray-bounds" }
  T (char, S (9), r_2_3);       // { dg-warning "\\\[-Warray-bounds" }
}