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
|
/* { dg-do compile } */
/* { dg-options "-O2 -Wformat -Wformat-truncation=1 -ftrack-macro-expansion=0" } */
typedef struct
{
char a0[0];
char a1[1];
char a2[2];
char a3[3];
char a4[4];
char ax[];
} Arrays;
char buffer[1024];
#define buffer(size) (buffer + sizeof buffer - size)
int value_range (int min, int max)
{
extern int value (void);
int val = value ();
return val < min || max < val ? min : val;
}
#define R(min, max) value_range (min, max)
/* Verify that calls to snprintf whose return value is unused are
diagnosed if certain or possible truncation is detected. */
#define T(size, ...) \
__builtin_snprintf (buffer (size), size, __VA_ARGS__)
void test_int_retval_unused (void)
{
T (2, "%i", 123); /* { dg-warning "output truncated" } */
T (2, "%i", R (1, 99)); /* { dg-warning "output may be truncated" } */
T (2, "%i", R (10, 99)); /* { dg-warning "output truncated" } */
T (3, "%i%i", R (1, 99), R (1, 99)); /* { dg-warning "output may be truncated" } */
}
void test_string_retval_unused (const Arrays *ar)
{
T (1, "%-s", ar->a0);
T (1, "%-s", ar->a1);
T (1, "%-s", ar->a2); /* { dg-warning "output may be truncated" } */
}
/* Verify that calls to snprintf whose return value is used are
diagnosed only if certain truncation is detected but not when
truncation is only possible but not certain. */
volatile int retval;
#undef T
#define T(size, ...) \
retval = __builtin_snprintf (buffer (size), size, __VA_ARGS__)
void test_int_retval_used (void)
{
T (2, "%i", 123); /* { dg-warning "output truncated" } */
T (2, "%i", R (1, 99));
T (2, "%i", R (10, 99)); /* { dg-warning "output truncated" } */
T (3, "%i%i", R (1, 99), R (1, 99));
}
void test_string_retval_used (const Arrays *ar)
{
T (1, "%-s", ar->a0);
T (1, "%-s", ar->a1);
T (1, "%-s", ar->a2);
T (1, "%-s", ar->a4);
T (1, "%-s", "123"); /* { dg-warning "output truncated" } */
}
|