From: Joel Rosdahl Date: Mon, 7 Feb 2022 19:20:08 +0000 (+0100) Subject: fix: Ensure that raw filenames are static in length X-Git-Tag: v4.6~20 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bf3a2a9bb9c9c1bbe1b30094384ca2df9c82bdf4;p=thirdparty%2Fccache.git fix: Ensure that raw filenames are static in length 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. --- diff --git a/src/Result.cpp b/src/Result.cpp index 35ac7f714..36ff33997 100644 --- a/src/Result.cpp +++ b/src/Result.cpp @@ -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(); + 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(); @@ -334,7 +344,7 @@ Writer::do_finalize() writer.write_int(k_result_format_version); writer.write_int(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); diff --git a/src/Result.hpp b/src/Result.hpp index d691ba616..6c85fe0d3 100644 --- a/src/Result.hpp +++ b/src/Result.hpp @@ -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 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 diff --git a/src/ResultExtractor.cpp b/src/ResultExtractor.cpp index 84df13611..d5e30c0c7 100644 --- a/src/ResultExtractor.cpp +++ b/src/ResultExtractor.cpp @@ -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 raw_file) diff --git a/src/ResultExtractor.hpp b/src/ResultExtractor.hpp index 9f2ffc1e9..c34392f63 100644 --- a/src/ResultExtractor.hpp +++ b/src/ResultExtractor.hpp @@ -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 raw_file) override; diff --git a/src/ResultInspector.cpp b/src/ResultInspector.cpp index 49a3370ac..ba3151ec7 100644 --- a/src/ResultInspector.cpp +++ b/src/ResultInspector.cpp @@ -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 raw_file) diff --git a/src/ResultInspector.hpp b/src/ResultInspector.hpp index 98d725cb5..10dad1c42 100644 --- a/src/ResultInspector.hpp +++ b/src/ResultInspector.hpp @@ -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 raw_file) override; diff --git a/src/ResultRetriever.cpp b/src/ResultRetriever.cpp index 576038c37..fc2323be3 100644 --- a/src/ResultRetriever.cpp +++ b/src/ResultRetriever.cpp @@ -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 raw_file) diff --git a/src/ResultRetriever.hpp b/src/ResultRetriever.hpp index 3edfc69ec..98f0b9111 100644 --- a/src/ResultRetriever.hpp +++ b/src/ResultRetriever.hpp @@ -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 raw_file) override;