Fixed out of bounds error in writing section load addresses

This commit is contained in:
Mr-Wiseguy 2023-02-06 22:13:06 -05:00
parent 3f73f99cc6
commit fedeb72451
2 changed files with 7 additions and 5 deletions

View file

@ -1131,6 +1131,8 @@ int main(int argc, char** argv) {
section_load_table += "};\n"; section_load_table += "};\n";
fmt::print(overlay_file, "{}", section_load_table); fmt::print(overlay_file, "{}", section_load_table);
fmt::print(overlay_file, "const size_t num_sections = {};\n", context.sections.size());
} }
return 0; return 0;

View file

@ -4,7 +4,7 @@
#include "recomp.h" #include "recomp.h"
#include "../funcs/recomp_overlays.inl" #include "../funcs/recomp_overlays.inl"
constexpr size_t num_sections = ARRLEN(section_table); constexpr size_t num_code_sections = ARRLEN(section_table);
// SectionTableEntry sections[] defined in recomp_overlays.inl // SectionTableEntry sections[] defined in recomp_overlays.inl
@ -38,12 +38,12 @@ int32_t section_addresses[num_sections];
extern "C" void load_overlays(uint32_t rom, int32_t ram_addr, uint32_t size) { extern "C" void load_overlays(uint32_t rom, int32_t ram_addr, uint32_t size) {
// Search for the first section that's included in the loaded rom range // Search for the first section that's included in the loaded rom range
// Sections were sorted by `init_overlays` so we can use the bounds functions // Sections were sorted by `init_overlays` so we can use the bounds functions
auto lower = std::lower_bound(&section_table[0], &section_table[num_sections], rom, auto lower = std::lower_bound(&section_table[0], &section_table[num_code_sections], rom,
[](const SectionTableEntry& entry, uint32_t addr) { [](const SectionTableEntry& entry, uint32_t addr) {
return entry.rom_addr < addr; return entry.rom_addr < addr;
} }
); );
auto upper = std::upper_bound(&section_table[0], &section_table[num_sections], (uint32_t)(rom + size), auto upper = std::upper_bound(&section_table[0], &section_table[num_code_sections], (uint32_t)(rom + size),
[](uint32_t addr, const SectionTableEntry& entry) { [](uint32_t addr, const SectionTableEntry& entry) {
return addr < entry.size + entry.rom_addr; return addr < entry.size + entry.rom_addr;
} }
@ -87,12 +87,12 @@ extern "C" void unload_overlays(int32_t ram_addr, uint32_t size) {
} }
void init_overlays() { void init_overlays() {
for (size_t section_index = 0; section_index < num_sections; section_index++) { for (size_t section_index = 0; section_index < num_code_sections; section_index++) {
section_addresses[section_index] = section_table[section_index].ram_addr; section_addresses[section_index] = section_table[section_index].ram_addr;
} }
// Sort the executable sections by rom address // Sort the executable sections by rom address
std::sort(&section_table[0], &section_table[num_sections], std::sort(&section_table[0], &section_table[num_code_sections],
[](const SectionTableEntry& a, const SectionTableEntry& b) { [](const SectionTableEntry& a, const SectionTableEntry& b) {
return a.rom_addr < b.rom_addr; return a.rom_addr < b.rom_addr;
} }