Skip to content

Commit 6fe3609

Browse files
StanFromIrelandmiss-islington
authored andcommitted
gh-146054: Limit the growth of encodings.search_function cache (GH-146055)
(cherry picked from commit 9d7621b) Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
1 parent a005f32 commit 6fe3609

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

Lib/encodings/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from . import aliases
3434

3535
_cache = {}
36+
_MAXCACHE = 500
3637
_unknown = '--unknown--'
3738
_import_tail = ['*']
3839
_aliases = aliases.aliases
@@ -115,6 +116,8 @@ def search_function(encoding):
115116

116117
if mod is None:
117118
# Cache misses
119+
if len(_cache) >= _MAXCACHE:
120+
_cache.clear()
118121
_cache[encoding] = None
119122
return None
120123

@@ -136,6 +139,8 @@ def search_function(encoding):
136139
entry = codecs.CodecInfo(*entry)
137140

138141
# Cache the codec registry entry
142+
if len(_cache) >= _MAXCACHE:
143+
_cache.clear()
139144
_cache[encoding] = entry
140145

141146
# Register its aliases (without overwriting previously registered

Lib/test/test_codecs.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3907,5 +3907,16 @@ def test_encodings_normalize_encoding(self):
39073907
self.assertEqual(normalize('utf...8'), 'utf...8')
39083908

39093909

3910+
class CodecCacheTest(unittest.TestCase):
3911+
def test_cache_bounded(self):
3912+
for i in range(encodings._MAXCACHE + 1000):
3913+
try:
3914+
b'x'.decode(f'nonexist_{i}')
3915+
except LookupError:
3916+
pass
3917+
3918+
self.assertLessEqual(len(encodings._cache), encodings._MAXCACHE)
3919+
3920+
39103921
if __name__ == "__main__":
39113922
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Limit the size of :func:`encodings.search_function` cache.
2+
Found by OSS Fuzz in :oss-fuzz:`493449985`.

0 commit comments

Comments
 (0)