tst-freopen.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Test freopen with mmap stdio.
  2. Copyright (C) 2002-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 <string.h>
  18. #include <unistd.h>
  19. #include <libc-diag.h>
  20. #include <support/check.h>
  21. #include <support/temp_file.h>
  22. static int fd;
  23. static char *name;
  24. static void
  25. do_prepare (int argc, char *argv[])
  26. {
  27. fd = create_temp_file ("tst-freopen.", &name);
  28. TEST_VERIFY_EXIT (fd != -1);
  29. }
  30. #define PREPARE do_prepare
  31. /* Basic tests for freopen. */
  32. static void
  33. do_test_basic (void)
  34. {
  35. const char * const test = "Let's test freopen.\n";
  36. DIAG_PUSH_NEEDS_COMMENT_CLANG;
  37. DIAG_IGNORE_NEEDS_COMMENT_CLANG (3.4, "-Wgnu-folding-constant");
  38. char temp[strlen (test) + 1];
  39. DIAG_POP_NEEDS_COMMENT_CLANG;
  40. FILE *f = fdopen (fd, "w");
  41. if (f == NULL)
  42. FAIL_EXIT1 ("fdopen: %m");
  43. fputs (test, f);
  44. fclose (f);
  45. f = fopen (name, "r");
  46. if (f == NULL)
  47. FAIL_EXIT1 ("fopen: %m");
  48. if (fread (temp, 1, strlen (test), f) != strlen (test))
  49. FAIL_EXIT1 ("fread: %m");
  50. temp [strlen (test)] = '\0';
  51. if (strcmp (test, temp))
  52. FAIL_EXIT1 ("read different string than was written: (%s, %s)",
  53. test, temp);
  54. f = freopen (name, "r+", f);
  55. if (f == NULL)
  56. FAIL_EXIT1 ("freopen: %m");
  57. if (fseek (f, 0, SEEK_SET) != 0)
  58. FAIL_EXIT1 ("fseek: %m");
  59. if (fread (temp, 1, strlen (test), f) != strlen (test))
  60. FAIL_EXIT1 ("fread: %m");
  61. temp [strlen (test)] = '\0';
  62. if (strcmp (test, temp))
  63. FAIL_EXIT1 ("read different string than was written: (%s, %s)",
  64. test, temp);
  65. fclose (f);
  66. }
  67. #if defined __GNUC__ && __GNUC__ >= 11
  68. /* Force an error to detect incorrectly making freopen a deallocator
  69. for its last argument via attribute malloc. The function closes
  70. the stream without deallocating it so either the argument or
  71. the pointer returned from the function (but not both) can be passed
  72. to fclose. */
  73. #pragma GCC diagnostic push
  74. #pragma GCC diagnostic error "-Wmismatched-dealloc"
  75. #endif
  76. /* Verify that freopen returns stream. */
  77. static void
  78. do_test_return_stream (void)
  79. {
  80. FILE *f1 = fopen (name, "r");
  81. if (f1 == NULL)
  82. FAIL_EXIT1 ("fopen: %m");
  83. FILE *f2 = freopen (name, "r+", f1);
  84. if (f2 == NULL)
  85. FAIL_EXIT1 ("freopen: %m");
  86. /* Verify that freopen isn't declared with the no-argument attribute
  87. malloc (which could let GCC fold the inequality to false). */
  88. if (f1 != f2)
  89. FAIL_EXIT1 ("freopen returned a different stream");
  90. /* This shouldn't trigger -Wmismatched-dealloc. */
  91. fclose (f1);
  92. }
  93. #if defined __GNUC__ && __GNUC__ >= 11
  94. /* Pop -Wmismatched-dealloc set to error above. */
  95. # pragma GCC diagnostic pop
  96. #endif
  97. /* Test for BZ#21398, where it tries to freopen stdio after the close
  98. of its file descriptor. */
  99. static void
  100. do_test_bz21398 (void)
  101. {
  102. (void) close (STDIN_FILENO);
  103. FILE *f = freopen (name, "r", stdin);
  104. if (f == NULL)
  105. FAIL_EXIT1 ("freopen: %m");
  106. TEST_VERIFY_EXIT (ferror (f) == 0);
  107. char buf[128];
  108. char *ret = fgets (buf, sizeof (buf), stdin);
  109. TEST_VERIFY_EXIT (ret != NULL);
  110. TEST_VERIFY_EXIT (ferror (f) == 0);
  111. }
  112. static int
  113. do_test (void)
  114. {
  115. do_test_basic ();
  116. do_test_bz21398 ();
  117. do_test_return_stream ();
  118. return 0;
  119. }
  120. #include <support/test-driver.c>