mirror of
https://github.com/python/cpython.git
synced 2026-04-14 07:41:00 +00:00
[3.13] gh-146054: Limit the growth of encodings.search_function cache (GH-146055) (GH-146068)
(cherry picked from commit 9d7621b75b)
Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
This commit is contained in:
parent
ae99fe3a33
commit
2147a5c9de
3 changed files with 18 additions and 0 deletions
|
|
@ -33,6 +33,7 @@
|
|||
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
|
||||
|
|
|
|||
|
|
@ -3822,5 +3822,16 @@ def test_encodings_normalize_encoding(self):
|
|||
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()
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
Limit the size of :func:`encodings.search_function` cache.
|
||||
Found by OSS Fuzz in :oss-fuzz:`493449985`.
|
||||
Loading…
Add table
Add a link
Reference in a new issue