Reject negative cache item sizes#401
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #401 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 5 5
Lines 1084 1086 +2
=========================================
+ Hits 1084 1086 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
@CodingFeng101: Thanks for contributing! I never thought that check was actually worth it, because "who would do such a thing?", but you're probably right. If |
|
Thanks for taking a look. My preference is to keep this PR at The negative case directly breaks the cache accounting invariant because
So I think |
What changed
Cache.__setitem__()now rejects negative values returned bygetsizeof()before mutating cache state.Why
A custom
getsizeofreturning a negative number currently letscurrsizebecome negative. That breaks the cache size invariant and can make later capacity checks behave incorrectly.Validation
PYTHONPATH=src python -m unittest tests.test_cache.CacheTest.test_getsizeof_negativePYTHONPATH=src python -m pytest tests -qpython -m ruff check src\cachetools\__init__.py tests\__init__.pygit diff --check