mirror of
https://github.com/EQEmu/Server.git
synced 2025-12-13 14:41:28 +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 <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;
|
|
||||||
std::string lines;
|
|
||||||
if (f.is_open()) {
|
|
||||||
while (f) {
|
|
||||||
std::getline(f, line);
|
|
||||||
lines += line + "\n";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
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{
|
return FileContentsResult{
|
||||||
.contents = lines,
|
.contents = lines,
|
||||||
.error = error,
|
.error = {}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user