-
Notifications
You must be signed in to change notification settings - Fork 304
Fix IndexError in constructor args with bitwise operators #426
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
base: develop
Are you sure you want to change the base?
Changes from all commits
93b47d5
f7cb5b3
b34d8f7
1489d3e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -920,6 +920,10 @@ | |
| # Match string that indicates we're working on a Linux Kernel file. | ||
| _SEARCH_KERNEL_FILE = re.compile(r"\b(?:LINT_KERNEL_FILE)") | ||
|
|
||
| # Operator sequences beginning with '<' (used to strip before counting angle brackets). | ||
| # Longer sequences must come first so '<<=' is not partially matched as '<<'. | ||
| _LANGLE_OPS_RE = re.compile(r"<<=|<=|<<") | ||
|
|
||
| # Commands for sed to fix the problem | ||
| _SED_FIXUPS = { | ||
| "Remove spaces around =": r"s/ = /=/", | ||
|
|
@@ -3972,15 +3976,27 @@ def CheckForNonStandardConstructs(filename, clean_lines, linenum, nesting_state, | |
| constructor_args = explicit_constructor_match.group(2).split(",") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not make one regex replacement here instead of needing to define a variable? |
||
|
|
||
| # collapse arguments so that commas in template parameter lists and function | ||
| # argument parameter lists don't split arguments in two | ||
| # argument parameter lists don't split arguments in two. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you're making this a sentence, please also capitalize. |
||
| # Strip operator sequences that begin with '<' before counting angle | ||
| # brackets, to avoid confusing operators with unmatched template | ||
|
Comment on lines
+3980
to
+3981
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could be terser |
||
| # brackets. Longer sequences must come first so that '<<=' is not | ||
| # partially matched as '<<' leaving a stray '='. | ||
| # Note: '>>' and '>=' are intentionally left alone because extra '>' | ||
| # characters do not trigger the joining loop (condition is | ||
| # count('<') > count('>')), and stripping '>>' would break nested | ||
| # template types such as vector<vector<int>>. | ||
|
Comment on lines
+3982
to
+3987
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These important comments about the design of the compiled regex should not be separated from the compiled regex. |
||
| i = 0 | ||
| while i < len(constructor_args): | ||
| constructor_arg = constructor_args[i] | ||
| while constructor_arg.count("<") > constructor_arg.count(">") or constructor_arg.count( | ||
| cleaned_arg = _LANGLE_OPS_RE.sub("", constructor_arg) | ||
| while cleaned_arg.count("<") > cleaned_arg.count(">") or cleaned_arg.count( | ||
| "(" | ||
| ) > constructor_arg.count(")"): | ||
| ) > cleaned_arg.count(")"): | ||
| if i + 1 >= len(constructor_args): | ||
| break | ||
| constructor_arg += "," + constructor_args[i + 1] | ||
| del constructor_args[i + 1] | ||
| cleaned_arg = _LANGLE_OPS_RE.sub("", constructor_arg) | ||
| constructor_args[i] = constructor_arg | ||
| i += 1 | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1823,6 +1823,32 @@ class Foo { | |||||
| """ | ||||||
| class Foo { | ||||||
| explicit Foo(int f, int g); | ||||||
| };""", | ||||||
| "", | ||||||
| ) | ||||||
| # No crash or warning for constructors with '<'-containing operators | ||||||
| # in default parameter values (regression test for issue #223). | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| # left shift << | ||||||
| self.TestMultiLineLint( | ||||||
| """ | ||||||
| class A { | ||||||
| A(int b, int c, int a = 1 << 1) {} | ||||||
| };""", | ||||||
| "", | ||||||
| ) | ||||||
| # compound left-shift-assign <<= | ||||||
| self.TestMultiLineLint( | ||||||
| """ | ||||||
| class A { | ||||||
| A(int b, int c, int a = (x <<= 1)) {} | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the error involves comma-joining logic, we should rotate the parameter order in tests, even though I suspect doing so won't catch any real bugs. |
||||||
| };""", | ||||||
| "", | ||||||
| ) | ||||||
| # less-or-equal <= | ||||||
| self.TestMultiLineLint( | ||||||
| """ | ||||||
| class A { | ||||||
| A(int b, int c, int a = b <= c ? 1 : 0) {} | ||||||
| };""", | ||||||
| "", | ||||||
| ) | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's "LANGLE"?
If this variable is necessary, it should not be all the way up here for more global things; instead should be before the function and named _RE_PATTERN_SOMETHING_DESCRIPTIVE .