lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Tests of *printf for very large strings. |
| 2 | Copyright (C) 2000-2015 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | Contributed by Ulrich Drepper <drepper@redhat.com>, 2000. |
| 5 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; if not, see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include <locale.h> |
| 21 | #include <mcheck.h> |
| 22 | #include <stdint.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <unistd.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <libc-internal.h> |
| 29 | |
| 30 | |
| 31 | const char *locs[] = |
| 32 | { |
| 33 | "C", "de_DE.ISO-8859-1", "de_DE.UTF-8", "ja_JP.EUC-JP" |
| 34 | }; |
| 35 | #define nlocs (sizeof (locs) / sizeof (locs[0])) |
| 36 | |
| 37 | |
| 38 | char large[50000]; |
| 39 | |
| 40 | static int |
| 41 | do_test (void) |
| 42 | { |
| 43 | char buf[25]; |
| 44 | size_t i; |
| 45 | int res = 0; |
| 46 | int fd; |
| 47 | |
| 48 | mtrace (); |
| 49 | |
| 50 | strcpy (buf, "/tmp/test-vfprintfXXXXXX"); |
| 51 | fd = mkstemp (buf); |
| 52 | if (fd == -1) |
| 53 | { |
| 54 | printf ("cannot open temporary file: %m\n"); |
| 55 | exit (1); |
| 56 | } |
| 57 | unlink (buf); |
| 58 | |
| 59 | for (i = 0; i < nlocs; ++i) |
| 60 | { |
| 61 | FILE *fp; |
| 62 | struct stat st; |
| 63 | int fd2; |
| 64 | |
| 65 | setlocale (LC_ALL, locs[i]); |
| 66 | |
| 67 | memset (large, '\1', sizeof (large)); |
| 68 | large[sizeof (large) - 1] = '\0'; |
| 69 | |
| 70 | fd2 = dup (fd); |
| 71 | if (fd2 == -1) |
| 72 | { |
| 73 | printf ("cannot dup for locale %s: %m\n", |
| 74 | setlocale (LC_ALL, NULL)); |
| 75 | exit (1); |
| 76 | } |
| 77 | |
| 78 | if (ftruncate (fd2, 0) != 0) |
| 79 | { |
| 80 | printf ("cannot truncate file for locale %s: %m\n", |
| 81 | setlocale (LC_ALL, NULL)); |
| 82 | exit (1); |
| 83 | } |
| 84 | |
| 85 | fp = fdopen (fd2, "a"); |
| 86 | if (fp == NULL) |
| 87 | { |
| 88 | printf ("cannot create FILE for locale %s: %m\n", |
| 89 | setlocale (LC_ALL, NULL)); |
| 90 | exit (1); |
| 91 | } |
| 92 | |
| 93 | fprintf (fp, "%s", large); |
| 94 | fprintf (fp, "%.*s", 30000, large); |
| 95 | large[20000] = '\0'; |
| 96 | /* We're testing a large format string here and need to generate it |
| 97 | to avoid this source file being ridiculous. So disable the warning |
| 98 | about a generated format string. */ |
| 99 | DIAG_PUSH_NEEDS_COMMENT; |
| 100 | DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wformat-security"); |
| 101 | fprintf (fp, large); |
| 102 | DIAG_POP_NEEDS_COMMENT; |
| 103 | fprintf (fp, "%-1.300000000s", "hello"); |
| 104 | |
| 105 | if (fflush (fp) != 0 || ferror (fp) != 0 || fclose (fp) != 0) |
| 106 | { |
| 107 | printf ("write error for locale %s: %m\n", |
| 108 | setlocale (LC_ALL, NULL)); |
| 109 | exit (1); |
| 110 | } |
| 111 | |
| 112 | if (fstat (fd, &st) != 0) |
| 113 | { |
| 114 | printf ("cannot stat for locale %s: %m\n", |
| 115 | setlocale (LC_ALL, NULL)); |
| 116 | exit (1); |
| 117 | } |
| 118 | else if (st.st_size != 50000 + 30000 + 19999 + 5) |
| 119 | { |
| 120 | printf ("file size incorrect for locale %s: %jd instead of %jd\n", |
| 121 | setlocale (LC_ALL, NULL), |
| 122 | (intmax_t) st.st_size, |
| 123 | (intmax_t) 50000 + 30000 + 19999 + 5); |
| 124 | res = 1; |
| 125 | } |
| 126 | else |
| 127 | printf ("locale %s OK\n", setlocale (LC_ALL, NULL)); |
| 128 | } |
| 129 | |
| 130 | close (fd); |
| 131 | |
| 132 | return res; |
| 133 | } |
| 134 | |
| 135 | #define TEST_FUNCTION do_test () |
| 136 | #include "../test-skeleton.c" |