tst-strerror-fail.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* Check that strerror, strerror_l do not return NULL on failure (bug 30555).
  2. Copyright (C) 2023-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 <locale.h>
  16. #include <stdbool.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <support/check.h>
  20. #include <support/namespace.h>
  21. #include <support/xdlfcn.h>
  22. /* Interposed malloc that can be used to inject allocation failures. */
  23. static volatile bool fail_malloc;
  24. void *
  25. malloc (size_t size)
  26. {
  27. if (fail_malloc)
  28. return NULL;
  29. static void *(*original_malloc) (size_t);
  30. if (original_malloc == NULL)
  31. original_malloc = xdlsym (RTLD_NEXT, "malloc");
  32. return original_malloc (size);
  33. }
  34. /* Callbacks for the actual tests. Use fork to run both tests with a
  35. clean state. */
  36. static void
  37. test_strerror (void *closure)
  38. {
  39. fail_malloc = true;
  40. const char *s = strerror (999);
  41. fail_malloc = false;
  42. TEST_COMPARE_STRING (s, "Unknown error");
  43. }
  44. static void
  45. test_strerror_l (void *closure)
  46. {
  47. locale_t loc = newlocale (LC_ALL, "C", (locale_t) 0);
  48. TEST_VERIFY (loc != (locale_t) 0);
  49. fail_malloc = true;
  50. const char *s = strerror_l (999, loc);
  51. fail_malloc = false;
  52. TEST_COMPARE_STRING (s, "Unknown error");
  53. freelocale (loc);
  54. }
  55. static int
  56. do_test (void)
  57. {
  58. support_isolate_in_subprocess (test_strerror, NULL);
  59. support_isolate_in_subprocess (test_strerror_l, NULL);
  60. return 0;
  61. }
  62. #include <support/test-driver.c>