Fix panic on CREATE VIRTUAL TABLE IF NOT EXISTS by halting VM properly

Fixes a runtime panic caused by failing to halt the virtual machine
after executing CREATE VIRTUAL TABLE IF NOT EXISTS.

Previously resulted in:
thread 'main' panicked at core/vdbe/mod.rs:408:52:
index out of bounds: the len is 3 but the index is 3
This commit is contained in:
Piotr Rzysko
2025-05-11 21:19:26 +02:00
parent fdffbc9534
commit d5984445a9
2 changed files with 12 additions and 0 deletions

View File

@@ -484,10 +484,12 @@ pub fn translate_create_virtual_table(
approx_num_labels: 1, approx_num_labels: 1,
}); });
let init_label = program.emit_init(); let init_label = program.emit_init();
let start_offset = program.offset();
program.emit_halt(); program.emit_halt();
program.preassign_label_to_next_insn(init_label); program.preassign_label_to_next_insn(init_label);
program.emit_transaction(true); program.emit_transaction(true);
program.emit_constant_insns(); program.emit_constant_insns();
program.emit_goto(start_offset);
return Ok(program); return Ok(program);
} }
bail_parse_error!("Table {} already exists", tbl_name); bail_parse_error!("Table {} already exists", tbl_name);

View File

@@ -593,6 +593,11 @@ def test_create_virtual_table():
lambda res: "× Parse error: Table t1 already exists" == res, lambda res: "× Parse error: Table t1 already exists" == res,
"create virtual table fails if virtual table with the same name already exists", "create virtual table fails if virtual table with the same name already exists",
) )
limbo.run_test_fn(
"CREATE VIRTUAL TABLE IF NOT EXISTS t1 USING kv_store;",
null,
"create virtual table with IF NOT EXISTS succeeds",
)
limbo.debug_print("CREATE TABLE t2 (col INTEGER);") limbo.debug_print("CREATE TABLE t2 (col INTEGER);")
limbo.run_test_fn( limbo.run_test_fn(
@@ -600,6 +605,11 @@ def test_create_virtual_table():
lambda res: "× Parse error: Table t2 already exists" == res, lambda res: "× Parse error: Table t2 already exists" == res,
"create virtual table fails if regular table with the same name already exists", "create virtual table fails if regular table with the same name already exists",
) )
limbo.run_test_fn(
"CREATE VIRTUAL TABLE IF NOT EXISTS t2 USING kv_store;",
null,
"create virtual table with IF NOT EXISTS succeeds",
)
limbo.debug_print("CREATE VIRTUAL TABLE t3 USING kv_store;") limbo.debug_print("CREATE VIRTUAL TABLE t3 USING kv_store;")
limbo.run_test_fn( limbo.run_test_fn(