summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorIvan Maidanski <ivmai@mail.ru>2023-01-03 12:09:47 +0300
committerIvan Maidanski <ivmai@mail.ru>2023-01-03 13:11:00 +0300
commit051f577622d6fc1de0c96d42785100ad426ddab1 (patch)
tree143988b963d68e7e9ea82d31541799236b4627bd /tests
parentbfefe564225dec355fad9bc34a17de7b8d35a6bb (diff)
downloadbdwgc-051f577622d6fc1de0c96d42785100ad426ddab1.tar.gz
Fix potential SIGSEGV on out-of-memory in gctest
(fix of commits 463d083ff, ff6c3d9fb) * tests/gctest.c (test_generic_malloc_or_special): Call CHECK_OUT_OF_MEMORY() before GC_get_kind_and_size(). * tests/gctest.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().
Diffstat (limited to 'tests')
-rw-r--r--tests/gctest.c37
1 files changed, 26 insertions, 11 deletions
diff --git a/tests/gctest.c b/tests/gctest.c
index 3896a91f..f6e9050e 100644
--- a/tests/gctest.c
+++ b/tests/gctest.c
@@ -754,9 +754,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;
@@ -1430,9 +1432,8 @@ void * GC_CALLBACK set_stackbottom(void *cd)
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;
@@ -1454,28 +1455,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;
@@ -1499,6 +1513,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");