summaryrefslogtreecommitdiffhomepage
path: root/vm.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2021-07-08 08:59:41 +0200
committerJo-Philipp Wich <jo@mein.io>2021-07-11 15:49:14 +0200
commit6bcc318de336e1cb3264fb306c8ce132b8703ebc (patch)
tree47979c0169bf224a6e88b26028a98ed21828010f /vm.c
parent4ae056867d96b1795fec7f4cfd0f68b124e398cd (diff)
vm: fix handling exceptions in top-level function calls
When a toplevel function call raises an exception, the call stack will be empty when invoking the exception handler, ensure to handle this case appropriately. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'vm.c')
-rw-r--r--vm.c9
1 files changed, 6 insertions, 3 deletions
diff --git a/vm.c b/vm.c
index f4c56db..9997580 100644
--- a/vm.c
+++ b/vm.c
@@ -730,12 +730,15 @@ uc_vm_exception_new(uc_vm *vm, uc_exception_type_t type, const char *message, uc
static bool
uc_vm_handle_exception(uc_vm *vm)
{
- uc_callframe *frame = uc_vm_current_frame(vm);
+ uc_callframe *frame = NULL;
uc_chunk *chunk = NULL;
uc_value_t *exo;
size_t i, pos;
- if (!frame->closure)
+ if (vm->callframes.count)
+ frame = uc_vm_current_frame(vm);
+
+ if (!frame || !frame->closure)
return false;
chunk = uc_vm_frame_chunk(frame);
@@ -2270,7 +2273,7 @@ uc_vm_execute_chunk(uc_vm *vm, uc_value_t **retvalp)
/* walk up callframes until something handles the exception or the root is reached */
while (!uc_vm_handle_exception(vm)) {
/* no further callframe to pop, report unhandled exception and terminate */
- if (vm->callframes.count == 1) {
+ if (vm->callframes.count <= 1) {
uc_vm_output_exception(vm);
return ERROR_RUNTIME;