tst-fortify-wide.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Fortify check for wprintf.
  2. Copyright (C) 2023-2026 Free Software Foundation, Inc.
  3. Copyright The GNU Toolchain Authors.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <https://www.gnu.org/licenses/>. */
  16. #include <setjmp.h>
  17. #include <stdio.h>
  18. #include <wchar.h>
  19. #include <unistd.h>
  20. #include <support/support.h>
  21. static volatile int chk_fail_ok;
  22. static volatile int ret;
  23. static sigjmp_buf chk_fail_buf;
  24. static void
  25. handler (int sig)
  26. {
  27. if (chk_fail_ok)
  28. {
  29. chk_fail_ok = 0;
  30. longjmp (chk_fail_buf, 1);
  31. }
  32. else
  33. _exit (127);
  34. }
  35. static const wchar_t *wstr3 = L"%ls%n%ls%n";
  36. static const wchar_t *wstr4 = L"Hello, ";
  37. static const wchar_t *wstr5 = L"World!\n";
  38. static wchar_t wbuf2[20] = L"%ls";
  39. #define WFAIL \
  40. do { wprintf (L"Failure on line %d\n", __LINE__); ret = 1; } while (0)
  41. #define CHK_FAIL_START \
  42. chk_fail_ok = 1; \
  43. if (! sigsetjmp (chk_fail_buf, 1)) \
  44. {
  45. #define CHK_FAIL_END \
  46. chk_fail_ok = 0; \
  47. WFAIL; \
  48. }
  49. static int
  50. do_test (void)
  51. {
  52. set_fortify_handler (handler);
  53. int n1, n2;
  54. int orientation = fwide (stdout, 1);
  55. if (orientation <= 0)
  56. WFAIL;
  57. /* Constant literals passed directly are always ok
  58. (even with warnings about possible bugs from GCC). */
  59. if (wprintf (L"%ls%n%ls%n", wstr4, &n1, wstr5, &n2) != 14
  60. || n1 != 7 || n2 != 14)
  61. WFAIL;
  62. /* In this case the format string is not known at compile time,
  63. but resides in read-only memory, so is ok. */
  64. if (wprintf (wstr3, wstr4, &n1, wstr5, &n2) != 14
  65. || n1 != 7 || n2 != 14)
  66. WFAIL;
  67. wcpcpy (&wbuf2[3], L"%n%ls%n");
  68. /* When the format string is writable and contains %n,
  69. with -D_FORTIFY_SOURCE=2 it causes __chk_fail. */
  70. CHK_FAIL_START
  71. if (wprintf (wbuf2, wstr4, &n1, wstr5, &n1) != 14)
  72. WFAIL;
  73. CHK_FAIL_END
  74. /* But if there is no %n, even writable format string
  75. should work. */
  76. wbuf2[8] = L'\0';
  77. if (wprintf (&wbuf2[5], wstr5) != 7)
  78. WFAIL;
  79. /* Check whether missing N$ formats are detected. */
  80. CHK_FAIL_START
  81. wprintf (L"%3$d\n", 1, 2, 3, 4);
  82. CHK_FAIL_END
  83. return ret;
  84. }
  85. #include <support/test-driver.c>