From 03ac433d4958b6914b0107d71f41ba189e567bc5 Mon Sep 17 00:00:00 2001 From: aarrbba123 <70095196+aarrbba123@users.noreply.github.com> Date: Sun, 24 Sep 2023 04:36:05 +0800 Subject: [PATCH 1/5] Fix Heap Corruption Issue --- .gitignore | 2 ++ CMakeLists.txt | 2 +- src/core/FatSystem.cpp | 31 ++++++++++++++++++++----------- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 0803e65..4d9e2fc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ build **.swp test +*.bak +.vscode \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index d6a2649..1fedd50 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 2.8) +cmake_minimum_required(VERSION 2.8.12) project(fatcat) OPTION(DEFINE_WIN "Compiling windows" OFF) diff --git a/src/core/FatSystem.cpp b/src/core/FatSystem.cpp index 79cda8c..ce74c42 100644 --- a/src/core/FatSystem.cpp +++ b/src/core/FatSystem.cpp @@ -156,11 +156,11 @@ void FatSystem::parseHeader() totalSectors = FAT_READ_LONG(buffer, FAT_TOTAL_SECTORS)&0xffffffff; } - unsigned int rootDirSectors = + unsigned long long int rootDirSectors = (rootEntries*FAT_ENTRY_SIZE + bytesPerSector-1)/bytesPerSector; - unsigned long dataSectors = totalSectors - + unsigned long long dataSectors = totalSectors - (reservedSectors + fats*sectorsPerFat + rootDirSectors); - unsigned long totalClusters = dataSectors/sectorsPerCluster; + unsigned long long totalClusters = dataSectors/sectorsPerCluster; bits = (totalClusters > MAX_FAT12) ? 16 : 12; } else { type = FAT32; @@ -212,10 +212,15 @@ unsigned int FatSystem::nextCluster(unsigned int cluster, int fat) } if (cacheEnabled) { + cout << "[Debug] Using cache!" << endl; return cache[cluster]; } + #ifdef __WIN__ + readData(fatStart+fatSize*fat+(bits*cluster)/8, buffer, sizeof(*buffer)); + #else readData(fatStart+fatSize*fat+(bits*cluster)/8, buffer, sizeof(buffer)); + #endif unsigned int next; @@ -250,11 +255,11 @@ unsigned int FatSystem::nextCluster(unsigned int cluster, int fat) } } #ifdef __WIN__ -} -__finally -{ - delete[] buffer; -} + } + __finally + { + delete[] buffer; + } #endif } @@ -290,7 +295,7 @@ bool FatSystem::writeNextCluster(unsigned int cluster, unsigned int next, int fa buffer[1] = (buffer[1]&0xf0)|((next>>8)&0x0f); } } else { - for (int i=0; i<(bits/8); i++) { + for (int i=0; i < (bits/8); i++) { buffer[i] = (next>>(8*i))&0xff; } } @@ -737,7 +742,7 @@ void FatSystem::infos() printf("Data start address: %016llx\n", dataStart); cout << "Root directory cluster: " << rootDirectory << endl; cout << "Disk label: " << diskLabel << endl; - cout << endl; + cout << endl; computeStats(); cout << "Free clusters: " << freeClusters << "/" << totalClusters; @@ -847,7 +852,11 @@ FatEntry FatSystem::rootEntry() bool FatSystem::freeCluster(unsigned int cluster) { - return nextCluster(cluster) == 0; + __try { + return nextCluster(cluster) == 0; + } __finally { + cout << "[Debug] Yeah This part works lol" << endl; + } } void FatSystem::computeStats() From b2e9ee2b72287d3944063acf3fb9244be6d84b77 Mon Sep 17 00:00:00 2001 From: aarrbba123 <70095196+aarrbba123@users.noreply.github.com> Date: Sun, 24 Sep 2023 05:22:35 +0800 Subject: [PATCH 2/5] Remove Debug, Continue Fixing Heap Corruption --- src/core/FatSystem.cpp | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/core/FatSystem.cpp b/src/core/FatSystem.cpp index ce74c42..6878a3f 100644 --- a/src/core/FatSystem.cpp +++ b/src/core/FatSystem.cpp @@ -212,7 +212,6 @@ unsigned int FatSystem::nextCluster(unsigned int cluster, int fat) } if (cacheEnabled) { - cout << "[Debug] Using cache!" << endl; return cache[cluster]; } @@ -605,13 +604,13 @@ void FatSystem::readFile(unsigned int cluster, unsigned int size, FILE *f, bool int toRead = size; if (toRead > bytesPerCluster || size < 0) { toRead = bytesPerCluster; - } + } #ifndef __WIN__ - char buffer[bytesPerCluster]; + char buffer[bytesPerCluster]; #else - char *buffer = new char[bytesPerCluster]; - __try - { + char *buffer = new char[bytesPerCluster]; + __try + { #endif readData(clusterAddress(cluster), buffer, toRead); @@ -852,11 +851,7 @@ FatEntry FatSystem::rootEntry() bool FatSystem::freeCluster(unsigned int cluster) { - __try { - return nextCluster(cluster) == 0; - } __finally { - cout << "[Debug] Yeah This part works lol" << endl; - } + return nextCluster(cluster) == 0; } void FatSystem::computeStats() @@ -895,7 +890,11 @@ void FatSystem::rewriteUnallocated(bool random) buffer[i] = 0x0; } } + #ifndef __WIN__ writeData(clusterAddress(cluster), buffer, sizeof(buffer)); + #else + writeData(clusterAddress(cluster), buffer, sizeof(*buffer)); + #endif total++; #ifdef __WIN__ } From d7c6be27c995fac63dd2ce4578aa666ddc3f9bf9 Mon Sep 17 00:00:00 2001 From: aarrbba123 <70095196+aarrbba123@users.noreply.github.com> Date: Sun, 24 Sep 2023 07:19:41 +0800 Subject: [PATCH 3/5] Final Fix --- src/core/FatSystem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/FatSystem.cpp b/src/core/FatSystem.cpp index 6878a3f..8470c53 100644 --- a/src/core/FatSystem.cpp +++ b/src/core/FatSystem.cpp @@ -102,7 +102,6 @@ int FatSystem::readData(unsigned long long address, char *buffer, int size) size -= n; } } while ((size>0) && (n>0)); - return n; } @@ -240,6 +239,7 @@ unsigned int FatSystem::nextCluster(unsigned int cluster, int fat) next = next >> 4; } next &= 0xfff; + cout << next << endl; if (next >= 0xff0) { return FAT_LAST; } else { From 4cb5474d5912bf58c2c0db46e2926a9b68106441 Mon Sep 17 00:00:00 2001 From: aarrbba123 <70095196+aarrbba123@users.noreply.github.com> Date: Sun, 24 Sep 2023 07:38:00 +0800 Subject: [PATCH 4/5] Forgot to remove all debug prints --- src/core/FatSystem.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/core/FatSystem.cpp b/src/core/FatSystem.cpp index 8470c53..48caaa5 100644 --- a/src/core/FatSystem.cpp +++ b/src/core/FatSystem.cpp @@ -239,7 +239,6 @@ unsigned int FatSystem::nextCluster(unsigned int cluster, int fat) next = next >> 4; } next &= 0xfff; - cout << next << endl; if (next >= 0xff0) { return FAT_LAST; } else { From af151e9ca5c11e24e73a7fe01bb77b62d9f27b1f Mon Sep 17 00:00:00 2001 From: aarrbba123 <70095196+aarrbba123@users.noreply.github.com> Date: Thu, 28 Sep 2023 01:34:23 +0800 Subject: [PATCH 5/5] Size Workaround --- src/core/FatSystem.cpp | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/core/FatSystem.cpp b/src/core/FatSystem.cpp index 48caaa5..eb8a7e9 100644 --- a/src/core/FatSystem.cpp +++ b/src/core/FatSystem.cpp @@ -214,11 +214,7 @@ unsigned int FatSystem::nextCluster(unsigned int cluster, int fat) return cache[cluster]; } - #ifdef __WIN__ - readData(fatStart+fatSize*fat+(bits*cluster)/8, buffer, sizeof(*buffer)); - #else - readData(fatStart+fatSize*fat+(bits*cluster)/8, buffer, sizeof(buffer)); - #endif + readData(fatStart+fatSize*fat+(bits*cluster)/8, buffer, bytes); unsigned int next; @@ -889,11 +885,8 @@ void FatSystem::rewriteUnallocated(bool random) buffer[i] = 0x0; } } - #ifndef __WIN__ - writeData(clusterAddress(cluster), buffer, sizeof(buffer)); - #else - writeData(clusterAddress(cluster), buffer, sizeof(*buffer)); - #endif + + writeData(clusterAddress(cluster), buffer, bytesPerCluster); total++; #ifdef __WIN__ }