re_search_internal: Avoid overflow in computing re_malloc buffer size
[glibc.git] / libio / bug-wmemstream1.c
blob22d67f71e93f481a743377e8425480bb8eca6721
1 #include <stdio.h>
2 #include <string.h>
3 #include <wchar.h>
6 static int
7 do_test (void)
9 size_t size;
10 wchar_t *buf;
11 FILE *fp = open_wmemstream (&buf, &size);
12 if (fp == NULL)
14 puts ("open_wmemstream failed");
15 return 1;
18 off64_t off = ftello64 (fp);
19 if (off != 0)
21 puts ("initial position wrong");
22 return 1;
25 if (fseek (fp, 32768, SEEK_SET) != 0)
27 puts ("fseek failed");
28 return 1;
31 if (fputws (L"foo", fp) == EOF)
33 puts ("fputws failed");
34 return 1;
37 if (fclose (fp) == EOF)
39 puts ("fclose failed");
40 return 1;
43 if (size != 32768 + 3)
45 printf ("expected size %d, got %zu\n", 32768 + 3, size);
46 return 1;
49 for (int i = 0; i < 32768; ++i)
50 if (buf[i] != L'\0')
52 printf ("wide character at offset %d is %#x\n",
53 i, (unsigned int) buf[i]);
54 return 1;
57 if (wmemcmp (buf + 32768, L"foo", 3) != 0)
59 puts ("written string incorrect");
60 return 1;
63 /* Mark the buffer. */
64 wmemset (buf, L'A', size);
65 free (buf);
67 /* Try again, this time with write mode enabled before the seek. */
68 fp = open_wmemstream (&buf, &size);
69 if (fp == NULL)
71 puts ("2nd open_wmemstream failed");
72 return 1;
75 off = ftello64 (fp);
76 if (off != 0)
78 puts ("2nd initial position wrong");
79 return 1;
82 if (fputws (L"bar", fp) == EOF)
84 puts ("2nd fputws failed");
85 return 1;
88 if (fseek (fp, 32768, SEEK_SET) != 0)
90 puts ("2nd fseek failed");
91 return 1;
94 if (fputws (L"foo", fp) == EOF)
96 puts ("3rd fputws failed");
97 return 1;
100 if (fclose (fp) == EOF)
102 puts ("2nd fclose failed");
103 return 1;
106 if (size != 32768 + 3)
108 printf ("2nd expected size %d, got %zu\n", 32768 + 3, size);
109 return 1;
112 if (wmemcmp (buf, L"bar", 3) != 0)
114 puts ("initial string incorrect in 2nd try");
115 return 1;
118 for (int i = 3; i < 32768; ++i)
119 if (buf[i] != L'\0')
121 printf ("wide character at offset %d is %#x in 2nd try\n",
122 i, (unsigned int) buf[i]);
123 return 1;
126 if (wmemcmp (buf + 32768, L"foo", 3) != 0)
128 puts ("written string incorrect in 2nd try");
129 return 1;
132 return 0;
135 #define TEST_FUNCTION do_test ()
136 #include "../test-skeleton.c"