1
0
mirror of https://github.com/TomHarte/CLK.git synced 2025-01-10 00:29:40 +00:00

Avoid risk of unbounded memory consumption.

This commit is contained in:
Thomas Harte 2024-12-16 22:29:23 -05:00
parent 5acdf39566
commit 15583e7975

View File

@ -181,13 +181,21 @@ std::vector<File> Analyser::Static::Commodore::GetFiles(const std::shared_ptr<St
uint8_t next_track = 18;
uint8_t next_sector = 1;
directory.reserve(20 * 1024); // Probably more than plenty.
std::set<std::pair<uint8_t, uint8_t>> visited;
while(true) {
// Don't be fooled by disks that are encoded with a looping directory.
const auto key = std::make_pair(next_track, next_sector);
if(visited.find(key) != visited.end()) break;
visited.insert(key);
// Append sector to directory and follow next link.
const auto sector = parser.sector(next_track, next_sector);
if(!sector) break;
directory.insert(directory.end(), sector->data.begin(), sector->data.end());
next_track = sector->data[0];
next_sector = sector->data[1];
// Check for end-of-directory.
if(!next_track) break;
}