]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
fix: Ensure that raw filenames are static in length
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 7 Feb 2022 19:20:08 +0000 (20:20 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 7 Feb 2022 19:25:52 +0000 (20:25 +0100)
PrimaryStorage::evict assumes that the entry number is encoded in the
filename as one character, so make sure that this precondition holds in
Result::{Reader,Writer} as well.

src/Result.cpp
src/Result.hpp
src/ResultExtractor.cpp
src/ResultExtractor.hpp
src/ResultInspector.cpp
src/ResultInspector.hpp
src/ResultRetriever.cpp
src/ResultRetriever.hpp

index 35ac7f7147a4b43e50ad7b531d1136aafcb3e999..36ff33997f01c94a9ae65bcc833087dcc286bbd4 100644 (file)
@@ -84,8 +84,15 @@ const uint8_t k_embedded_file_marker = 0;
 const uint8_t k_raw_file_marker = 1;
 
 std::string
-get_raw_file_path(string_view result_path, uint32_t entry_number)
+get_raw_file_path(string_view result_path, uint8_t entry_number)
 {
+  if (entry_number >= 10) {
+    // To support more entries in the future, encode to [0-9a-z]. Note that
+    // PrimaryStorage::evict currently assumes that the entry number is
+    // represented as one character.
+    throw core::Error("Too high raw file entry number: {}", entry_number);
+  }
+
   const auto prefix = result_path.substr(
     0, result_path.length() - Result::k_file_suffix.length());
   return FMT("{}{}W", prefix, entry_number);
@@ -210,8 +217,11 @@ Reader::read(Consumer& consumer)
   }
 
   const auto n_entries = m_reader.read_int<uint8_t>();
+  if (n_entries >= 10) {
+    throw core::Error("Too many entries raw file entries: {}", n_entries);
+  }
 
-  uint32_t i;
+  uint8_t i;
   for (i = 0; i < n_entries; ++i) {
     read_entry(i, consumer);
   }
@@ -224,7 +234,7 @@ Reader::read(Consumer& consumer)
 }
 
 void
-Reader::read_entry(uint32_t entry_number, Reader::Consumer& consumer)
+Reader::read_entry(uint8_t entry_number, Reader::Consumer& consumer)
 {
   const auto marker = m_reader.read_int<uint8_t>();
 
@@ -334,7 +344,7 @@ Writer::do_finalize()
   writer.write_int(k_result_format_version);
   writer.write_int<uint8_t>(m_entries_to_write.size());
 
-  uint32_t entry_number = 0;
+  uint8_t entry_number = 0;
   for (const auto& entry : m_entries_to_write) {
     const bool store_raw =
       entry.value_type == ValueType::path
@@ -406,7 +416,7 @@ Result::Writer::write_embedded_file_entry(core::CacheEntryWriter& writer,
 
 FileSizeAndCountDiff
 Result::Writer::write_raw_file_entry(const std::string& path,
-                                     uint32_t entry_number)
+                                     uint8_t entry_number)
 {
   const auto raw_file = get_raw_file_path(m_result_path, entry_number);
   const auto old_stat = Stat::stat(raw_file);
index d691ba616062750c969b6ed9389032981a188e35..6c85fe0d3e1981012394a6a4f3303f159b90feb9 100644 (file)
@@ -109,7 +109,7 @@ public:
   public:
     virtual ~Consumer() = default;
 
-    virtual void on_entry_start(uint32_t entry_number,
+    virtual void on_entry_start(uint8_t entry_number,
                                 FileType file_type,
                                 uint64_t file_len,
                                 nonstd::optional<std::string> raw_file) = 0;
@@ -124,7 +124,7 @@ private:
   core::CacheEntryReader& m_reader;
   const std::string m_result_path;
 
-  void read_entry(uint32_t entry_number, Reader::Consumer& consumer);
+  void read_entry(uint8_t entry_number, Reader::Consumer& consumer);
 };
 
 // This class knows how to write a result cache entry.
@@ -161,7 +161,7 @@ private:
                                         const std::string& path,
                                         uint64_t file_size);
   FileSizeAndCountDiff write_raw_file_entry(const std::string& path,
-                                            uint32_t entry_number);
+                                            uint8_t entry_number);
 };
 
 } // namespace Result
index 84df13611f0546b1188042739d531e55657b1f5a..d5e30c0c71fd7d18fd473790d7f3966dbc7e988b 100644 (file)
@@ -34,7 +34,7 @@ ResultExtractor::ResultExtractor(const std::string& directory)
 }
 
 void
-ResultExtractor::on_entry_start(uint32_t /*entry_number*/,
+ResultExtractor::on_entry_start(uint8_t /*entry_number*/,
                                 Result::FileType file_type,
                                 uint64_t /*file_len*/,
                                 nonstd::optional<std::string> raw_file)
index 9f2ffc1e9aae871b533df32cf145b208e92d2c99..c34392f63e4975c028dbb054fb1a5ebcc81783c6 100644 (file)
@@ -29,7 +29,7 @@ class ResultExtractor : public Result::Reader::Consumer
 public:
   ResultExtractor(const std::string& directory);
 
-  void on_entry_start(uint32_t entry_number,
+  void on_entry_start(uint8_t entry_number,
                       Result::FileType file_type,
                       uint64_t file_len,
                       nonstd::optional<std::string> raw_file) override;
index 49a3370ac1352811a98a4f202b2ef53b350d5369..ba3151ec74032af5392bc780c0d5f5e4827125dd 100644 (file)
@@ -29,7 +29,7 @@ ResultInspector::ResultInspector(FILE* stream) : m_stream(stream)
 }
 
 void
-ResultInspector::on_entry_start(uint32_t entry_number,
+ResultInspector::on_entry_start(uint8_t entry_number,
                                 Result::FileType file_type,
                                 uint64_t file_len,
                                 optional<std::string> raw_file)
index 98d725cb513b196dbbe0b064e90caa63aefdd354..10dad1c42e2831aad7625904e45148c455968d8a 100644 (file)
@@ -29,7 +29,7 @@ class ResultInspector : public Result::Reader::Consumer
 public:
   ResultInspector(FILE* stream);
 
-  void on_entry_start(uint32_t entry_number,
+  void on_entry_start(uint8_t entry_number,
                       Result::FileType file_type,
                       uint64_t file_len,
                       nonstd::optional<std::string> raw_file) override;
index 576038c3772d09ced280a6380b03aa17bc5f7603..fc2323be3d4cc7127dd41b9666bbaeca41f0099d 100644 (file)
@@ -42,7 +42,7 @@ ResultRetriever::ResultRetriever(Context& ctx, bool rewrite_dependency_target)
 }
 
 void
-ResultRetriever::on_entry_start(uint32_t entry_number,
+ResultRetriever::on_entry_start(uint8_t entry_number,
                                 FileType file_type,
                                 uint64_t file_len,
                                 nonstd::optional<std::string> raw_file)
index 3edfc69ec0670264e07c739fce13cd0384a532f9..98f0b911157163705ce584ffb74005d3e1c57fe0 100644 (file)
@@ -29,7 +29,7 @@ class ResultRetriever : public Result::Reader::Consumer
 public:
   ResultRetriever(Context& ctx, bool rewrite_dependency_target);
 
-  void on_entry_start(uint32_t entry_number,
+  void on_entry_start(uint8_t entry_number,
                       Result::FileType file_type,
                       uint64_t file_len,
                       nonstd::optional<std::string> raw_file) override;