Do not crash when an error message contains format placeholders - #3203
Do not crash when an error message contains format placeholders#3203Sanjays2402 wants to merge 2 commits into
Conversation
AstroidError.__str__ calls str.format on its message, and several
inference errors build that message from user code. A typename such as
namedtuple("{0}", "") therefore reached "ValueError: " + str(exc)
with a positional placeholder and no arguments, raising IndexError
instead of the intended UseInferenceDefault.
The fallback already handled ValueError; it now also handles
IndexError and KeyError, the other two exceptions str.format
raises for unsatisfiable placeholders.
Closes pylint-dev#3199
Merging this PR will not alter performance
Comparing Footnotes
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3203 +/- ##
=======================================
Coverage 93.64% 93.64%
=======================================
Files 93 93
Lines 11567 11567
=======================================
Hits 10832 10832
Misses 735 735
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
DanielNoord
left a comment
There was a problem hiding this comment.
Can we cover the KeyError with a test as well?
|
Two payloads still get through:
#3214 covers both by also dropping message from the substitution fields, which stops the message from being able to interpolate itself (Please do it that way and credit @kali834x for it). Also adding the changeLog entry would be welcome, plus the KeyError test Daniel asked for. |
|
Implemented the requested hardening in 889342c: AstroidError now excludes the message itself from format fields and falls back on LookupError/ValueError/AttributeError/TypeError, which prevents both attribute traversal and large-width self-formatting. Added the KeyError and AttributeError inference regressions plus a ChangeLog entry. The full tests/brain/test_named_tuple.py passes (26 passed); with the source fix reverted, the AttributeError regression fails during AstroidError.str, and it passes again with the fix. |
|
Please fix CI and rebase. |
Type of Changes
Description
AstroidError.__str__runsstr.formaton its message, and several inference errors build that message out of user code.namedtuple("{0}", "")therefore reached"ValueError: " + str(exc)with an unsatisfiable positional placeholder and raisedIndexErrorinstead of the intendedUseInferenceDefault, crashing pylint.The fallback already caught
ValueError; it now also catchesIndexErrorandKeyError, the other two exceptionsstr.formatraises for placeholders it cannot fill. A regression test was added beside the siblingtest_*_does_not_crash_inferencecases intests/brain/test_named_tuple.py(fails without the fix with the reportedIndexError, passes with it).Happy to add a ChangeLog entry if you would like one for this.
This change was prepared with AI assistance; the regression test was run locally and fails without the fix.
Closes #3199