]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-146054: Limit the growth of `encodings.search_function` cache (GH-146055...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 18 Mar 2026 12:46:50 +0000 (13:46 +0100)
committerGitHub <noreply@github.com>
Wed, 18 Mar 2026 12:46:50 +0000 (13:46 +0100)
(cherry picked from commit 9d7621b75bc4935e14d4f12dffb3cb1d89ea1bc6)

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
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 f9075b8f0d98acf23cbf61ae62f4881aa5d15f79..1c420ba1c0b7a21d11f125c2a8b44017bf291a94 100644 (file)
@@ -33,6 +33,7 @@ import sys
 from . import aliases
 
 _cache = {}
+_MAXCACHE = 500
 _unknown = '--unknown--'
 _import_tail = ['*']
 _aliases = aliases.aliases
@@ -115,6 +116,8 @@ def search_function(encoding):
 
     if mod is None:
         # Cache misses
+        if len(_cache) >= _MAXCACHE:
+            _cache.clear()
         _cache[encoding] = None
         return None
 
@@ -136,6 +139,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 2d20efe74f2ea6f986f53e54124cede94ff19a2a..9eca8fff62d84d293f594e1aed922289492cfeb6 100644 (file)
@@ -3822,5 +3822,16 @@ class CodecNameNormalizationTest(unittest.TestCase):
         self.assertEqual(normalize('utf...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`.