diff --git a/src/core/compile.c b/src/core/compile.c index 05c19ba2..8ab0e3a2 100644 --- a/src/core/compile.c +++ b/src/core/compile.c @@ -978,7 +978,14 @@ JanetFuncDef *janetc_pop_funcdef(JanetCompiler *c) { jsm.death_pc = pair.death_pc - scope->bytecode_start; } /* Handle birth_pc == 0 correctly */ - jsm.birth_pc = pair.birth_pc ? pair.birth_pc - scope->bytecode_start : 0; + if ((uint32_t) scope->bytecode_start > pair.birth_pc) { + jsm.birth_pc = 0; + } else { + jsm.birth_pc = pair.birth_pc - scope->bytecode_start; + } + janet_assert(jsm.birth_pc <= jsm.death_pc, "birth pc after death pc"); + janet_assert(jsm.birth_pc < (uint32_t) def->bytecode_length, "bad birth pc"); + janet_assert(jsm.death_pc <= (uint32_t) def->bytecode_length, "bad death pc"); jsm.slot_index = pair.slot.index; jsm.symbol = pair.sym2; janet_v_push(locals, jsm); diff --git a/test/suite0015.janet b/test/suite0015.janet index bb00a9b6..aba27bd9 100644 --- a/test/suite0015.janet +++ b/test/suite0015.janet @@ -47,4 +47,15 @@ (assert (= 10 (do (var x 10) (def y x) (++ x) y)) "no invalid aliasing") +# Crash issue #1174 - bad debug info +(defn crash [] + (debug/stack (fiber/current))) +(do + (math/random) + (defn foo [_] + (crash) + 1) + (foo 0) + 10) + (end-suite)