[Crash] Fix large file size crash in File::GetContents for windows (#4735)

This commit is contained in:
Chris Miles 2025-03-01 18:32:35 -06:00 committed by GitHub
parent 5b94e736b3
commit ab14458f9e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -39,6 +39,7 @@
#include <filesystem>
#include <iostream>
#include <sys/stat.h>
#include <vector>
namespace fs = std::filesystem;
@ -90,23 +91,21 @@ std::string File::GetCwd()
FileContentsResult File::GetContents(const std::string &file_name)
{
std::string error;
std::ifstream f;
f.open(file_name);
std::string line;
std::string lines;
if (f.is_open()) {
while (f) {
std::getline(f, line);
lines += line + "\n";
}
std::ifstream f(file_name, std::ios::in | std::ios::binary);
if (!f) {
return { .error = fmt::format("Couldn't open file [{}]", file_name) };
}
else {
error = fmt::format("Couldn't open file [{}]", file_name);
constexpr size_t CHUNK_SIZE = 4096; // Read 4KB chunks
std::string lines;
std::vector<char> buffer(CHUNK_SIZE);
while (f.read(buffer.data(), CHUNK_SIZE) || f.gcount() > 0) {
lines.append(buffer.data(), f.gcount());
}
return FileContentsResult{
.contents = lines,
.error = error,
.error = {}
};
}