diff --git a/Source/Tools/FEXRootFSFetcher/XXFileHash.cpp b/Source/Tools/FEXRootFSFetcher/XXFileHash.cpp index 66449285a9..2ccd34c4a0 100644 --- a/Source/Tools/FEXRootFSFetcher/XXFileHash.cpp +++ b/Source/Tools/FEXRootFSFetcher/XXFileHash.cpp @@ -17,19 +17,27 @@ std::pair HashFile(const fextl::string& Filepath) { return {false, 0}; } - auto HadError = [fd]() -> std::pair { + XXH3_state_t* State {}; + auto HadError = [fd, &State]() -> std::pair { close(fd); + if (State) { + XXH3_freeState(State); + } return {false, 0}; }; // Get file size off_t Size = lseek(fd, 0, SEEK_END); - double SizeD = Size; + if (Size == -1) { + return HadError(); + } // Reset to beginning - lseek(fd, 0, SEEK_SET); + if (lseek(fd, 0, SEEK_SET) == -1) { + return HadError(); + } // Set up XXHash state - XXH3_state_t* const State = XXH3_createState(); + State = XXH3_createState(); const XXH64_hash_t Seed = 0; if (!State) { @@ -37,11 +45,10 @@ std::pair HashFile(const fextl::string& Filepath) { } if (XXH3_64bits_reset_withSeed(State, Seed) == XXH_ERROR) { - XXH3_freeState(State); - close(fd); return HadError(); } + const double SizeD = Size; std::vector Data(BLOCK_SIZE); off_t CurrentOffset = 0; auto Now = std::chrono::high_resolution_clock::now(); @@ -52,14 +59,10 @@ std::pair HashFile(const fextl::string& Filepath) { ssize_t Result = pread(fd, Data.data(), BLOCK_SIZE, CurrentOffset); if (Result == -1) { - XXH3_freeState(State); - close(fd); return HadError(); } if (XXH3_64bits_update(State, Data.data(), Result) == XXH_ERROR) { - XXH3_freeState(State); - close(fd); return HadError(); } auto Cur = std::chrono::high_resolution_clock::now();