From e6a3bf4c061d60ebaa5b16adf72ba760fe376f09 Mon Sep 17 00:00:00 2001 From: kichikuou Date: Fri, 1 Nov 2019 19:44:50 +0900 Subject: [PATCH] Fix undefined behavior of stack_{push,set} This makes sure that stack_ptr is evaluated / updated after the evaluation of the macro argument. --- vm.h | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/vm.h b/vm.h index 7e4c2d7..e22713b 100644 --- a/vm.h +++ b/vm.h @@ -91,9 +91,19 @@ static inline union vm_value vm_float(float v) bool: vm_bool, \ float: vm_float)(v) +static inline void stack_set_value(int n, union vm_value v) +{ + stack[stack_ptr - (1 + n)] = v; +} + +static inline void stack_push_value(union vm_value v) +{ + stack[stack_ptr++] = v; +} + // Set the Nth value from the top of the stack to V. -#define stack_set(n, v) (stack[stack_ptr - (1 + (n))] = vm_value_cast(v)) -#define stack_push(v) (stack[stack_ptr++] = vm_value_cast(v)) +#define stack_set(n, v) (stack_set_value((n), vm_value_cast(v))) +#define stack_push(v) (stack_push_value(vm_value_cast(v))) union vm_value stack_pop(void); int vm_string_ref(struct string *s);