]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
fix: Verify cache entry type before parsing manifest or result
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 27 Apr 2026 16:58:32 +0000 (18:58 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Mon, 27 Apr 2026 16:58:32 +0000 (18:58 +0200)
Manifest and result cache entry keys should never collide, but handle
collision properly if they do anyway.

src/ccache/ccache.cpp

index d4fe5de4aab57a3a90bbc7a11a1eb290a2fd4cfd..2111b8740bda499b68e702e31c304579938d6657 100644 (file)
@@ -892,6 +892,12 @@ read_manifest(Context& ctx, std::span<const uint8_t> cache_entry_data)
 {
   try {
     core::CacheEntry cache_entry(cache_entry_data);
+    if (cache_entry.header().entry_type != core::CacheEntryType::manifest) {
+      throw core::Error(
+        FMT("expected cache entry type {} (manifest), actual {}",
+            static_cast<uint8_t>(core::CacheEntryType::manifest),
+            static_cast<uint8_t>(cache_entry.header().entry_type)));
+    }
     cache_entry.verify_checksum();
     ctx.manifest.read(cache_entry.payload());
   } catch (const core::Error& e) {
@@ -2641,6 +2647,12 @@ from_cache(Context& ctx, FromCacheCallMode mode, const Hash::Digest& result_key)
 
   try {
     core::CacheEntry cache_entry(cache_entry_data);
+    if (cache_entry.header().entry_type != core::CacheEntryType::result) {
+      throw core::Error(
+        FMT("expected cache entry type {} (result), actual {}",
+            static_cast<uint8_t>(core::CacheEntryType::result),
+            static_cast<uint8_t>(cache_entry.header().entry_type)));
+    }
     cache_entry.verify_checksum();
     core::result::Deserializer deserializer(cache_entry.payload());
     core::ResultRetriever result_retriever(ctx, result_key);