1/* Test reporting of out-of-bounds access for dynamic arrays.
2 Copyright (C) 2017-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
19#include "tst-dynarray-shared.h"
20
21#include <signal.h>
22#include <stdint.h>
23#include <string.h>
24#include <support/capture_subprocess.h>
25#include <support/check.h>
26
27/* Run CALLBACK and check that the data on standard error equals
28 EXPECTED. */
29static void
30check (const char *test, void (*callback) (void *), size_t index,
31 const char *expected)
32{
33 struct support_capture_subprocess result
34 = support_capture_subprocess (callback, closure: &index);
35 if (strcmp (result.err.buffer, expected) != 0)
36 {
37 support_record_failure ();
38 printf (format: "error: test %s (%zu) unexpected standard error data\n"
39 " expected: %s\n"
40 " actual: %s\n",
41 test, index, expected, result.err.buffer);
42 }
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);
48}
49
50/* Try indexing an empty array. */
51static void
52test_empty (void *closure)
53{
54 size_t *pindex = closure;
55 struct dynarray_int dyn;
56 dynarray_int_init (list: &dyn);
57 dynarray_int_at (list: &dyn, index: *pindex);
58}
59
60/* Try indexing a one-element array. */
61static void
62test_one (void *closure)
63{
64 size_t *pindex = closure;
65 struct dynarray_int dyn;
66 dynarray_int_init (list: &dyn);
67 TEST_VERIFY (dynarray_int_resize (&dyn, 1));
68 dynarray_int_at (list: &dyn, index: *pindex);
69}
70
71/* Try indexing a longer array. */
72static void
73test_many (void *closure)
74{
75 size_t *pindex = closure;
76 struct dynarray_int dyn;
77 dynarray_int_init (list: &dyn);
78 TEST_VERIFY (dynarray_int_resize (&dyn, 5371));
79 dynarray_int_at (list: &dyn, index: *pindex);
80}
81
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
90
91static int
92do_test (void)
93{
94 TEST_VERIFY (setenv ("LIBC_FATAL_STDERR_", "1", 1) == 0);
95
96 check (test: "test_empty", callback: test_empty, index: 0,
97 expected: "Fatal glibc error: array index 0 not less than array length 0\n");
98 check (test: "test_empty", callback: test_empty, index: 1,
99 expected: "Fatal glibc error: array index 1 not less than array length 0\n");
100 check (test: "test_empty", callback: test_empty, index: -1,
101 expected: "Fatal glibc error: array index " MINUS_1
102 " not less than array length 0\n");
103
104 check (test: "test_one", callback: test_one, index: 1,
105 expected: "Fatal glibc error: array index 1 not less than array length 1\n");
106 check (test: "test_one", callback: test_one, index: 2,
107 expected: "Fatal glibc error: array index 2 not less than array length 1\n");
108 check (test: "test_one", callback: test_one, index: -1,
109 expected: "Fatal glibc error: array index " MINUS_1
110 " not less than array length 1\n");
111
112 check (test: "test_many", callback: test_many, index: 5371,
113 expected: "Fatal glibc error: array index 5371"
114 " not less than array length 5371\n");
115 check (test: "test_many", callback: test_many, index: 5372,
116 expected: "Fatal glibc error: array index 5372"
117 " not less than array length 5371\n");
118 check (test: "test_many", callback: test_many, index: -1,
119 expected: "Fatal glibc error: array index " MINUS_1
120 " not less than array length 5371\n");
121
122 return 0;
123}
124
125#include <support/test-driver.c>
126

source code of glibc/malloc/tst-dynarray-at-fail.c