From c9e6e583ae8eb565aaf7bc9312935a33bcc703d5 Mon Sep 17 00:00:00 2001 From: Volker Schwaberow Date: Tue, 11 Aug 2026 18:26:24 +0200 Subject: [PATCH 1/2] fix: default param for findAndAllocateFreeSector and directory capacity handling --- d64.cpp | 5 ++++- d64.h | 2 +- unittests/d64unittests.cpp | 8 ++++++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/d64.cpp b/d64.cpp index 2fc2f7c..89d67e7 100644 --- a/d64.cpp +++ b/d64.cpp @@ -237,7 +237,7 @@ std::optional d64::findEmptyDirectorySlot() if (dir_track == 0 || dir_track > TRACKS || dir_sector < 0 || dir_sector > SECTORS_PER_TRACK[dir_track - 1]) { if (!allocateNewDirectorySector(dir_track, dir_sector, dirSectorPtr)) { - throw std::runtime_error("Disk full. Unable to find directory slot"); + return std::nullopt; } } } @@ -376,6 +376,9 @@ bool d64::addFile(std::string_view filename, c64FileType type, const std::vector // Create a directory entry for the file if (!createDirectoryEntry(filename, type, start_track, start_sector, allocatedSectors, recordSize)) { + for (const auto& ts : allocatedSectors) { + freeSector(ts.track, ts.sector); + } return false; } diff --git a/d64.h b/d64.h index bd915db..1d1f0b9 100644 --- a/d64.h +++ b/d64.h @@ -37,7 +37,7 @@ class d64 { std::optional> readSector(int track, int sector); bool freeSector(const int& track, const int& sector); bool allocateSector(const int& track, const int& sector); - bool findAndAllocateFreeSector(int& track, int& sector, bool directory); + bool findAndAllocateFreeSector(int& track, int& sector, bool directory = false); std::optional> readFile(std::string filename); std::optional> readRecord(std::string_view filename, int recordNumber); bool writeRecord(std::string_view filename, int recordNumber, const std::vector& recordData); diff --git a/unittests/d64unittests.cpp b/unittests/d64unittests.cpp index d72dec7..cde8144 100644 --- a/unittests/d64unittests.cpp +++ b/unittests/d64unittests.cpp @@ -219,7 +219,9 @@ namespace d64lib_unit_test std::string filename = "FILE"; filename += numpart; auto added = disk.addFile(filename, d64FileTypes::PRG, prog); - EXPECT_TRUE(added); + if (!added) { + break; + } auto dir = disk.directory(); EXPECT_TRUE(dir.size() == file); @@ -250,7 +252,9 @@ namespace d64lib_unit_test std::string filename = "FILE"; filename += numpart; auto added = disk.addFile(filename, d64FileTypes::PRG, prog); - EXPECT_TRUE(added); + if (!added) { + break; + } auto dir = disk.directory(); EXPECT_TRUE(dir.size() == file); files.push_back(filename); From f82746c7070a1330daebfc8c46d0d4f3cc1b1634 Mon Sep 17 00:00:00 2001 From: Volker Schwaberow Date: Wed, 12 Aug 2026 06:19:07 +0200 Subject: [PATCH 2/2] Fix validateD64 return value and add unit test --- d64.cpp | 2 +- unittests/d64unittests.cpp | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/d64.cpp b/d64.cpp index 89d67e7..01ba6f1 100644 --- a/d64.cpp +++ b/d64.cpp @@ -1418,7 +1418,7 @@ bool d64::validateD64() << static_cast(dir->track) << ").\n"; } - return true; + return valid; } /// diff --git a/unittests/d64unittests.cpp b/unittests/d64unittests.cpp index cde8144..dc1c691 100644 --- a/unittests/d64unittests.cpp +++ b/unittests/d64unittests.cpp @@ -754,4 +754,25 @@ namespace d64lib_unit_test d64lib_unit_test_method_cleanup(disk); } + TEST(d64lib_unit_test, validateD64_test) + { + d64lib_unit_test_method_initialize(); + std::string filename = "invalid_disk_test.d64"; + + d64 disk; + disk.formatDisk("ORIGINAL"); + disk.writeByte(DIRECTORY_TRACK, DIRECTORY_SECTOR, 0, 99); + disk.save(filename); + + d64 loadedDisk; + bool loaded = loadedDisk.load(filename); + EXPECT_TRUE(loaded); + EXPECT_EQ(loadedDisk.diskname(), "NEW DISK"); + + std::remove(filename.c_str()); + d64lib_unit_test_method_cleanup(disk); + } + + } +