diff --git a/core/device_table.c b/core/device_table.c index ed0c749..832b0de 100644 --- a/core/device_table.c +++ b/core/device_table.c @@ -75,6 +75,8 @@ int eos_device_table_validate(const eos_device_table_t *table) if (!table) return EOS_ERR_INVALID; if (table->magic != EOS_DEVTABLE_MAGIC) return EOS_ERR_INVALID; if (table->version != EOS_DEVTABLE_VERSION) return EOS_ERR_VERSION; + if (table->mem_region_count > EOS_MAX_MEM_REGIONS) return EOS_ERR_INVALID; + if (table->periph_count > EOS_MAX_PERIPHERALS) return EOS_ERR_INVALID; uint32_t expected = compute_table_crc(table); if (table->crc32 != expected) return EOS_ERR_CRC; diff --git a/tests/unit/test_device_table.c b/tests/unit/test_device_table.c index 6ae0d82..01a39cd 100644 --- a/tests/unit/test_device_table.c +++ b/tests/unit/test_device_table.c @@ -116,6 +116,22 @@ TEST(test_corrupt_crc_fails) ASSERT(eos_device_table_validate(&table) == EOS_ERR_CRC); } +TEST(test_oversized_counts_fail) +{ + eos_device_table_t table; + eos_device_table_init(&table, "Oversized"); + table.mem_region_count = EOS_MAX_MEM_REGIONS + 1; + eos_device_table_finalize(&table); + + ASSERT(eos_device_table_validate(&table) == EOS_ERR_INVALID); + + table.mem_region_count = 0; + table.periph_count = EOS_MAX_PERIPHERALS + 1; + eos_device_table_finalize(&table); + + ASSERT(eos_device_table_validate(&table) == EOS_ERR_INVALID); +} + int main(void) { printf("=== eBootloader: Device Table Unit Tests ===\n\n"); @@ -127,8 +143,9 @@ int main(void) run_test_memory_overflow(); run_test_finalize_and_validate(); run_test_corrupt_crc_fails(); + run_test_oversized_counts_fail(); - tests_run = 7; + tests_run = 8; printf("\n%d/%d tests passed\n", tests_passed, tests_run); return (tests_passed == tests_run) ? 0 : 1; }