Skip to content

Commit 4e2fa5b

Browse files
committed
test: clang makes invalid assumptions about malloc and errno
clang's internal malloc failure tests do not set errno, and so they fail to replicate the C library behavior. Skip errno tests when using clang. llvm/llvm-project#114772 Signed-off-by: Keith Packard <[email protected]>
1 parent 25920de commit 4e2fa5b

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

test/malloc.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@
5353
#pragma GCC diagnostic ignored "-Warray-bounds"
5454
#endif
5555

56+
#ifdef __clang__
57+
#define CHECK_ERRNO() 0
58+
#else
59+
#define CHECK_ERRNO() (errno != ENOMEM)
60+
#endif
61+
5662
int
5763
main(void)
5864
{
@@ -119,7 +125,7 @@ main(void)
119125
if (r)
120126
q = malloc(PTRDIFF_MAX);
121127
// printf("malloc(PTRDIFF_MAX: %p %p\n", r, q);
122-
if ((r && q) || errno != ENOMEM) {
128+
if ((r && q) || CHECK_ERRNO()) {
123129
printf("2*malloc(PTRDIFF_MAX) should have failed. got %p,%p error %s\n", r, q, strerror(errno));
124130
result = 1;
125131
}
@@ -129,23 +135,24 @@ main(void)
129135
errno = 0;
130136
r = malloc(SIZE_MAX);
131137
// printf("malloc(SIZE_MAX): %p\n", r);
132-
if (r || errno != ENOMEM) {
138+
if (r || CHECK_ERRNO()) {
133139
printf("malloc(SIZE_MAX) should have failed. got %p error %s\n", r, strerror(errno));
134140
result = 1;
135141
}
136142

137143
errno = 0;
138144
r = calloc(1, SIZE_MAX);
139145
// printf("calloc(1, SIZE_MAX): %p\n", r);
140-
if (r || errno != ENOMEM) {
146+
if (r || CHECK_ERRNO()) {
141147
printf("calloc(1, SIZE_MAX) should have failed. got %p error %s\n", r, strerror(errno));
142148
result = 1;
143149
}
150+
free(r);
144151

145152
errno = 0;
146153
r = reallocarray(NULL, 1, SIZE_MAX);
147154
// printf("reallocarray(NULL, 1, SIZE_MAX): %p\n", r);
148-
if (r || errno != ENOMEM) {
155+
if (r || CHECK_ERRNO()) {
149156
printf("reallocarray(NULL, 1, SIZE_MAX) should have failed. got %p error %s\n", r, strerror(errno));
150157
result = 1;
151158
}
@@ -154,14 +161,14 @@ main(void)
154161
errno = 0;
155162
r = calloc(SIZE_MAX >> pow, SIZE_MAX >> pow);
156163
// printf("calloc(SIZE_MAX >> %d, SIZE_MAX >> %d): %p\n", pow, pow, r);
157-
if (r || errno != ENOMEM) {
164+
if (r || CHECK_ERRNO()) {
158165
printf("calloc(SIZE_MAX >> %d, SIZE_MAX >> %d) should have failed. got %p error %s\n", pow, pow, r, strerror(errno));
159166
result = 1;
160167
}
161168
free(r);
162169
r = reallocarray(NULL, SIZE_MAX >> pow, SIZE_MAX >> pow);
163170
// printf("reallocarray(SIZE_MAX >> %d, SIZE_MAX >> %d): %p\n", pow, pow, r);
164-
if (r || errno != ENOMEM) {
171+
if (r || CHECK_ERRNO()) {
165172
printf("reallocarray(NULL, SIZE_MAX >> %d, SIZE_MAX >> %d) should have failed. got %p error %s\n", pow, pow, r, strerror(errno));
166173
result = 1;
167174
}

0 commit comments

Comments
 (0)