mirror of
https://github.com/EQEmu/Server.git
synced 2025-12-11 12:41:30 +00:00
[Crash] Fix large file size crash in File::GetContents for windows (#4735)
This commit is contained in:
parent
5b94e736b3
commit
ab14458f9e
@ -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 = {}
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user