lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <locale.h> |
| 4 | #include <string.h> |
| 5 | #include <libc-internal.h> |
| 6 | |
| 7 | |
| 8 | static int |
| 9 | do_test (void) |
| 10 | { |
| 11 | char buf[100]; |
| 12 | int result = 0; |
| 13 | |
| 14 | if (sprintf (buf, "%.0ls", L"foo") != 0 |
| 15 | || strlen (buf) != 0) |
| 16 | { |
| 17 | puts ("sprintf (buf, \"%.0ls\", L\"foo\") produced some output"); |
| 18 | result = 1; |
| 19 | } |
| 20 | |
| 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 | |
| 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 | |
| 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) |
| 56 | { |
| 57 | printf ("sprintf (buf, \"%%67108863.16\\\"%%d\", 7) produced `%s' output", |
| 58 | buf); |
| 59 | result = 1; |
| 60 | } |
| 61 | |
| 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) |
| 69 | { |
| 70 | printf ("sprintf (buf, \"%%*\\\"%%d\", 0x3ffffff, 7) produced `%s' output", buf); |
| 71 | result = 1; |
| 72 | } |
| 73 | |
| 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 | |
| 86 | return result; |
| 87 | } |
| 88 | |
| 89 | #define TEST_FUNCTION do_test () |
| 90 | #include "../test-skeleton.c" |