1 /* Check static buffer handling with setvbuf (BZ #22415)
2 
3    Copyright (C) 2017-2022 Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
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    <https://www.gnu.org/licenses/>.  */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <wchar.h>
23 #include <mcheck.h>
24 
25 #include <support/check.h>
26 #include <support/temp_file.h>
27 
28 static int
do_test(void)29 do_test (void)
30 {
31   mtrace ();
32 
33   char *temp_file;
34   TEST_VERIFY_EXIT (create_temp_file ("tst-bz22145.", &temp_file));
35 
36   char buf[BUFSIZ];
37 
38   {
39     /* Check if backup buffer is correctly freed and changing back
40        to normal buffer does not trigger an invalid free in case of
41        static buffer set by setvbuf.  */
42 
43     FILE *f = fopen (temp_file, "w+b");
44     TEST_VERIFY_EXIT (f != NULL);
45 
46     TEST_VERIFY_EXIT (setvbuf (f, buf, _IOFBF, BUFSIZ) == 0);
47     TEST_VERIFY_EXIT (ungetc ('x', f) == 'x');
48     TEST_VERIFY_EXIT (fseek (f, 0L, SEEK_SET) == 0);
49     TEST_VERIFY_EXIT (fputc ('y', f) ==  'y');
50 
51     TEST_VERIFY_EXIT (fclose (f) == 0);
52   }
53 
54   {
55     /* Check if backup buffer is correctly freed and changing back
56        to normal buffer does not trigger an invalid free in case of
57        static buffer set by setvbuf.  */
58 
59     FILE *f = fopen (temp_file, "w+b");
60     TEST_VERIFY_EXIT (f != NULL);
61 
62     TEST_VERIFY_EXIT (setvbuf (f, buf, _IOFBF, BUFSIZ) == 0);
63     TEST_VERIFY_EXIT (ungetc ('x', f) == 'x');
64     TEST_VERIFY_EXIT (fputc ('y', f) ==  'y');
65 
66     TEST_VERIFY_EXIT (fclose (f) == 0);
67   }
68 
69   {
70     FILE *f = fopen (temp_file, "w+b");
71     TEST_VERIFY_EXIT (f != NULL);
72 
73     TEST_VERIFY_EXIT (setvbuf (f, buf, _IOFBF, BUFSIZ) == 0);
74     TEST_VERIFY_EXIT (ungetwc (L'x', f) == L'x');
75     TEST_VERIFY_EXIT (fseek (f, 0L, SEEK_SET) == 0);
76     TEST_VERIFY_EXIT (fputwc (L'y', f) ==  L'y');
77 
78     TEST_VERIFY_EXIT (fclose (f) == 0);
79   }
80 
81   {
82     FILE *f = fopen (temp_file, "w+b");
83     TEST_VERIFY_EXIT (f != NULL);
84 
85     TEST_VERIFY_EXIT (setvbuf (f, buf, _IOFBF, BUFSIZ) == 0);
86     TEST_VERIFY_EXIT (ungetwc (L'x', f) == L'x');
87     TEST_VERIFY_EXIT (fputwc (L'y', f) ==  L'y');
88 
89     TEST_VERIFY_EXIT (fclose (f) == 0);
90   }
91 
92   free (temp_file);
93 
94   return 0;
95 }
96 
97 #include <support/test-driver.c>
98