]>
Commit | Line | Data |
---|---|---|
13c5a442 | 1 | #include <stdio.h> |
fa4a36fd | 2 | #include <stdlib.h> |
715a900c | 3 | #include <locale.h> |
13c5a442 | 4 | #include <string.h> |
e15f7de6 | 5 | #include <libc-diag.h> |
13c5a442 UD |
6 | |
7 | ||
29955b5d AS |
8 | static int |
9 | do_test (void) | |
13c5a442 UD |
10 | { |
11 | char buf[100]; | |
12 | int result = 0; | |
13 | ||
14 | if (sprintf (buf, "%.0ls", L"foo") != 0 | |
15 | || strlen (buf) != 0) | |
16 | { | |
40a54e4d | 17 | puts ("sprintf (buf, \"%.0ls\", L\"foo\") produced some output"); |
13c5a442 UD |
18 | result = 1; |
19 | } | |
20 | ||
40a54e4d UD |
21 | #define SIZE (1024*70000) |
22 | #define STR(x) #x | |
23 | ||
24 | char *dst = malloc (SIZE + 1); | |
25 | ||
26 | if (dst == NULL) | |
27 | { | |
28 | puts ("memory allocation failure"); | |
29 | result = 1; | |
30 | } | |
31 | else | |
32 | { | |
33 | sprintf (dst, "%*s", SIZE, ""); | |
34 | if (strnlen (dst, SIZE + 1) != SIZE) | |
35 | { | |
36 | puts ("sprintf (dst, \"%*s\", " STR(SIZE) ", \"\") did not produce enough output"); | |
37 | result = 1; | |
38 | } | |
39 | free (dst); | |
40 | } | |
41 | ||
af269dd9 UD |
42 | if (sprintf (buf, "%1$d%3$.*2$s%4$d", 7, 67108863, "x", 8) != 3 |
43 | || strcmp (buf, "7x8") != 0) | |
44 | { | |
45 | printf ("sprintf (buf, \"%%1$d%%3$.*2$s%%4$d\", 7, 67108863, \"x\", 8) produced `%s' output", buf); | |
46 | result = 1; | |
47 | } | |
48 | ||
1c4053db RM |
49 | /* We are testing a corner case of the sprintf format string here. */ |
50 | DIAG_PUSH_NEEDS_COMMENT; | |
51 | DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat"); | |
52 | int n = sprintf (buf, "%67108863.16\"%d", 7); | |
53 | DIAG_POP_NEEDS_COMMENT; | |
54 | ||
55 | if (n != 14 || strcmp (buf, "%67108863.16\"7") != 0) | |
af269dd9 | 56 | { |
1c4053db RM |
57 | printf ("sprintf (buf, \"%%67108863.16\\\"%%d\", 7) produced `%s' output", |
58 | buf); | |
af269dd9 UD |
59 | result = 1; |
60 | } | |
61 | ||
1c4053db RM |
62 | /* We are testing a corner case of the sprintf format string here. */ |
63 | DIAG_PUSH_NEEDS_COMMENT; | |
64 | DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat"); | |
65 | n = sprintf (buf, "%*\"%d", 0x3ffffff, 7); | |
66 | DIAG_POP_NEEDS_COMMENT; | |
67 | ||
68 | if (n != 11 || strcmp (buf, "%67108863\"7") != 0) | |
af269dd9 UD |
69 | { |
70 | printf ("sprintf (buf, \"%%*\\\"%%d\", 0x3ffffff, 7) produced `%s' output", buf); | |
71 | result = 1; | |
72 | } | |
73 | ||
715a900c JL |
74 | if (setlocale (LC_ALL, "de_DE.UTF-8") == NULL) |
75 | { | |
76 | puts ("cannot set locale"); | |
77 | result = 1; | |
78 | } | |
79 | else if (sprintf (buf, "%.8s\n", "Foo: \277") != 7 | |
80 | || strcmp (buf, "Foo: \277\n") != 0) | |
81 | { | |
82 | printf ("sprintf (buf, \"%%.8s\\n\", \"Foo: \\277\") produced '%s' output\n", buf); | |
83 | result = 1; | |
84 | } | |
85 | ||
13c5a442 UD |
86 | return result; |
87 | } | |
29955b5d AS |
88 | |
89 | #define TEST_FUNCTION do_test () | |
90 | #include "../test-skeleton.c" |