From 26a9760afc7d1994509e0fa81d082225341367ea Mon Sep 17 00:00:00 2001 From: Bao Date: Sat, 26 Jul 2025 03:53:37 +0700 Subject: [PATCH] util: fix read_file for files > 2GB (#35787) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * util: fix read_file for files > 2GB * fix tellg on directory not returning -1 --------- Co-authored-by: Ngô Việt Hoài Bảo --- common/util.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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);