diff --git a/src/vm/fzl_vm.c b/src/vm/fzl_vm.c index ce026e1..f39b1cd 100644 --- a/src/vm/fzl_vm.c +++ b/src/vm/fzl_vm.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -13,6 +14,11 @@ #include char last_err[256]; +size_t last_jmp_ip = 0; + +// NOTE: add `--max-iter` option for limit +size_t max_iter_limit = 100000; +size_t total_jmp_on_same = 0; static char *expect_hdr = "FZL\0\1"; @@ -667,7 +673,22 @@ int fzl_vm_run(fzl_vm_t *vm) return -1; } uint64_t jmp_ip = vm->ops[++vm->ip]; - vm->ip = jmp_ip - 1; + if (jmp_ip == last_jmp_ip) + total_jmp_on_same++; + else + total_jmp_on_same = 0; + + if (total_jmp_on_same > max_iter_limit) + { + snprintf(last_err, sizeof(last_err), + "runtime error - exceeded max loop jumps (infinite loop?)\n" + "If you thinks there's mistake, change jumps limit with " + "--max-iter=N"); + return -1; + } + + vm->ip = jmp_ip - 1; + last_jmp_ip = jmp_ip; break; }