ReactOS  0.4.14-dev-50-g13bb5e2
sprintf.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS api tests
3  * LICENSE: GPLv2+ - See COPYING in the top level directory
4  * PURPOSE: Test for sprintf
5  * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
6  */
7 
8 #include <apitest.h>
9 
10 #define WIN32_NO_STATUS
11 #include <stdio.h>
12 #include <tchar.h>
13 #include <pseh/pseh2.h>
14 #include <ndk/mmfuncs.h>
15 #include <ndk/rtlfuncs.h>
16 
17 #ifdef _MSC_VER
18 #pragma warning(disable:4778) // unterminated format string '%'
19 #elif defined(__GNUC__)
20 #pragma GCC diagnostic ignored "-Wformat"
21 #pragma GCC diagnostic ignored "-Wformat-zero-length"
22 #pragma GCC diagnostic ignored "-Wnonnull"
23 #if __GNUC__ >= 7
24 #pragma GCC diagnostic ignored "-Wformat-overflow"
25 #endif
26 #endif
27 
28 static
29 PVOID
31  SIZE_T SizeRequested)
32 {
34  SIZE_T Size = PAGE_ROUND_UP(SizeRequested + PAGE_SIZE);
35  PVOID VirtualMemory = NULL;
36  PCHAR StartOfBuffer;
37 
39 
40  if (!NT_SUCCESS(Status))
41  return NULL;
42 
43  Size -= PAGE_SIZE;
44  if (Size)
45  {
47  if (!NT_SUCCESS(Status))
48  {
49  Size = 0;
51  ok(Status == STATUS_SUCCESS, "Status = %lx\n", Status);
52  return NULL;
53  }
54  }
55 
56  StartOfBuffer = VirtualMemory;
57  StartOfBuffer += Size - SizeRequested;
58 
59  return StartOfBuffer;
60 }
61 
62 static
63 VOID
65  PVOID Pointer)
66 {
68  PVOID VirtualMemory = (PVOID)PAGE_ROUND_DOWN((SIZE_T)Pointer);
69  SIZE_T Size = 0;
70 
72  ok(Status == STATUS_SUCCESS, "Status = %lx\n", Status);
73 }
74 
75 /* NOTE: This test is not only used for all the CRT apitests, but also for
76  * user32's wsprintf. Make sure to test them all */
78 {
79  int Length;
80  CHAR Buffer[128];
81  PCHAR String;
82 
83  /* basic parameter tests */
84  StartSeh()
85  Length = sprintf(NULL, NULL);
87 
88  StartSeh()
89  Length = sprintf(NULL, "");
90  ok_int(Length, 0);
92 
93  StartSeh()
94  Length = sprintf(NULL, "Hello");
95  ok_int(Length, 5);
97 
98  /* some basic formats */
99  Length = sprintf(Buffer, "abcde");
100  ok_str(Buffer, "abcde");
101  ok_int(Length, 5);
102 
103  Length = sprintf(Buffer, "%%");
104  ok_str(Buffer, "%");
105  ok_int(Length, 1);
106 
107  Length = sprintf(Buffer, "%");
108  ok_str(Buffer, "");
109  ok_int(Length, 0);
110 
111  Length = sprintf(Buffer, "%%%");
112  ok_str(Buffer, "%");
113  ok_int(Length, 1);
114 
115  Length = sprintf(Buffer, "%d", 8);
116  ok_str(Buffer, "8");
117  ok_int(Length, 1);
118 
119  Length = sprintf(Buffer, "%s", "hello");
120  ok_str(Buffer, "hello");
121  ok_int(Length, 5);
122 
123  /* field width for %s */
124  Length = sprintf(Buffer, "%8s", "hello");
125  ok_str(Buffer, " hello");
126  ok_int(Length, 8);
127 
128  Length = sprintf(Buffer, "%4s", "hello");
129  ok_str(Buffer, "hello");
130  ok_int(Length, 5);
131 
132  Length = sprintf(Buffer, "%-8s", "hello");
133  ok_str(Buffer, "hello ");
134  ok_int(Length, 8);
135 
136  Length = sprintf(Buffer, "%-5s", "hello");
137  ok_str(Buffer, "hello");
138  ok_int(Length, 5);
139 
140  Length = sprintf(Buffer, "%0s", "hello");
141  ok_str(Buffer, "hello");
142  ok_int(Length, 5);
143 
144  Length = sprintf(Buffer, "%-0s", "hello");
145  ok_str(Buffer, "hello");
146  ok_int(Length, 5);
147 
148  Length = sprintf(Buffer, "%*s", -8, "hello");
149 #ifdef TEST_USER32
150  ok_str(Buffer, "*s");
151  ok_int(Length, 2);
152 #else
153  ok_str(Buffer, "hello ");
154  ok_int(Length, 8);
155 #endif
156 
157  /* precision for %s */
158  Length = sprintf(Buffer, "%.s", "hello");
159  ok_str(Buffer, "");
160  ok_int(Length, 0);
161 
162  Length = sprintf(Buffer, "%.0s", "hello");
163  ok_str(Buffer, "");
164  ok_int(Length, 0);
165 
166  Length = sprintf(Buffer, "%.10s", "hello");
167  ok_str(Buffer, "hello");
168  ok_int(Length, 5);
169 
170  Length = sprintf(Buffer, "%.5s", "hello");
171  ok_str(Buffer, "hello");
172  ok_int(Length, 5);
173 
174  Length = sprintf(Buffer, "%.4s", "hello");
175  ok_str(Buffer, "hell");
176  ok_int(Length, 4);
177 
178  StartSeh()
179  Length = sprintf(Buffer, "%.*s", -1, "hello");
180 #ifdef TEST_USER32
181  ok_str(Buffer, "*s");
182  ok_int(Length, 2);
183 #else
184  ok_str(Buffer, "hello");
185  ok_int(Length, 5);
186 #endif
188 
189  String = AllocateGuarded(6);
190  if (!String)
191  {
192  skip("Guarded allocation failure\n");
193  return;
194  }
195 
196  strcpy(String, "hello");
197  StartSeh()
198  Length = sprintf(Buffer, "%.8s", String);
199  ok_str(Buffer, "hello");
200  ok_int(Length, 5);
202 
203  StartSeh()
204  Length = sprintf(Buffer, "%.6s", String);
205  ok_str(Buffer, "hello");
206  ok_int(Length, 5);
208 
209  StartSeh()
210  Length = sprintf(Buffer, "%.5s", String);
211  ok_str(Buffer, "hello");
212  ok_int(Length, 5);
214 
215  StartSeh()
216  Length = sprintf(Buffer, "%.4s", String);
217  ok_str(Buffer, "hell");
218  ok_int(Length, 4);
220 
221  String[5] = '!';
222  StartSeh()
223  Length = sprintf(Buffer, "%.5s", String);
224  ok_str(Buffer, "hello");
225  ok_int(Length, 5);
226 #ifdef TEST_USER32
228 #else
230 #endif
231 
232  StartSeh()
233  Length = sprintf(Buffer, "%.6s", String);
234  ok_str(Buffer, "hello!");
235  ok_int(Length, 6);
236 #ifdef TEST_USER32
238 #else
240 #endif
241 
242  StartSeh()
243  Length = sprintf(Buffer, "%.*s", 5, String);
244 #ifdef TEST_USER32
245  ok_str(Buffer, "*s");
246  ok_int(Length, 2);
247 #else
248  ok_str(Buffer, "hello");
249  ok_int(Length, 5);
250 #endif
252 
253  StartSeh()
254  Length = sprintf(Buffer, "%.*s", 6, String);
255 #ifdef TEST_USER32
256  ok_str(Buffer, "*s");
257  ok_int(Length, 2);
258 #else
259  ok_str(Buffer, "hello!");
260  ok_int(Length, 6);
261 #endif
263 
264  /* both field width and precision */
265  StartSeh()
266  Length = sprintf(Buffer, "%8.5s", String);
267  ok_str(Buffer, " hello");
268  ok_int(Length, 8);
269 #ifdef TEST_USER32
271 #else
273 #endif
274 
275  StartSeh()
276  Length = sprintf(Buffer, "%-*.6s", -8, String);
277 #ifdef TEST_USER32
278  ok_str(Buffer, "*.6s");
279  ok_int(Length, 4);
280 #else
281  ok_str(Buffer, "hello! ");
282  ok_int(Length, 8);
283 #endif
285 
286  StartSeh()
287  Length = sprintf(Buffer, "%*.*s", -8, 6, String);
288 #ifdef TEST_USER32
289  ok_str(Buffer, "*.*s");
290  ok_int(Length, 4);
291 #else
292  ok_str(Buffer, "hello! ");
293  ok_int(Length, 8);
294 #endif
296 
298 }
signed char * PCHAR
Definition: retypes.h:7
#define ok_str(x, y)
Definition: sprintf.c:18
#define ok_int(x, y)
Definition: sprintf.c:21
char CHAR
Definition: xmlstorage.h:175
LONG NTSTATUS
Definition: precomp.h:26
static PVOID AllocateGuarded(SIZE_T SizeRequested)
Definition: sprintf.c:30
#define PAGE_ROUND_DOWN(x)
Definition: mmtypes.h:36
static WCHAR String[]
Definition: stringtable.c:55
#define MEM_COMMIT
Definition: nt_native.h:1313
#define StartSeh()
Definition: _sntprintf.h:16
#define PAGE_ROUND_UP(x)
Definition: scsiport_int.h:13
#define MEM_RESERVE
Definition: nt_native.h:1314
#define PAGE_NOACCESS
Definition: nt_native.h:1302
smooth NULL
Definition: ftsmooth.c:416
Definition: bufpool.h:45
void * PVOID
Definition: retypes.h:9
#define NtCurrentProcess()
Definition: nt_native.h:1657
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
static VOID FreeGuarded(PVOID Pointer)
Definition: sprintf.c:64
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
#define PAGE_SIZE
Definition: env_spec_w32.h:49
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
Status
Definition: gdiplustypes.h:24
ULONG_PTR SIZE_T
Definition: typedefs.h:78
NTSTATUS NTAPI NtAllocateVirtualMemory(IN HANDLE ProcessHandle, IN OUT PVOID *UBaseAddress, IN ULONG_PTR ZeroBits, IN OUT PSIZE_T URegionSize, IN ULONG AllocationType, IN ULONG Protect)
Definition: virtual.c:4367
#define STATUS_ACCESS_VIOLATION
Definition: ntstatus.h:228
#define ok(value,...)
Definition: atltest.h:57
int sprintf(char *buf, const char *fmt,...)
Definition: sprintf.c:703
#define skip(...)
Definition: atltest.h:64
#define MEM_RELEASE
Definition: nt_native.h:1316
char * strcpy(char *DstString, const char *SrcString)
Definition: utclib.c:388
return STATUS_SUCCESS
Definition: btrfs.c:2966
START_TEST(sprintf)
Definition: sprintf.c:77
#define PAGE_READWRITE
Definition: nt_native.h:1304
#define EndSeh(ExpectedStatus)
Definition: _sntprintf.h:17
NTSTATUS NTAPI NtFreeVirtualMemory(IN HANDLE ProcessHandle, IN PVOID *UBaseAddress, IN PSIZE_T URegionSize, IN ULONG FreeType)
Definition: virtual.c:5090