1 /* Check static buffer handling with setvbuf (BZ #22415)
3 Copyright (C) 2017-2023 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
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.
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.
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/>. */
25 #include <support/check.h>
26 #include <support/temp_file.h>
34 TEST_VERIFY_EXIT (create_temp_file ("tst-bz22145.", &temp_file
));
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. */
43 FILE *f
= fopen (temp_file
, "w+b");
44 TEST_VERIFY_EXIT (f
!= NULL
);
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');
51 TEST_VERIFY_EXIT (fclose (f
) == 0);
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. */
59 FILE *f
= fopen (temp_file
, "w+b");
60 TEST_VERIFY_EXIT (f
!= NULL
);
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');
66 TEST_VERIFY_EXIT (fclose (f
) == 0);
70 FILE *f
= fopen (temp_file
, "w+b");
71 TEST_VERIFY_EXIT (f
!= NULL
);
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');
78 TEST_VERIFY_EXIT (fclose (f
) == 0);
82 FILE *f
= fopen (temp_file
, "w+b");
83 TEST_VERIFY_EXIT (f
!= NULL
);
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');
89 TEST_VERIFY_EXIT (fclose (f
) == 0);
97 #include <support/test-driver.c>