tst-wfile-sync.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* Test that _IO_wfile_sync does not crash (bug 20568).
  2. Copyright (C) 2019-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 <fcntl.h>
  16. #include <locale.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <wchar.h>
  21. #include <support/check.h>
  22. #include <support/xstdio.h>
  23. #include <support/xunistd.h>
  24. #include <support/temp_file.h>
  25. static const char test_data[] = "This is a test of _IO_wfile_sync.";
  26. static int
  27. do_test (void)
  28. {
  29. static char *infile;
  30. int infd;
  31. FILE *infp;
  32. infd = create_temp_file ("tst-wfile-sync-in-", &infile);
  33. xwrite (infd, test_data, strlen (test_data));
  34. xclose (infd);
  35. infd = xopen (infile, O_RDONLY, 0);
  36. infp = fdopen (infd, "r");
  37. TEST_VERIFY_EXIT (setlocale (LC_ALL, "de_DE.UTF-8") != NULL);
  38. /* Fill the stdio buffer and advance the read pointer. */
  39. TEST_VERIFY_EXIT (fgetwc (infp) != WEOF);
  40. /* This calls _IO_wfile_sync, it should not crash. */
  41. TEST_VERIFY_EXIT (setvbuf (infp, NULL, _IONBF, 0) == 0);
  42. /* Verify that the external file offset has been synchronized. */
  43. TEST_COMPARE (xlseek (infd, 0, SEEK_CUR), 1);
  44. fclose (infp);
  45. free (infile);
  46. return 0;
  47. }
  48. #include <support/test-driver.c>