diff options
-rw-r--r-- | src/util.cc | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/src/util.cc b/src/util.cc index 7bfe033..e793a92 100644 --- a/src/util.cc +++ b/src/util.cc @@ -346,9 +346,19 @@ int ReadFile(const string& path, string* contents, string* err) { return -errno; } + struct stat st; + if (fstat(fileno(f), &st) < 0) { + err->assign(strerror(errno)); + fclose(f); + return -errno; + } + + // +1 is for the resize in ManifestParser::Load + contents->reserve(st.st_size + 1); + char buf[64 << 10]; size_t len; - while ((len = fread(buf, 1, sizeof(buf), f)) > 0) { + while (!feof(f) && (len = fread(buf, 1, sizeof(buf), f)) > 0) { contents->append(buf, len); } if (ferror(f)) { |