test-xvasprintf: disable some -Wformat-security diagnostics
[gnulib.git] / tests / test-xvasprintf.c
1 /* Test of xvasprintf() and xasprintf() functions.
2    Copyright (C) 2007-2013 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 /* Tell GCC not to warn about the specific edge cases tested here.  */
20 #if (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) || 4 < __GNUC__
21 # pragma GCC diagnostic ignored "-Wformat-zero-length"
22 # pragma GCC diagnostic ignored "-Wformat-nonliteral"
23 # pragma GCC diagnostic ignored "-Wformat-security"
24 #endif
25
26 #include <config.h>
27
28 #include "xvasprintf.h"
29
30 #include <stdarg.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "progname.h"
35 #include "macros.h"
36
37 static char *
38 my_xasprintf (const char *format, ...)
39 {
40   va_list args;
41   char *ret;
42
43   va_start (args, format);
44   ret = xvasprintf (format, args);
45   va_end (args);
46   return ret;
47 }
48
49 static void
50 test_xvasprintf (void)
51 {
52   int repeat;
53   char *result;
54
55   for (repeat = 0; repeat <= 8; repeat++)
56     {
57       result = my_xasprintf ("%d", 12345);
58       ASSERT (result != NULL);
59       ASSERT (strcmp (result, "12345") == 0);
60       free (result);
61     }
62
63   {
64     /* Silence gcc warning about zero-length format string.  */
65     const char *empty = "";
66     result = my_xasprintf (empty);
67     ASSERT (result != NULL);
68     ASSERT (strcmp (result, "") == 0);
69     free (result);
70   }
71
72   result = my_xasprintf ("%s", "foo");
73   ASSERT (result != NULL);
74   ASSERT (strcmp (result, "foo") == 0);
75   free (result);
76
77   result = my_xasprintf ("%s%s", "foo", "bar");
78   ASSERT (result != NULL);
79   ASSERT (strcmp (result, "foobar") == 0);
80   free (result);
81
82   result = my_xasprintf ("%s%sbaz", "foo", "bar");
83   ASSERT (result != NULL);
84   ASSERT (strcmp (result, "foobarbaz") == 0);
85   free (result);
86 }
87
88 static void
89 test_xasprintf (void)
90 {
91   int repeat;
92   char *result;
93
94   for (repeat = 0; repeat <= 8; repeat++)
95     {
96       result = xasprintf ("%d", 12345);
97       ASSERT (result != NULL);
98       ASSERT (strcmp (result, "12345") == 0);
99       free (result);
100     }
101
102   {
103     /* Silence gcc warning about zero-length format string,
104        and about "format not a string literal and no format"
105        (whatever that means) .  */
106     const char *empty = "";
107     result = xasprintf (empty, empty);
108     ASSERT (result != NULL);
109     ASSERT (strcmp (result, "") == 0);
110     free (result);
111   }
112
113   result = xasprintf ("%s", "foo");
114   ASSERT (result != NULL);
115   ASSERT (strcmp (result, "foo") == 0);
116   free (result);
117
118   result = xasprintf ("%s%s", "foo", "bar");
119   ASSERT (result != NULL);
120   ASSERT (strcmp (result, "foobar") == 0);
121   free (result);
122
123   result = my_xasprintf ("%s%sbaz", "foo", "bar");
124   ASSERT (result != NULL);
125   ASSERT (strcmp (result, "foobarbaz") == 0);
126   free (result);
127 }
128
129 int
130 main (int argc _GL_UNUSED, char *argv[])
131 {
132   set_program_name (argv[0]);
133
134   test_xvasprintf ();
135   test_xasprintf ();
136
137   return 0;
138 }