tst-strtod-overflow.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /* Test for integer/buffer overflow in strtod.
  2. Copyright (C) 2012-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 <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <support/blob_repeat.h>
  19. #include <support/test-driver.h>
  20. #define EXPONENT "e-2147483649"
  21. #define SIZE 214748364
  22. static int
  23. do_test (void)
  24. {
  25. struct support_blob_repeat repeat = support_blob_repeat_allocate
  26. ("0", 1, 1 + SIZE + sizeof (EXPONENT));
  27. if (repeat.size == 0)
  28. {
  29. puts ("warning: memory allocation failed, cannot test for overflow");
  30. return EXIT_UNSUPPORTED;
  31. }
  32. char *p = repeat.start;
  33. p[0] = '1';
  34. memcpy (p + 1 + SIZE, EXPONENT, sizeof (EXPONENT));
  35. double d = strtod (p, NULL);
  36. if (d != 0)
  37. {
  38. printf ("error: strtod returned wrong value: %a\n", d);
  39. return 1;
  40. }
  41. support_blob_repeat_free (&repeat);
  42. return 0;
  43. }
  44. #define TEST_FUNCTION do_test ()
  45. #define TIMEOUT 30
  46. #include "../test-skeleton.c"