aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/tree-ssa/builtin-snprintf-4.c
blob: 97a385e581da8c04208a317a992ca87b7f72efba (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/* PR tree-optimization/87096 - "Optimised" snprintf is not POSIX conformant
   Verify that calls to snprintf with size in excess of INT_MAX are not
   treated as successful.
   It would be valid for GCC to fold some of these calls to a negative
   value provided it also arranged to set errno to EOVERFLOW.  If that
   is ever implemented this test will need to be adjusted.
   { dg-do compile }
   { dg-options "-O2 -Wall -fdump-tree-optimized -ftrack-macro-expansion=0" } */

#include "../range.h"

typedef __builtin_va_list va_list;

extern int snprintf (char*, size_t, const char*, ...);
extern int vsnprintf (char*, size_t, const char*, va_list);

#define CAT(x, y) x ## y
#define CONCAT(x, y) CAT (x, y)
#define FAILNAME(name) CONCAT (call_ ## name ##_on_line_, __LINE__)

#define FAIL(name) do {				\
    extern void FAILNAME (name) (void);		\
    FAILNAME (name)();				\
  } while (0)

/* Macro to emit a call to function named
     call_in_true_branch_not_eliminated_on_line_NNN()
   for each expression that's expected to fold to false but that
   GCC does not fold.  The dg-final scan-tree-dump-time directive
   at the bottom of the test verifies that no such call appears
   in output.  */
#define ELIM(expr)							\
  if ((expr)) FAIL (in_true_branch_not_eliminated); else (void)0

/* Macro to emit a call to a function named
     call_made_in_{true,false}_branch_on_line_NNN()
   for each call that's expected to be retained.  The dg-final
   scan-tree-dump-time directive at the bottom of the test verifies
   that the expected number of both kinds of calls appears in output
   (a pair for each line with the invocation of the KEEP() macro.  */
#define KEEP(expr)				\
  if (expr)					\
    FAIL (made_in_true_branch);			\
  else						\
    FAIL (made_in_false_branch)

extern void sink (int, ...);
#define sink(...) sink (0, __VA_ARGS__)

#define WARN(N, expr)				\
  do {						\
    char a[N];					\
    expr;					\
    sink (a);					\
  } while (0)


static const size_t imax = __INT_MAX__;
static const size_t imaxp1 = imax + 1;

#if __PTRDIFF_MAX__ == __INT_MAX__
/* Make the test pass on ILP32 the same way it does on LP64.  */
static const size_t dmax = __PTRDIFF_MAX__ + (size_t)1;
#else
static const size_t dmax = __PTRDIFF_MAX__;
#endif
static const size_t dmaxp1 = dmax + 1;

static const size_t szmax = __SIZE_MAX__;
static const size_t szmaxm1 = __SIZE_MAX__ - 1;


void test_size_cst (char **d)
{
  ELIM (0 > snprintf (*d++, imax, "%s", ""));

  KEEP (0 > snprintf (*d++, imaxp1, "%s", ""));   /* { dg-warning "\\\[-Wformat-truncation=]" } */

  KEEP (0 > snprintf (*d++, dmax, "%s", ""));     /* { dg-warning "\\\[-Wformat-truncation=]" } */
  KEEP (0 > snprintf (*d++, dmaxp1, "%s", ""));   /* { dg-warning "\\\[-Wformat-truncation=]" } */
  KEEP (0 > snprintf (*d++, szmaxm1, "%s", ""));  /* { dg-warning "\\\[-Wformat-truncation=]" } */
  KEEP (0 > snprintf (*d++, szmax, "%s", ""));    /* { dg-warning "\\\[-Wformat-truncation=]" } */
}


void test_size_cst_va (char **d, va_list va)
{
  ELIM (0 > vsnprintf (*d++, imax, " ", va));

  KEEP (0 > vsnprintf (*d++, imaxp1, " ", va));   /* { dg-warning "\\\[-Wformat-truncation=]" } */

  KEEP (0 > vsnprintf (*d++, dmax, " ", va));     /* { dg-warning "\\\[-Wformat-truncation=]" } */
  KEEP (0 > vsnprintf (*d++, dmaxp1, " ", va));   /* { dg-warning "\\\[-Wformat-truncation=]" } */
  KEEP (0 > vsnprintf (*d++, szmaxm1, " ", va));  /* { dg-warning "\\\[-Wformat-truncation=]" } */
  KEEP (0 > vsnprintf (*d++, szmax, " ", va));    /* { dg-warning "\\\[-Wformat-truncation=]" } */
}


void test_size_range (char **d)
{
  size_t r = UR (imax - 1, imax);
  ELIM (0 > snprintf (*d++, r, "%s", ""));

  r = UR (imax, imax + 1);
  KEEP (0 > snprintf (*d++, r, "%s", ""));

  r = UR (imaxp1, imaxp1 + 1);
  KEEP (0 > snprintf (*d++, r, "%s", ""));        /* { dg-warning "specified bound range \\\[\[0-9\]+, \[0-9\]+] exceeds .INT_MAX." } */

  r = UR (dmax, dmaxp1);
  KEEP (0 > snprintf (*d++, r, "%s", ""));        /* { dg-warning "\\\[-Wformat-truncation=]" } */

  r = UR (dmaxp1, dmaxp1 + 1);
  KEEP (0 > snprintf (*d++, r, "%s", ""));        /* { dg-warning "\\\[-Wformat-truncation=]" } */

  r = UR (szmaxm1, szmax);
  KEEP (0 > snprintf (*d++, r, "%s", ""));        /* { dg-warning "\\\[-Wformat-truncation=]" } */
}


void test_size_range_va (char **d, va_list va)
{
  size_t r = UR (imax - 1, imax);
  ELIM (0 > vsnprintf (*d++, r, " ", va));

  r = UR (imax, imax + 1);
  KEEP (0 > vsnprintf (*d++, r, " ", va));

  r = UR (imaxp1, imaxp1 + 1);
  KEEP (0 > vsnprintf (*d++, r, " ", va));        /* { dg-warning "specified bound range \\\[\[0-9\]+, \[0-9\]+] exceeds .INT_MAX." } */

  r = UR (dmax, dmaxp1);
  KEEP (0 > vsnprintf (*d++, r, " ", va));        /* { dg-warning "\\\[-Wformat-truncation=]" } */

  r = UR (dmaxp1, dmaxp1 + 1);
  KEEP (0 > vsnprintf (*d++, r, " ", va));        /* { dg-warning "\\\[-Wformat-truncation=]" } */

  r = UR (szmaxm1, szmax);
  KEEP (0 > vsnprintf (*d++, r, " ", va));        /* { dg-warning "\\\[-Wformat-truncation=]" } */
}


void test_size_varying (char **d, size_t n)
{
  KEEP (0 > snprintf (*d++, n, "%s", ""));

  n += 1;
  KEEP (0 > snprintf (*d++, n, "%s", ""));
}


void test_size_varying_va (char **d, size_t n, va_list va)
{
  KEEP (0 > vsnprintf (*d++, n, " ", va));

  n += 1;
  KEEP (0 > vsnprintf (*d++, n, " ", va));
}

/* { dg-final { scan-tree-dump-times " = snprintf" 12 "optimized"} }
   { dg-final { scan-tree-dump-times " = vsnprintf" 12 "optimized"} } */