From 8dca3ef10d01fe116d34b2c4768b08e678d180b0 Mon Sep 17 00:00:00 2001 From: Stephen Dolan Date: Tue, 18 Dec 2012 17:01:23 +0000 Subject: [PATCH] Print an error message and abort in out-of-memory situations. Closes #43. Tested with: ulimit -v 5000 ./jq -n -c 'def f(x): x,f([x,x]); f(0)' --- jv_alloc.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/jv_alloc.c b/jv_alloc.c index 3e1c8fe..320f16e 100644 --- a/jv_alloc.c +++ b/jv_alloc.c @@ -1,9 +1,18 @@ #include +#include #include "jv_alloc.h" +static void memory_exhausted() { + fprintf(stderr, "error: cannot allocate memory\n"); + abort(); +} void* jv_mem_alloc(size_t sz) { - return malloc(sz); + void* p = malloc(sz); + if (!p) { + memory_exhausted(); + } + return p; } void jv_mem_free(void* p) { @@ -11,5 +20,9 @@ void jv_mem_free(void* p) { } void* jv_mem_realloc(void* p, size_t sz) { - return realloc(p, sz); + p = realloc(p, sz); + if (!p) { + memory_exhausted(); + } + return p; } -- 2.40.0