File tree Expand file tree Collapse file tree 3 files changed +18
-0
lines changed
Expand file tree Collapse file tree 3 files changed +18
-0
lines changed Original file line number Diff line number Diff line change 3333from . 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
Original file line number Diff line number Diff line change @@ -3822,5 +3822,16 @@ def test_encodings_normalize_encoding(self):
38223822 self .assertEqual (normalize ('utf...8' ), 'utf...8' )
38233823
38243824
3825+ class CodecCacheTest (unittest .TestCase ):
3826+ def test_cache_bounded (self ):
3827+ for i in range (encodings ._MAXCACHE + 1000 ):
3828+ try :
3829+ b'x' .decode (f'nonexist_{ i } ' )
3830+ except LookupError :
3831+ pass
3832+
3833+ self .assertLessEqual (len (encodings ._cache ), encodings ._MAXCACHE )
3834+
3835+
38253836if __name__ == "__main__" :
38263837 unittest .main ()
Original file line number Diff line number Diff line change 1+ Limit the size of :func: `encodings.search_function ` cache.
2+ Found by OSS Fuzz in :oss-fuzz: `493449985 `.
You can’t perform that action at this time.
0 commit comments