tst-bz22415.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* Check static buffer handling with setvbuf (BZ #22415)
  2. Copyright (C) 2017-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <wchar.h>
  18. #include <mcheck.h>
  19. #include <support/check.h>
  20. #include <support/temp_file.h>
  21. static int
  22. do_test (void)
  23. {
  24. mtrace ();
  25. char *temp_file;
  26. TEST_VERIFY_EXIT (create_temp_file ("tst-bz22145.", &temp_file));
  27. char buf[BUFSIZ];
  28. {
  29. /* Check if backup buffer is correctly freed and changing back
  30. to normal buffer does not trigger an invalid free in case of
  31. static buffer set by setvbuf. */
  32. FILE *f = fopen (temp_file, "w+b");
  33. TEST_VERIFY_EXIT (f != NULL);
  34. TEST_VERIFY_EXIT (setvbuf (f, buf, _IOFBF, BUFSIZ) == 0);
  35. TEST_VERIFY_EXIT (ungetc ('x', f) == 'x');
  36. TEST_VERIFY_EXIT (fseek (f, 0L, SEEK_SET) == 0);
  37. TEST_VERIFY_EXIT (fputc ('y', f) == 'y');
  38. TEST_VERIFY_EXIT (fclose (f) == 0);
  39. }
  40. {
  41. /* Check if backup buffer is correctly freed and changing back
  42. to normal buffer does not trigger an invalid free in case of
  43. static buffer set by setvbuf. */
  44. FILE *f = fopen (temp_file, "w+b");
  45. 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 (fputc ('y', f) == 'y');
  49. TEST_VERIFY_EXIT (fclose (f) == 0);
  50. }
  51. {
  52. FILE *f = fopen (temp_file, "w+b");
  53. TEST_VERIFY_EXIT (f != NULL);
  54. TEST_VERIFY_EXIT (setvbuf (f, buf, _IOFBF, BUFSIZ) == 0);
  55. TEST_VERIFY_EXIT (ungetwc (L'x', f) == L'x');
  56. TEST_VERIFY_EXIT (fseek (f, 0L, SEEK_SET) == 0);
  57. TEST_VERIFY_EXIT (fputwc (L'y', f) == L'y');
  58. TEST_VERIFY_EXIT (fclose (f) == 0);
  59. }
  60. {
  61. FILE *f = fopen (temp_file, "w+b");
  62. TEST_VERIFY_EXIT (f != NULL);
  63. TEST_VERIFY_EXIT (setvbuf (f, buf, _IOFBF, BUFSIZ) == 0);
  64. TEST_VERIFY_EXIT (ungetwc (L'x', f) == L'x');
  65. TEST_VERIFY_EXIT (fputwc (L'y', f) == L'y');
  66. TEST_VERIFY_EXIT (fclose (f) == 0);
  67. }
  68. free (temp_file);
  69. return 0;
  70. }
  71. #include <support/test-driver.c>