Skip to content

Commit

Permalink
Merge pull request #4054 from Sonicadvance1/fix_xxhash
Browse files Browse the repository at this point in the history
FEXRootFSFetcher/XXHash: Fix double fd close
  • Loading branch information
Sonicadvance1 authored Sep 10, 2024
2 parents f99900b + a05d6ae commit 360ea53
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions Source/Tools/FEXRootFSFetcher/XXFileHash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,38 @@ std::pair<bool, uint64_t> HashFile(const fextl::string& Filepath) {
return {false, 0};
}

auto HadError = [fd]() -> std::pair<bool, uint64_t> {
XXH3_state_t* State {};
auto HadError = [fd, &State]() -> std::pair<bool, uint64_t> {
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) {
return HadError();
}

if (XXH3_64bits_reset_withSeed(State, Seed) == XXH_ERROR) {
XXH3_freeState(State);
close(fd);
return HadError();
}

const double SizeD = Size;
std::vector<char> Data(BLOCK_SIZE);
off_t CurrentOffset = 0;
auto Now = std::chrono::high_resolution_clock::now();
Expand All @@ -52,14 +59,10 @@ std::pair<bool, uint64_t> 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();
Expand Down

0 comments on commit 360ea53

Please sign in to comment.