From ff14cda95908b1c475a6640b6a4eaa23e8f7a3a6 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Sat, 5 Nov 2016 21:23:23 -0700 Subject: [PATCH] Abort if malloc fails --- src/runtime/alloc.h | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/runtime/alloc.h b/src/runtime/alloc.h index 55ab15d1..b6ff0902 100644 --- a/src/runtime/alloc.h +++ b/src/runtime/alloc.h @@ -7,6 +7,7 @@ extern "C" { #include #include +#include #if defined(TREE_SITTER_WRAP_MALLOC) @@ -45,19 +46,34 @@ static inline bool ts_toggle_allocation_recording(bool value) { } static inline void *ts_malloc(size_t size) { - return malloc(size); + void *result = malloc(size); + if (size > 0 && !result) { + fprintf(stderr, "tree-sitter failed to allocate %lu bytes", size); + exit(1); + } + return result; } static inline void *ts_calloc(size_t count, size_t size) { - return calloc(count, size); + void *result = calloc(count, size); + if (count > 0 && !result) { + fprintf(stderr, "tree-sitter failed to allocate %lu bytes", count * size); + exit(1); + } + return result; } static inline void *ts_realloc(void *buffer, size_t size) { - return realloc(buffer, size); + void *result = realloc(buffer, size); + if (size > 0 && !result) { + fprintf(stderr, "tree-sitter failed to reallocate %lu bytes", size); + exit(1); + } + return result; } static inline void ts_free(void *buffer) { - return free(buffer); + free(buffer); } #endif