-
Notifications
You must be signed in to change notification settings - Fork 180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Detect broken string interpolation and formatting #370
Conversation
This is awesome. The only potential issue I can see with this is that someone could DoS pyflakes by creating a very large string using float interpolation. For instance, |
Maybe that's a feature, since it's equally a problem with the program under analysis. What's the impact to performance? |
Well I would expect rogue code to not be able to crash a static analysis tool. I can also write Also, it's a mistake to believe that |
I didn't think of that, I thought evaluating the string interpolation to be safe because there is no arbitrary Python code (as opposed to eg. There is another bug in number formatting: |
Hi, I don't have any bright idea for avoiding the unbounded memory use. It's an intrinsic flaw of asking Python to evaluate the string. I think I'll close this PR, because it'll not go anywhere I'm afraid. |
Instead of evaluating the format string, you could parse it and compare the fields. There's a stdlib parser at least for Parsing % format is a little trickier, though I think I have that as well: pyupgrade again I don't think there's anything necessary for f-strings since they have their own ast representation, though if you wanted a parser for those I've ported the C parser to pure python here: future-fstrings |
Verify simple cases, such as:
"%s: %s" % (foo, bar, baz)
and"{foo}: {bar}".format(foo="foo", baaar="baz")
.This is done by asking Python to do the formatting, filling in the arguments with dummy objects.
The testing for
.format
string is skipped for Python3 but should be rather easy to add; I want to get an idea of whether you like the current approach first.Resolves #148