Skip to content

Commit 26a9760

Browse files
util: fix read_file for files > 2GB (#35787)
* util: fix read_file for files > 2GB * fix tellg on directory not returning -1 --------- Co-authored-by: Ngô Việt Hoài Bảo <[email protected]>
1 parent b4ae643 commit 26a9760

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

common/util.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <iomanip>
1313
#include <random>
1414
#include <sstream>
15+
#include <limits>
1516

1617
#ifdef __linux__
1718
#include <sys/prctl.h>
@@ -78,8 +79,9 @@ std::string read_file(const std::string& fn) {
7879
std::ifstream f(fn, std::ios::binary | std::ios::in);
7980
if (f.is_open()) {
8081
f.seekg(0, std::ios::end);
81-
int size = f.tellg();
82-
if (f.good() && size > 0) {
82+
std::streamsize size = f.tellg();
83+
// seekg and tellg on a directory doesn't return pos_type(-1) but max(streamsize)
84+
if (f.good() && size > 0 && size < std::numeric_limits<std::streamsize>::max()) {
8385
std::string result(size, '\0');
8486
f.seekg(0, std::ios::beg);
8587
f.read(result.data(), size);

0 commit comments

Comments
 (0)