summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Maidanski <ivmai@mail.ru>2023-01-03 12:09:47 +0300
committerIvan Maidanski <ivmai@mail.ru>2023-03-14 11:38:16 +0300
commitb8b3712bce2fbb20dae0d85fed0fd045195b2066 (patch)
tree8dda871a6284d102f832a5969ef3f989ffbe8919
parent116f52abdefd4f58f3fd06209a5cddd1a33d57b5 (diff)
downloadbdwgc-b8b3712bce2fbb20dae0d85fed0fd045195b2066.tar.gz
Fix potential SIGSEGV on out-of-memory in gctest
(a cherry-pick of commit 051f57762 from 'master') * tests/test.c (test_generic_malloc_or_special): Call CHECK_OUT_OF_MEMORY() before GC_get_kind_and_size(). * tests/test.c [DBG_HDRS_ALL] (run_one_test): Set y to fail_proc1 right before its use; call CHECK_OUT_OF_MEMORY() before each GC_size() invocation; call CHECK_OUT_OF_MEMORY() before GC_base().
-rw-r--r--tests/test.c37
1 files changed, 26 insertions, 11 deletions
diff --git a/tests/test.c b/tests/test.c
index 7559717e..7f3f1fbe 100644
--- a/tests/test.c
+++ b/tests/test.c
@@ -685,9 +685,11 @@ void check_marks_int_list(sexpr x)
void test_generic_malloc_or_special(void *p) {
size_t size;
- int kind = GC_get_kind_and_size(p, &size);
+ int kind;
void *p2;
+ CHECK_OUT_OF_MEMORY(p);
+ kind = GC_get_kind_and_size(p, &size);
if (size != GC_size(p)) {
GC_printf("GC_get_kind_and_size returned size not matching GC_size\n");
FAIL;
@@ -1327,9 +1329,8 @@ void * GC_CALLBACK inc_int_counter(void *pcounter)
void run_one_test(void)
{
# ifndef DBG_HDRS_ALL
- char *x;
+ char *x, *y;
char **z;
- char *y = (char *)(GC_word)fail_proc1;
# endif
# ifndef NO_CLOCK
CLOCK_TYPE start_time;
@@ -1350,28 +1351,41 @@ void run_one_test(void)
# endif
test_tinyfl();
# ifndef DBG_HDRS_ALL
- AO_fetch_and_add1(&collectable_count); /* 1 */
- AO_fetch_and_add1(&collectable_count); /* 2 */
- AO_fetch_and_add1(&collectable_count); /* 3 */
- if ((GC_size(GC_malloc(7)) != 8 &&
- GC_size(GC_malloc(7)) != MIN_WORDS * sizeof(GC_word))
- || GC_size(GC_malloc(15)) != 16) {
+ AO_fetch_and_add1(&collectable_count);
+ x = (char*)GC_malloc(7);
+ CHECK_OUT_OF_MEMORY(x);
+ AO_fetch_and_add1(&collectable_count);
+ y = (char*)GC_malloc(7);
+ CHECK_OUT_OF_MEMORY(y);
+ if (GC_size(x) != 8 && GC_size(y) != MIN_WORDS * sizeof(GC_word)) {
GC_printf("GC_size produced unexpected results\n");
FAIL;
}
AO_fetch_and_add1(&collectable_count);
- if (GC_size(GC_malloc(0)) != MIN_WORDS * sizeof(GC_word)) {
+ x = (char*)GC_malloc(15);
+ CHECK_OUT_OF_MEMORY(x);
+ if (GC_size(x) != 16) {
+ GC_printf("GC_size produced unexpected results 2\n");
+ FAIL;
+ }
+ AO_fetch_and_add1(&collectable_count);
+ x = (char*)GC_malloc(0);
+ CHECK_OUT_OF_MEMORY(x);
+ if (GC_size(x) != MIN_WORDS * sizeof(GC_word)) {
GC_printf("GC_malloc(0) failed: GC_size returns %lu\n",
(unsigned long)GC_size(GC_malloc(0)));
FAIL;
}
AO_fetch_and_add1(&uncollectable_count);
- if (GC_size(GC_malloc_uncollectable(0)) != MIN_WORDS * sizeof(GC_word)) {
+ x = (char*)GC_malloc_uncollectable(0);
+ CHECK_OUT_OF_MEMORY(x);
+ if (GC_size(x) != MIN_WORDS * sizeof(GC_word)) {
GC_printf("GC_malloc_uncollectable(0) failed\n");
FAIL;
}
AO_fetch_and_add1(&collectable_count);
x = (char*)GC_malloc(16);
+ CHECK_OUT_OF_MEMORY(x);
if (GC_base(GC_PTR_ADD(x, 13)) != x) {
GC_printf("GC_base(heap ptr) produced incorrect result\n");
FAIL;
@@ -1395,6 +1409,7 @@ void run_one_test(void)
GC_printf("Bad INCR/DECR result\n");
FAIL;
}
+ y = (char *)(GC_word)fail_proc1;
# ifndef PCR
if (GC_base(y) != 0) {
GC_printf("GC_base(fn_ptr) produced incorrect result\n");