C2x scanf %b support
[glibc.git] / malloc / tst-dynarray-at-fail.c
blob860e3b945db63c2f6e47577d92d7a77b56bcad50
1 /* Test reporting of out-of-bounds access for dynamic arrays.
2 Copyright (C) 2017-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include "tst-dynarray-shared.h"
21 #include <signal.h>
22 #include <stdint.h>
23 #include <string.h>
24 #include <support/capture_subprocess.h>
25 #include <support/check.h>
27 /* Run CALLBACK and check that the data on standard error equals
28 EXPECTED. */
29 static void
30 check (const char *test, void (*callback) (void *), size_t index,
31 const char *expected)
33 struct support_capture_subprocess result
34 = support_capture_subprocess (callback, &index);
35 if (strcmp (result.err.buffer, expected) != 0)
37 support_record_failure ();
38 printf ("error: test %s (%zu) unexpected standard error data\n"
39 " expected: %s\n"
40 " actual: %s\n",
41 test, index, expected, result.err.buffer);
43 TEST_VERIFY (strlen (result.out.buffer) == 0);
44 TEST_VERIFY (WIFSIGNALED (result.status));
45 if (WIFSIGNALED (result.status))
46 TEST_VERIFY (WTERMSIG (result.status) == SIGABRT);
47 support_capture_subprocess_free (&result);
50 /* Try indexing an empty array. */
51 static void
52 test_empty (void *closure)
54 size_t *pindex = closure;
55 struct dynarray_int dyn;
56 dynarray_int_init (&dyn);
57 dynarray_int_at (&dyn, *pindex);
60 /* Try indexing a one-element array. */
61 static void
62 test_one (void *closure)
64 size_t *pindex = closure;
65 struct dynarray_int dyn;
66 dynarray_int_init (&dyn);
67 TEST_VERIFY (dynarray_int_resize (&dyn, 1));
68 dynarray_int_at (&dyn, *pindex);
71 /* Try indexing a longer array. */
72 static void
73 test_many (void *closure)
75 size_t *pindex = closure;
76 struct dynarray_int dyn;
77 dynarray_int_init (&dyn);
78 TEST_VERIFY (dynarray_int_resize (&dyn, 5371));
79 dynarray_int_at (&dyn, *pindex);
82 /* (size_t) -1 for use in string literals. */
83 #if SIZE_WIDTH == 32
84 # define MINUS_1 "4294967295"
85 #elif SIZE_WIDTH == 64
86 # define MINUS_1 "18446744073709551615"
87 #else
88 # error "unknown value for SIZE_WIDTH"
89 #endif
91 static int
92 do_test (void)
94 TEST_VERIFY (setenv ("LIBC_FATAL_STDERR_", "1", 1) == 0);
96 check ("test_empty", test_empty, 0,
97 "Fatal glibc error: array index 0 not less than array length 0\n");
98 check ("test_empty", test_empty, 1,
99 "Fatal glibc error: array index 1 not less than array length 0\n");
100 check ("test_empty", test_empty, -1,
101 "Fatal glibc error: array index " MINUS_1
102 " not less than array length 0\n");
104 check ("test_one", test_one, 1,
105 "Fatal glibc error: array index 1 not less than array length 1\n");
106 check ("test_one", test_one, 2,
107 "Fatal glibc error: array index 2 not less than array length 1\n");
108 check ("test_one", test_one, -1,
109 "Fatal glibc error: array index " MINUS_1
110 " not less than array length 1\n");
112 check ("test_many", test_many, 5371,
113 "Fatal glibc error: array index 5371"
114 " not less than array length 5371\n");
115 check ("test_many", test_many, 5372,
116 "Fatal glibc error: array index 5372"
117 " not less than array length 5371\n");
118 check ("test_many", test_many, -1,
119 "Fatal glibc error: array index " MINUS_1
120 " not less than array length 5371\n");
122 return 0;
125 #include <support/test-driver.c>