]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-146054: Limit the growth of `encodings.search_function` cache (GH-146055)
authorStan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Tue, 17 Mar 2026 14:02:59 +0000 (14:02 +0000)
committerGitHub <noreply@github.com>
Tue, 17 Mar 2026 14:02:59 +0000 (15:02 +0100)
Lib/encodings/__init__.py
Lib/test/test_codecs.py
Misc/NEWS.d/next/Library/2026-03-17-11-46-20.gh-issue-146054.udYcqn.rst [new file with mode: 0644]

index e205ec326376d8134ba8d1ec3ca15a16c224e1d1..169c48324f227bf51b88d8b2e1a6a9df39067533 100644 (file)
@@ -34,6 +34,7 @@ from _codecs import _normalize_encoding
 from . import aliases
 
 _cache = {}
+_MAXCACHE = 500
 _unknown = '--unknown--'
 _import_tail = ['*']
 _aliases = aliases.aliases
@@ -111,6 +112,8 @@ def search_function(encoding):
 
     if mod is None:
         # Cache misses
+        if len(_cache) >= _MAXCACHE:
+            _cache.clear()
         _cache[encoding] = None
         return None
 
@@ -132,6 +135,8 @@ def search_function(encoding):
         entry = codecs.CodecInfo(*entry)
 
     # Cache the codec registry entry
+    if len(_cache) >= _MAXCACHE:
+        _cache.clear()
     _cache[encoding] = entry
 
     # Register its aliases (without overwriting previously registered
index c31faec9ee5214919411d74d023eccc30599faa1..79c8a7ef8864829312a36cfc954ebead2ced0b91 100644 (file)
@@ -3908,5 +3908,16 @@ class CodecNameNormalizationTest(unittest.TestCase):
             self.assertEqual(normalize('utf\xE9\u20AC\U0010ffff-8'), 'utf_8')
 
 
+class CodecCacheTest(unittest.TestCase):
+    def test_cache_bounded(self):
+        for i in range(encodings._MAXCACHE + 1000):
+            try:
+                b'x'.decode(f'nonexist_{i}')
+            except LookupError:
+                pass
+
+        self.assertLessEqual(len(encodings._cache), encodings._MAXCACHE)
+
+
 if __name__ == "__main__":
     unittest.main()
diff --git a/Misc/NEWS.d/next/Library/2026-03-17-11-46-20.gh-issue-146054.udYcqn.rst b/Misc/NEWS.d/next/Library/2026-03-17-11-46-20.gh-issue-146054.udYcqn.rst
new file mode 100644 (file)
index 0000000..8692c7f
--- /dev/null
@@ -0,0 +1,2 @@
+Limit the size of :func:`encodings.search_function` cache.
+Found by OSS Fuzz in :oss-fuzz:`493449985`.