From fedf5a44a68726919298d20ddbdfc9e5068c6fc3 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Thu, 17 Aug 2017 15:20:49 -0400 Subject: [PATCH 1/4] Imposes a maximum track length. --- Storage/Disk/Encodings/MFM.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/Storage/Disk/Encodings/MFM.cpp b/Storage/Disk/Encodings/MFM.cpp index 3808baff2..dfe9330c7 100644 --- a/Storage/Disk/Encodings/MFM.cpp +++ b/Storage/Disk/Encodings/MFM.cpp @@ -173,6 +173,7 @@ template std::shared_ptr } while(segment.data.size() < expected_track_bytes) shifter.add_byte(0x00); + if(segment.data.size() > expected_track_bytes) segment.data.resize(expected_track_bytes); segment.number_of_bits = (unsigned int)(segment.data.size() * 8); return std::shared_ptr(new Storage::Disk::PCMTrack(std::move(segment))); From 733ee5a5c37285c95654692f35596627aebf3311 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Thu, 17 Aug 2017 15:30:02 -0400 Subject: [PATCH 2/4] Ensured no attempt to put a null track into the cache --- Storage/Disk/Disk.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/Storage/Disk/Disk.cpp b/Storage/Disk/Disk.cpp index 5844e1bea..ffd714385 100644 --- a/Storage/Disk/Disk.cpp +++ b/Storage/Disk/Disk.cpp @@ -37,6 +37,7 @@ std::shared_ptr Disk::get_track_at_position(unsigned int head, unsigned i std::lock_guard lock_guard(file_access_mutex_); std::shared_ptr track = get_uncached_track_at_position(head, position); + if(!track) return nullptr; cached_tracks_[address] = track; return track; } From 4f8b89772e4649174a4a3bb61dec17394796b188 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Thu, 17 Aug 2017 15:31:53 -0400 Subject: [PATCH 3/4] Improved logic for detecting when all sense has been derived from a track to spot any repeated track, not necessarily the first one. That avoids sectors that run over the index hold and obscure the first throwing things. --- Storage/Disk/Encodings/MFM.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Storage/Disk/Encodings/MFM.cpp b/Storage/Disk/Encodings/MFM.cpp index dfe9330c7..ccace455a 100644 --- a/Storage/Disk/Encodings/MFM.cpp +++ b/Storage/Disk/Encodings/MFM.cpp @@ -11,6 +11,8 @@ #include "../PCMTrack.hpp" #include "../../../NumberTheory/CRC.hpp" +#include + using namespace Storage::Encodings::MFM; class MFMEncoder: public Encoder { @@ -274,13 +276,20 @@ std::shared_ptr Parser::get_sector(uint8_t head, uint8_t track, uint8_t seek_to_track(track); int track_index = get_index(head, track, 0); - // Populate the sector cache if it's not already populated. + // Populate the sector cache if it's not already populated by asking for sectors unless and until + // one is returned that has already been seen. if(decoded_tracks_.find(track_index) == decoded_tracks_.end()) { std::shared_ptr first_sector = get_next_sector(); + std::set visited_sectors; if(first_sector) { while(1) { std::shared_ptr next_sector = get_next_sector(); - if(!next_sector || next_sector->sector == first_sector->sector) break; + if(next_sector) { + if(visited_sectors.find(next_sector->sector) != visited_sectors.end()) { + break; + } + visited_sectors.insert(next_sector->sector); + } } } decoded_tracks_.insert(track_index); From 2c2dd8073c10d6663faa8f67de3ba14dbda51084 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Thu, 17 Aug 2017 15:32:24 -0400 Subject: [PATCH 4/4] Modified to return nullptr if asked for an extended disk image track that doesn't exist. --- Storage/Disk/Formats/CPCDSK.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Storage/Disk/Formats/CPCDSK.cpp b/Storage/Disk/Formats/CPCDSK.cpp index ba57c539b..b67d5a007 100644 --- a/Storage/Disk/Formats/CPCDSK.cpp +++ b/Storage/Disk/Formats/CPCDSK.cpp @@ -57,8 +57,14 @@ std::shared_ptr CPCDSK::get_uncached_track_at_position(unsigned int head, // All DSK images reserve 0x100 bytes for their headers. long file_offset = 0x100; if(is_extended_) { - // Tracks are a variable size in the original DSK file format; sum the lengths - // of all tracks prior to the interesting one to get a file offset. + // Tracks are a variable size in the original DSK file format. + + // Check that there is anything stored for this track. + if(!track_sizes_[chronological_track]) { + return nullptr; + } + + // Sum the lengths of all tracks prior to the interesting one to get a file offset. unsigned int t = 0; while(t < chronological_track && t < track_sizes_.size()) { file_offset += track_sizes_[t];