mirror of
https://github.com/python/cpython.git
synced 2026-05-06 04:37:33 -04:00
gh-146054: Limit the growth of encodings.search_function cache (GH-146055)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
Limit the size of :func:`encodings.search_function` cache.
|
||||
Found by OSS Fuzz in :oss-fuzz:`493449985`.
|
||||
Reference in New Issue
Block a user