diff --git a/common/util.cc b/common/util.cc index a853faed0..3d03c96bb 100644 --- a/common/util.cc +++ b/common/util.cc @@ -12,6 +12,7 @@ #include #include #include +#include #ifdef __linux__ #include @@ -78,8 +79,9 @@ std::string read_file(const std::string& fn) { std::ifstream f(fn, std::ios::binary | std::ios::in); if (f.is_open()) { f.seekg(0, std::ios::end); - int size = f.tellg(); - if (f.good() && size > 0) { + std::streamsize size = f.tellg(); + // seekg and tellg on a directory doesn't return pos_type(-1) but max(streamsize) + if (f.good() && size > 0 && size < std::numeric_limits::max()) { std::string result(size, '\0'); f.seekg(0, std::ios::beg); f.read(result.data(), size);