From c10cbbff34b25f447d45d1a997cf8606769875c8 Mon Sep 17 00:00:00 2001 From: Gaelan Steele Date: Tue, 28 Apr 2020 13:45:53 -0700 Subject: [PATCH] Remove undefined behavior caught by LLVM 10 UBSAN. --- src/exec_stack.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/exec_stack.h b/src/exec_stack.h index 57e136507d..2a063e8cf9 100644 --- a/src/exec_stack.h +++ b/src/exec_stack.h @@ -61,8 +61,10 @@ static void stack_init(struct stack* s) { static void stack_reset(struct stack* s) { assert(s->limit == 0 && "stack freed while not empty"); - char* mem_start = s->mem_end - ( -s->bound + ALIGNMENT); - free(mem_start); + if(s->mem_end != NULL) { + char* mem_start = s->mem_end - ( -s->bound + ALIGNMENT); + free(mem_start); + } stack_init(s); } @@ -80,7 +82,7 @@ static stack_ptr* stack_block_next(struct stack* s, stack_ptr p) { static void stack_reallocate(struct stack* s, size_t sz) { int old_mem_length = -(s->bound) + ALIGNMENT; - char* old_mem_start = s->mem_end - old_mem_length; + char* old_mem_start = (s->mem_end != NULL) ? (s->mem_end - old_mem_length) : NULL; int new_mem_length = align_round_up((old_mem_length + sz + 256) * 2); char* new_mem_start = jv_mem_realloc(old_mem_start, new_mem_length);