-
Notifications
You must be signed in to change notification settings - Fork 134
Hash on stream instead of the entire file #481
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -22,7 +22,6 @@ std::string calculate_file_sha256(const std::string& file_path) { | |||||||
| if (!file.is_open()) { | ||||||||
| return ""; | ||||||||
| } | ||||||||
|
|
||||||||
| std::vector<unsigned char> hash(picosha2::k_digest_size); | ||||||||
| picosha2::hash256(file, hash.begin(), hash.end()); | ||||||||
| return picosha2::bytes_to_hex_string(hash.begin(), hash.end()); | ||||||||
|
|
@@ -34,16 +33,27 @@ std::string calculate_git_blob_oid(const std::string& file_path) { | |||||||
| return ""; | ||||||||
| } | ||||||||
| file.seekg(0, std::ios::end); | ||||||||
| size_t size = file.tellg(); | ||||||||
| file.seekg(0, std::ios::beg); | ||||||||
| if (!file) { | ||||||||
| return ""; | ||||||||
| } | ||||||||
|
|
||||||||
| std::ostringstream oss; | ||||||||
| oss << "blob " << size << '\0'; // Git blob header | ||||||||
| oss << file.rdbuf(); | ||||||||
| std::streampos end_pos = file.tellg(); | ||||||||
| if (end_pos == std::streampos(-1)) { | ||||||||
| return ""; | ||||||||
| } | ||||||||
|
|
||||||||
| std::string blob_data = oss.str(); | ||||||||
| size_t size = static_cast<size_t>(end_pos); | ||||||||
| file.clear(); | ||||||||
| file.seekg(0, std::ios::beg); | ||||||||
| if (!file) { | ||||||||
| return ""; | ||||||||
| } | ||||||||
|
|
||||||||
| std::string header = "blob " + std::to_string(size) + '\0'; | ||||||||
|
||||||||
| std::string header = "blob " + std::to_string(size) + '\0'; | |
| std::string header = "blob " + std::to_string(size); | |
| header.push_back('\0'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
end_pos(astd::streampos) is cast directly tosize_t. On some platforms/configs this can truncate/overflow for large files (e.g., 32-bit builds or very large model files), producing an incorrect Git blob OID and causing spurious hash mismatches. Consider computing the byte size viastd::filesystem::file_size()(with error_code) or converting throughstd::streamoffand explicitly checkingend_pos < 0/end_pos > std::numeric_limits<size_t>::max()before casting.