From 3cd7e0da00f2a65e544d8f8bb8a42edf822bb5a2 Mon Sep 17 00:00:00 2001 From: itchyny Date: Fri, 6 Mar 2026 15:32:29 +0900 Subject: [PATCH] Fix crash when importing a module with errors twice (#3497) When a module fails to parse as a library, `load_library` cached the invalid block. A second import/include of the same module would find the cached invalid block and pass it to `block_bind_library`, triggering the assertion failure. Skip caching the block when there are parse errors to avoid this problem. --- src/linker.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/linker.c b/src/linker.c index 9ef75493..cfd74d1d 100644 --- a/src/linker.c +++ b/src/linker.c @@ -366,11 +366,13 @@ static int load_library(jq_state *jq, jv lib_path, int is_data, int raw, int opt program = block_bind_self(program, OP_IS_CALL_PSEUDO); } } - state_idx = lib_state->ct++; - lib_state->names = jv_mem_realloc(lib_state->names, lib_state->ct * sizeof(const char *)); - lib_state->defs = jv_mem_realloc(lib_state->defs, lib_state->ct * sizeof(block)); - lib_state->names[state_idx] = strdup(jv_string_value(lib_path)); - lib_state->defs[state_idx] = program; + if (nerrors == 0) { + state_idx = lib_state->ct++; + lib_state->names = jv_mem_realloc(lib_state->names, lib_state->ct * sizeof(const char *)); + lib_state->defs = jv_mem_realloc(lib_state->defs, lib_state->ct * sizeof(block)); + lib_state->names[state_idx] = strdup(jv_string_value(lib_path)); + lib_state->defs[state_idx] = program; + } out: *out_block = program; jv_free(lib_path);