1 /* Test to verify that snprintf can determine the correct range
2 of lengths of string arguments based on the results of prior
3 calls to strlen.
4 { dg-do compile }
5 { dg-options "-O2 -Wall -fdump-tree-optimized" } */
6
7 typedef __SIZE_TYPE__ size_t;
8
9 void abort (void);
10 size_t strlen (const char *);
11 int snprintf (char * restrict, size_t, const char *restrict, ...);
12
one_str_exact(const char * str)13 void one_str_exact (const char *str)
14 {
15 if (1 == strlen (str))
16 if (1 != snprintf (0, 0, "%s", str))
17 abort ();
18 }
19
two_str_exact(const char * s1,const char * s2)20 void two_str_exact (const char *s1, const char *s2)
21 {
22 if (1 == strlen (s1) && 2 == strlen (s2))
23 if (3 != snprintf (0, 0, "%s%s", s1, s2))
24 abort ();
25 }
26
one_str_maxlen(const char * str)27 void one_str_maxlen (const char *str)
28 {
29 if (2 >= strlen (str))
30 if (2 < snprintf (0, 0, "%s", str))
31 abort ();
32 }
33
two_str_maxlen(const char * s1,const char * s2)34 void two_str_maxlen (const char *s1, const char *s2)
35 {
36 if (2 >= strlen (s1) && 3 >= strlen (s2))
37 if (5 < snprintf (0, 0, "%s%s", s1, s2))
38 abort ();
39 }
40
41 /* { dg-final { scan-tree-dump-not "abort" "optimized" } } */
42