]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Fix Util::read_file truncating files if size_hint is an underestimate (#808)
authorNicholas Hutchinson <nshutchinson@gmail.com>
Wed, 24 Feb 2021 19:32:20 +0000 (19:32 +0000)
committerGitHub <noreply@github.com>
Wed, 24 Feb 2021 19:32:20 +0000 (20:32 +0100)
If the size_hint passed to read_file was an underestimate, or the
platform's `stat()` implementation gives an inaccurate file size (e.g.
MinGW) then `Util::read_file()` would only issue a single `read()` call
instead of reading the entire file.

Fixes #803.

src/Util.cpp
unittest/test_Util.cpp

index 71de5f9cdb57fc49c335deb5654e280e4eb54d98..f49708fd4653d35352a3e54d776e82a73372e844 100644 (file)
@@ -1180,7 +1180,7 @@ read_file(const std::string& path, size_t size_hint)
   result.resize(size_hint);
 
   while (true) {
-    if (pos > result.size()) {
+    if (pos == result.size()) {
       result.resize(2 * result.size());
     }
     const size_t max_read = result.size() - pos;
index 56d4368f70add4c605f632ace5dfd4224abd69d0..cb2be415c2d126cde905a236b92dd7a321c93fde 100644 (file)
@@ -820,6 +820,10 @@ TEST_CASE("Util::read_file and Util::write_file")
 #else
   CHECK(data == "carpet\n\n");
 #endif
+
+  Util::write_file("size_hint_test", std::string(8192, '\0'));
+  CHECK(Util::read_file("size_hint_test", 4096 /*size_hint*/).size() == 8192);
+
   CHECK_THROWS_WITH(Util::read_file("does/not/exist"),
                     "No such file or directory");