tst-dynarray-at-fail.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Test reporting of out-of-bounds access for dynamic arrays.
  2. Copyright (C) 2017-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 "tst-dynarray-shared.h"
  16. #include <signal.h>
  17. #include <stdint.h>
  18. #include <string.h>
  19. #include <support/capture_subprocess.h>
  20. #include <support/check.h>
  21. /* Run CALLBACK and check that the data on standard error equals
  22. EXPECTED. */
  23. static void
  24. check (const char *test, void (*callback) (void *), size_t index,
  25. const char *expected)
  26. {
  27. struct support_capture_subprocess result
  28. = support_capture_subprocess (callback, &index);
  29. if (strcmp (result.err.buffer, expected) != 0)
  30. {
  31. support_record_failure ();
  32. printf ("error: test %s (%zu) unexpected standard error data\n"
  33. " expected: %s\n"
  34. " actual: %s\n",
  35. test, index, expected, result.err.buffer);
  36. }
  37. TEST_VERIFY (strlen (result.out.buffer) == 0);
  38. TEST_VERIFY (WIFSIGNALED (result.status));
  39. if (WIFSIGNALED (result.status))
  40. TEST_VERIFY (WTERMSIG (result.status) == SIGABRT);
  41. support_capture_subprocess_free (&result);
  42. }
  43. /* Try indexing an empty array. */
  44. static void
  45. test_empty (void *closure)
  46. {
  47. size_t *pindex = closure;
  48. struct dynarray_int dyn;
  49. dynarray_int_init (&dyn);
  50. dynarray_int_at (&dyn, *pindex);
  51. }
  52. /* Try indexing a one-element array. */
  53. static void
  54. test_one (void *closure)
  55. {
  56. size_t *pindex = closure;
  57. struct dynarray_int dyn;
  58. dynarray_int_init (&dyn);
  59. TEST_VERIFY (dynarray_int_resize (&dyn, 1));
  60. dynarray_int_at (&dyn, *pindex);
  61. }
  62. /* Try indexing a longer array. */
  63. static void
  64. test_many (void *closure)
  65. {
  66. size_t *pindex = closure;
  67. struct dynarray_int dyn;
  68. dynarray_int_init (&dyn);
  69. TEST_VERIFY (dynarray_int_resize (&dyn, 5371));
  70. dynarray_int_at (&dyn, *pindex);
  71. }
  72. /* (size_t) -1 for use in string literals. */
  73. #if SIZE_WIDTH == 32
  74. # define MINUS_1 "4294967295"
  75. #elif SIZE_WIDTH == 64
  76. # define MINUS_1 "18446744073709551615"
  77. #else
  78. # error "unknown value for SIZE_WIDTH"
  79. #endif
  80. static int
  81. do_test (void)
  82. {
  83. TEST_VERIFY (setenv ("LIBC_FATAL_STDERR_", "1", 1) == 0);
  84. check ("test_empty", test_empty, 0,
  85. "Fatal glibc error: array index 0 not less than array length 0\n");
  86. check ("test_empty", test_empty, 1,
  87. "Fatal glibc error: array index 1 not less than array length 0\n");
  88. check ("test_empty", test_empty, -1,
  89. "Fatal glibc error: array index " MINUS_1
  90. " not less than array length 0\n");
  91. check ("test_one", test_one, 1,
  92. "Fatal glibc error: array index 1 not less than array length 1\n");
  93. check ("test_one", test_one, 2,
  94. "Fatal glibc error: array index 2 not less than array length 1\n");
  95. check ("test_one", test_one, -1,
  96. "Fatal glibc error: array index " MINUS_1
  97. " not less than array length 1\n");
  98. check ("test_many", test_many, 5371,
  99. "Fatal glibc error: array index 5371"
  100. " not less than array length 5371\n");
  101. check ("test_many", test_many, 5372,
  102. "Fatal glibc error: array index 5372"
  103. " not less than array length 5371\n");
  104. check ("test_many", test_many, -1,
  105. "Fatal glibc error: array index " MINUS_1
  106. " not less than array length 5371\n");
  107. return 0;
  108. }
  109. #include <support/test-driver.c>