From 53b327151c0f1ad00b8ce33dabcc8c575c6e7dd8 Mon Sep 17 00:00:00 2001 From: pengyaozong Date: Mon, 15 Nov 2021 11:23:09 +0800 Subject: [PATCH] Fix function last line do not saved in pc2line table issue pc2line table only saves the diff between lines, so the function end next line is needed to generate the function's last line diff, the previous code missed it. This patch fixed it by change the line number generate logic a bit. This bug is found on https://github.com/koush/quickjs, an unofficial debugger implementation, push it to the upstream. Signed-off-by: pengyaozong --- quickjs.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/quickjs.c b/quickjs.c index 48aeffc..0ac8d1f 100644 --- a/quickjs.c +++ b/quickjs.c @@ -30831,7 +30831,7 @@ static __exception int resolve_variables(JSContext *ctx, JSFunctionDef *s) next: ; } - line_num = 0; /* avoid warning */ + line_num = s->line_num; /* init with function start line num */ for (pos = 0; pos < bc_len; pos = pos_next) { op = bc_buf[pos]; len = opcode_info[op].size; @@ -30967,9 +30967,18 @@ static __exception int resolve_variables(JSContext *ctx, JSFunctionDef *s) /* remove dead code */ int line = -1; dbuf_put(&bc_out, bc_buf + pos, len); - pos = skip_dead_code(s, bc_buf, bc_len, pos + len, &line); + if(pos + len < bc_len) + /* have dead code, remove it */ + pos = skip_dead_code(s, bc_buf, bc_len, pos + len, &line); + else { + /* already arrive the function end */ + pos += len; + line = line_num + 1; /* line_num is inited with function start line, line_num + 1 goes outside */ + } + pos_next = pos; - if (pos < bc_len && line >= 0 && line_num != line) { + /* use <= to allow save the function next line num */ + if (pos <= bc_len && line >= 0 && line_num != line) { line_num = line; s->line_number_size++; dbuf_putc(&bc_out, OP_line_num);