1 /* Test for the long double variants of *w*printf_chk functions.
2    Copyright (C) 2019-2021 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #define _FORTIFY_SOURCE 2
20 
21 #include <stdarg.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <wchar.h>
25 
26 #include <support/capture_subprocess.h>
27 #include <support/check.h>
28 
29 static void
do_test_call_varg(FILE * stream,const wchar_t * format,...)30 do_test_call_varg (FILE *stream, const wchar_t *format, ...)
31 {
32   wchar_t string[128];
33   va_list args;
34 
35   wprintf (L"%20Ls", L"__vfwprintf_chk: ");
36   va_start (args, format);
37   __vfwprintf_chk (stream, 1, format, args);
38   va_end (args);
39   wprintf (L"\n");
40 
41   wprintf (L"%20Ls", L"__vswprintf_chk: ");
42   va_start (args, format);
43   __vswprintf_chk (string, 79, 1, 127, format, args);
44   va_end (args);
45   wprintf (L"%Ls", string);
46   wprintf (L"\n");
47 
48   wprintf (L"%20Ls", L"__vwprintf_chk: ");
49   va_start (args, format);
50   __vwprintf_chk (1, format, args);
51   va_end (args);
52   wprintf (L"\n");
53 }
54 
55 static void
do_test_call_rarg(FILE * stream,const wchar_t * format,long double ld,double d)56 do_test_call_rarg (FILE *stream, const wchar_t *format, long double ld,
57 		   double d)
58 {
59   wchar_t string[128];
60 
61   wprintf (L"%20Ls", L"__fwprintf_chk: ");
62   __fwprintf_chk (stream, 1, format, ld, d);
63   wprintf (L"\n");
64 
65   wprintf (L"%20Ls", L"__swprintf_chk: ");
66   __swprintf_chk (string, 79, 1, 127, format, ld, d);
67   wprintf (L"%Ls", string);
68   wprintf (L"\n");
69 
70   wprintf (L"%20Ls", L"__wprintf_chk: ");
71   __wprintf_chk (1, format, ld, d);
72   wprintf (L"\n");
73 }
74 
75 static void
do_test_call(void)76 do_test_call (void)
77 {
78   long double ld = -1;
79   double d = -1;
80 
81   /* Print in decimal notation.  */
82   do_test_call_rarg (stdout, L"%.10Lf, %.10f", ld, d);
83   do_test_call_varg (stdout, L"%.10Lf, %.10f", ld, d);
84 
85   /* Print in hexadecimal notation.  */
86   do_test_call_rarg (stdout, L"%.10La, %.10a", ld, d);
87   do_test_call_varg (stdout, L"%.10La, %.10a", ld, d);
88 
89   /* Test positional parameters.  */
90   do_test_call_varg (stdout, L"%3$Lf, %2$Lf, %1$f",
91 		     (double) 1, (long double) 2, (long double) 3);
92 }
93 
94 static int
do_test(void)95 do_test (void)
96 {
97   struct support_capture_subprocess result;
98   result = support_capture_subprocess ((void *) &do_test_call, NULL);
99 
100   /* Compare against the expected output.  */
101   const char *expected =
102     "    __fwprintf_chk: -1.0000000000, -1.0000000000\n"
103     "    __swprintf_chk: -1.0000000000, -1.0000000000\n"
104     "     __wprintf_chk: -1.0000000000, -1.0000000000\n"
105     "   __vfwprintf_chk: -1.0000000000, -1.0000000000\n"
106     "   __vswprintf_chk: -1.0000000000, -1.0000000000\n"
107     "    __vwprintf_chk: -1.0000000000, -1.0000000000\n"
108     "    __fwprintf_chk: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
109     "    __swprintf_chk: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
110     "     __wprintf_chk: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
111     "   __vfwprintf_chk: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
112     "   __vswprintf_chk: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
113     "    __vwprintf_chk: -0x1.0000000000p+0, -0x1.0000000000p+0\n"
114     "   __vfwprintf_chk: 3.000000, 2.000000, 1.000000\n"
115     "   __vswprintf_chk: 3.000000, 2.000000, 1.000000\n"
116     "    __vwprintf_chk: 3.000000, 2.000000, 1.000000\n";
117   TEST_COMPARE_STRING (expected, result.out.buffer);
118 
119   return 0;
120 }
121 
122 #include <support/test-driver.c>
123