test-nan-overflow.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* Test nan functions stack overflow (bug 16962).
  2. Copyright (C) 2015-2016 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 <math.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <sys/resource.h>
  20. #define STACK_LIM 1048576
  21. #define STRING_SIZE (2 * STACK_LIM)
  22. static int
  23. do_test (void)
  24. {
  25. int result = 0;
  26. struct rlimit lim;
  27. getrlimit (RLIMIT_STACK, &lim);
  28. lim.rlim_cur = STACK_LIM;
  29. setrlimit (RLIMIT_STACK, &lim);
  30. char *nanstr = malloc (STRING_SIZE);
  31. if (nanstr == NULL)
  32. {
  33. puts ("malloc failed, cannot test");
  34. return 77;
  35. }
  36. memset (nanstr, '0', STRING_SIZE - 1);
  37. nanstr[STRING_SIZE - 1] = 0;
  38. #define NAN_TEST(TYPE, FUNC) \
  39. do \
  40. { \
  41. char *volatile p = nanstr; \
  42. volatile TYPE v = FUNC (p); \
  43. if (isnan (v)) \
  44. puts ("PASS: " #FUNC); \
  45. else \
  46. { \
  47. puts ("FAIL: " #FUNC); \
  48. result = 1; \
  49. } \
  50. } \
  51. while (0)
  52. NAN_TEST (float, nanf);
  53. NAN_TEST (double, nan);
  54. #ifndef NO_LONG_DOUBLE
  55. NAN_TEST (long double, nanl);
  56. #endif
  57. return result;
  58. }
  59. #define TEST_FUNCTION do_test ()
  60. #include "../test-skeleton.c"