Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEXRootFSFetcher/XXHash: Fix double fd close #4054

Merged
merged 2 commits into from
Sep 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading