From 04d984d4fcc1ea8fa3a34299ad021cad2c060374 Mon Sep 17 00:00:00 2001 From: Rodrigo Arias Date: Fri, 25 Oct 2024 11:10:10 +0200 Subject: [PATCH] Make cpu index check more strict --- src/emu/loom.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/emu/loom.c b/src/emu/loom.c index 62b1a38..4f8c8d2 100644 --- a/src/emu/loom.c +++ b/src/emu/loom.c @@ -97,6 +97,11 @@ load_cpus(struct loom *loom, JSON_Object *meta) int index = (int) json_object_get_number(jcpu, "index"); int phyid = (int) json_object_get_number(jcpu, "phyid"); + if (index < 0 || index >= (int) ncpus) { + err("cpu index %d out of bounds", index); + return -1; + } + struct cpu *cpu = loom_find_cpu(loom, phyid); if (cpu) { @@ -110,6 +115,14 @@ load_cpus(struct loom *loom, JSON_Object *meta) continue; } + /* If we reach this point, there shouldn't be a CPU with the + * same index either, as otherwise the phyid should have matched + * before. So it is an error. */ + if (loom_get_cpu(loom, index) != NULL) { + err("cpu index %d redefined with another phyid", index); + return -1; + } + cpu = calloc(1, sizeof(struct cpu)); if (cpu == NULL) { err("calloc failed:");