提交 154a47dc 编写于 作者: Y yejianwu

check filesize in read

上级 d218e459
...@@ -19,8 +19,9 @@ bool ReadFile(const std::string &filename, std::string &content, bool binary) { ...@@ -19,8 +19,9 @@ bool ReadFile(const std::string &filename, std::string &content, bool binary) {
content = ""; content = "";
std::ios_base::openmode mode = std::ios::in; std::ios_base::openmode mode = std::ios::in;
if (binary) if (binary) {
mode |= std::ios::binary; mode |= std::ios::binary;
}
std::ifstream ifs(filename, mode); std::ifstream ifs(filename, mode);
...@@ -30,7 +31,12 @@ bool ReadFile(const std::string &filename, std::string &content, bool binary) { ...@@ -30,7 +31,12 @@ bool ReadFile(const std::string &filename, std::string &content, bool binary) {
} }
ifs.seekg(0, std::ios::end); ifs.seekg(0, std::ios::end);
content.reserve(ifs.tellg()); size_t filesize = ifs.tellg();
if ((filesize / 1024.0 / 1024.0) > 10) {
LOG(ERROR) << "Filesize overflow 10MB";
return false;
}
content.reserve(filesize);
ifs.seekg(0, std::ios::beg); ifs.seekg(0, std::ios::beg);
content.assign(std::istreambuf_iterator<char>(ifs), content.assign(std::istreambuf_iterator<char>(ifs),
std::istreambuf_iterator<char>()); std::istreambuf_iterator<char>());
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册