From 9c6d1b3060bda68570e4a0c80412ba8df4deaeb2 Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Tue, 9 Feb 2016 23:35:33 +0300 Subject: [PATCH] Fix null pointer dereference on out-of-memory in tests * tests/disclaim_bench.c (main): Check GC_MALLOC result for NULL (abort in case of out-of-memory) before dereference. * tests/test.c (run_one_test): Likewise. * tests/disclaim_test.c (pair_new): Move is_pair call (in my_assert) down to be after GC_finalized_malloc result check for NULL. --- tests/disclaim_bench.c | 9 ++++++--- tests/disclaim_test.c | 2 +- tests/test.c | 1 + 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/disclaim_bench.c b/tests/disclaim_bench.c index f9bfc580..2984e8f4 100644 --- a/tests/disclaim_bench.c +++ b/tests/disclaim_bench.c @@ -92,9 +92,6 @@ int main(int argc, char **argv) GC_INIT(); GC_init_finalized_malloc(); - - keep_arr = GC_MALLOC(sizeof(void *)*KEEP_CNT); - if (argc == 2 && strcmp(argv[1], "--help") == 0) { fprintf(stderr, "Usage: %s [FINALIZATION_MODEL]\n" @@ -113,6 +110,12 @@ int main(int argc, char **argv) model_max = 2; } + keep_arr = GC_MALLOC(sizeof(void *) * KEEP_CNT); + if (NULL == keep_arr) { + fprintf(stderr, "Out of memory!\n"); + exit(3); + } + printf("\t\t\tfin. ratio time/s time/fin.\n"); for (model = model_min; model <= model_max; ++model) { double t = 0.0; diff --git a/tests/disclaim_test.c b/tests/disclaim_test.c index 659bf106..47d5144d 100644 --- a/tests/disclaim_test.c +++ b/tests/disclaim_test.c @@ -120,11 +120,11 @@ pair_new(pair_t car, pair_t cdr) static const struct GC_finalizer_closure fc = { pair_dct, NULL }; p = GC_finalized_malloc(sizeof(struct pair_s), &fc); - my_assert(!is_pair(p)); if (p == NULL) { fprintf(stderr, "Out of memory!\n"); exit(3); } + my_assert(!is_pair(p)); my_assert(memeq(p, 0, sizeof(struct pair_s))); memcpy(p->magic, pair_magic, sizeof(p->magic)); p->checksum = 782 + (car? car->checksum : 0) + (cdr? cdr->checksum : 0); diff --git a/tests/test.c b/tests/test.c index a8604b84..a09c4ad5 100644 --- a/tests/test.c +++ b/tests/test.c @@ -1244,6 +1244,7 @@ void run_one_test(void) FAIL; } z = GC_malloc(8); + CHECK_OUT_OF_MEMORY(z); GC_PTR_STORE(z, x); if (*z != x) { GC_printf("GC_PTR_STORE failed: %p != %p\n", (void *)(*z), (void *)x); -- 2.40.0