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