test-strerror-errno.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* BZ #24024 strerror and errno test.
  2. Copyright (C) 2019 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. <http://www.gnu.org/licenses/>. */
  15. #include <dlfcn.h>
  16. #include <errno.h>
  17. #include <string.h>
  18. #include <support/check.h>
  19. #include <support/support.h>
  20. /* malloc is allowed to change errno to a value different than 0, even when
  21. there is no actual error. This happens for example when the memory
  22. allocation through sbrk fails. Simulate this by interposing our own
  23. malloc implementation which sets errno to ENOMEM and calls the original
  24. malloc. */
  25. void
  26. *malloc (size_t size)
  27. {
  28. static void *(*real_malloc) (size_t size);
  29. if (!real_malloc)
  30. real_malloc = dlsym (RTLD_NEXT, "malloc");
  31. errno = ENOMEM;
  32. return (*real_malloc) (size);
  33. }
  34. /* strerror must not change the value of errno. Unfortunately due to GCC bug
  35. #88576, this happens when -fmath-errno is used. This simple test checks
  36. that it doesn't happen. */
  37. static int
  38. do_test (void)
  39. {
  40. char *msg;
  41. errno = 0;
  42. msg = strerror (-3);
  43. (void) msg;
  44. TEST_COMPARE (errno, 0);
  45. return 0;
  46. }
  47. #include <support/test-driver.c>