From 16c65de934230e86bafefb247637e5535ef6e3f6 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Mon, 7 Oct 2013 17:22:53 +0200 Subject: [PATCH] fixed segmention fault abort allocation after first malloc fail --- newlib/examples/memtest.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/newlib/examples/memtest.c b/newlib/examples/memtest.c index 6950e6e4..7501a92d 100644 --- a/newlib/examples/memtest.c +++ b/newlib/examples/memtest.c @@ -62,16 +62,20 @@ int main(int argc, char** argv) int i; for (i = 0; i < chunks; i++) { test[i] = malloc(size); - if (!test) - printf("malloc(%d)\tFAILED!\n", chunks * sizeof(void *)); - else + if (test[i]) printf("malloc(%d)\tCHUNK: %d START: %p END: %p\n", size, i, test[i], test[i] + size); + else { + printf("malloc(%d)\tFAILED! Abort allocation, start with freeing memory\n", size); + break; + } } // and release again for (i = 0; i < chunks; i++) { - free(test[i]); - printf("free(%p)\tCHUNK: %d\n", test[i], i); + if (test[i]) { + free(test[i]); + printf("free(%p)\tCHUNK: %d\n", test[i], i); + } } free(test);