From daddc3e3fb17dd06afd0e8176e85a2229d8236eb Mon Sep 17 00:00:00 2001 From: ICUMADETHIS Date: Sun, 22 Nov 2026 07:56:27 +0000 Subject: [PATCH] Check shader-cache index bounds before allocating --- tests/pack_shader_cache_test.py | 61 +++++++++++++++++++++++++++++++++ tools/pack_shader_cache.py | 10 ++++-- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/tests/pack_shader_cache_test.py b/tests/pack_shader_cache_test.py index 53356ef7..ebd45717 100644 --- a/tests/pack_shader_cache_test.py +++ b/tests/pack_shader_cache_test.py @@ -10,6 +10,8 @@ import sys import tempfile import unittest from pathlib import Path +from types import SimpleNamespace +from unittest import mock ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT / 'tools')) @@ -106,6 +108,65 @@ class PackTest(unittest.TestCase): n, _, _, _ = psc.pack(self.cache) self.assertEqual(n, 4) + def bounded_read_pack(self, path): + """Record real file reads; fail before a corrupt count can request a large allocation, even before the fix.""" + requests = [] + with path.open('rb') as f: + read = f.read + + def bounded(n): + requests.append(n) + self.assertLessEqual(n, 1024, 'corrupt index count reached the allocating read') + return read(n) + + with mock.patch.object(psc, 'open', return_value=f, create=True), \ + mock.patch.object(f, 'read', side_effect=bounded): + result = psc.read_pack(path) + return result, requests + + def test_truncated_index_is_rejected_before_reading_it(self): + path = self.cache / psc.PACK + for count in (1, 0xffffffff): + with self.subTest(count=count): + path.write_bytes(psc.MAGIC + struct.pack(' MAX_ENTRIES or index_size > size - 16: + print(f'{path}: invalid index size, rebuilt from the loose files only', flush=True) + return {} + raw = f.read(index_size) + if len(raw) != index_size: print(f'{path}: truncated index, rebuilt from the loose files only', flush=True) return {} out = {}