Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aa1337d

Browse files
Apply style suggestions from code review
1 parent e954db2 commit aa1337d

3 files changed

Lines changed: 24 additions & 33 deletions

File tree

java/ql/lib/semmle/code/java/regex/RegexTreeView.qll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,8 +527,7 @@ class RegExpEscape extends RegExpNormalChar {
527527
* Gets the hex number for the `hex` char.
528528
*/
529529
private int toHex(string hex) {
530-
hex = [0 .. 9].toString() and
531-
result = hex.toInt()
530+
result = [0 .. 9] and hex = result.toString()
532531
or
533532
result = 10 and hex = ["a", "A"]
534533
or
@@ -545,7 +544,7 @@ private int toHex(string hex) {
545544

546545
/**
547546
* A character class escape in a regular expression.
548-
* That is, an escaped charachter that denotes multiple characters.
547+
* That is, an escaped character that denotes multiple characters.
549548
*
550549
* Examples:
551550
*

java/ql/lib/semmle/code/java/regex/regex.qll

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,10 @@ abstract class RegexString extends StringLiteral {
5252
* This leads to very similar results for ReDoS queries.
5353
*/
5454
predicate charSet(int start, int end) {
55-
exists(int inner_start, int inner_end |
56-
this.charSetStart(start, inner_start) and
57-
not this.charSetStart(_, start)
58-
|
55+
exists(int inner_start, int inner_end | this.charSetStart(start, inner_start) |
5956
end = inner_end + 1 and
60-
inner_end > inner_start and
61-
this.charSetEnd(inner_end) and
62-
not exists(int mid | this.charSetEnd(mid) | mid > inner_start and mid < inner_end)
57+
inner_end =
58+
min(int end_delimiter | this.charSetEnd(end_delimiter) and end_delimiter > inner_start)
6359
)
6460
}
6561

@@ -159,7 +155,7 @@ abstract class RegexString extends StringLiteral {
159155

160156
/**
161157
* Helper predicate for `escapingChar`.
162-
* In order to avoid negative recusrion, we return a boolean.
158+
* In order to avoid negative recursion, we return a boolean.
163159
* This way, we can refer to `escaping(pos - 1).booleanNot()`
164160
* rather than to a negated version of `escaping(pos)`.
165161
* Does not take into account escape characters inside quote sequences.
@@ -199,13 +195,13 @@ abstract class RegexString extends StringLiteral {
199195
*/
200196
private boolean quoteDelimiter(int index, int pos) {
201197
result = this.quoteDelimiter(pos) and
202-
pos = rank[index](int p | this.quoteDelimiter(p) = [true, false])
198+
pos = rank[index](int p | exists(this.quoteDelimiter(p)))
203199
}
204200

205201
/** Holds if a quoted sequence is found between `start` and `end` */
206202
predicate quote(int start, int end) { this.quote(start, end, _, _) }
207203

208-
/** Holds if a quoted sequence is fund between `start` and `end`, with ontent found between `inner_start` and `inner_end`. */
204+
/** Holds if a quoted sequence is found between `start` and `end`, with ontent found between `inner_start` and `inner_end`. */
209205
predicate quote(int start, int end, int inner_start, int inner_end) {
210206
exists(int index |
211207
this.quoteDelimiter(index, start) = true and
@@ -216,11 +212,10 @@ abstract class RegexString extends StringLiteral {
216212
) and
217213
inner_start = start + 2 and
218214
inner_end = end - 2 and
219-
inner_end > inner_start and
220-
this.quoteDelimiter(inner_end) = false and
221-
not exists(int mid |
222-
this.quoteDelimiter(mid) = false and mid in [inner_start .. inner_end - 1]
223-
)
215+
inner_end =
216+
min(int end_delimiter |
217+
this.quoteDelimiter(end_delimiter) = false and end_delimiter > inner_start
218+
)
224219
)
225220
}
226221

@@ -266,9 +261,7 @@ abstract class RegexString extends StringLiteral {
266261
this.escapingChar(start) and
267262
this.getChar(start + 1) = ["N", "p", "P", "x"] and
268263
this.getChar(start + 2) = "{" and
269-
this.getChar(end - 1) = "}" and
270-
end > start and
271-
not exists(int i | start + 2 < i and i < end - 1 | this.getChar(i) = "}")
264+
end = min(int i | start + 2 < i and this.getChar(i - 1) = "}")
272265
}
273266

274267
/**
@@ -301,7 +294,7 @@ abstract class RegexString extends StringLiteral {
301294
or
302295
this.escapedBraces(start, end)
303296
or
304-
// Boundry matchers \b, \b{g}
297+
// Boundary matchers \b, \b{g}
305298
this.getChar(start + 1) = "b" and
306299
(
307300
if this.getText().substring(start + 2, start + 5) = "{g}"
@@ -654,9 +647,7 @@ abstract class RegexString extends StringLiteral {
654647
this.shortQuantifier(start, end, maybe_empty, may_repeat_forever) and
655648
not this.getChar(end) = ["?", "+"]
656649
or
657-
exists(int short_end |
658-
this.shortQuantifier(start, short_end, maybe_empty, may_repeat_forever)
659-
|
650+
exists(int short_end | this.shortQuantifier(start, short_end, maybe_empty, may_repeat_forever) |
660651
if this.getChar(short_end) = ["?", "+"] then end = short_end + 1 else end = short_end
661652
)
662653
}
@@ -752,11 +743,11 @@ abstract class RegexString extends StringLiteral {
752743
* a character set or a group.
753744
*/
754745
predicate sequence(int start, int end) {
755-
this.sequenceOrquantified(start, end) and
746+
this.sequenceOrQuantified(start, end) and
756747
not this.quantifiedItem(start, end, _, _)
757748
}
758749

759-
private predicate sequenceOrquantified(int start, int end) {
750+
private predicate sequenceOrQuantified(int start, int end) {
760751
this.subsequence(start, end) and
761752
not this.itemStart(end)
762753
}
@@ -787,7 +778,7 @@ abstract class RegexString extends StringLiteral {
787778
}
788779

789780
private predicate subalternation(int start, int end, int itemStart) {
790-
this.sequenceOrquantified(start, end) and
781+
this.sequenceOrQuantified(start, end) and
791782
not this.isOptionDivider(start - 1) and
792783
itemStart = start
793784
or
@@ -801,7 +792,7 @@ abstract class RegexString extends StringLiteral {
801792
this.isOptionDivider(mid) and
802793
itemStart = mid + 1
803794
|
804-
this.sequenceOrquantified(itemStart, end)
795+
this.sequenceOrQuantified(itemStart, end)
805796
or
806797
not this.itemStart(end) and end = itemStart
807798
)

java/ql/lib/semmle/code/java/security/performance/RegExpTreeView.qll

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
/**
22
* This module should provide a class hierarchy corresponding to a parse tree of regular expressions.
3+
* This is the interface to the shared ReDoS library.
34
*/
45

56
import java
67
import semmle.code.java.regex.RegexTreeView
78

89
/**
9-
* Holds if `term` is an ecape class representing e.g. `\d`.
10+
* Holds if `term` is an escape class representing e.g. `\d`.
1011
* `clazz` is which character class it represents, e.g. "d" for `\d`.
1112
*/
1213
predicate isEscapeClass(RegExpTerm term, string clazz) {
13-
exists(RegExpCharacterClassEscape escape | term = escape | escape.getValue() = clazz)
14+
term.(RegExpCharacterClassEscape).getValue() = clazz
1415
}
1516

1617
/**
1718
* Holds if the regular expression should not be considered.
1819
*
1920
* We make the pragmatic performance optimization to ignore regular expressions in files
20-
* that does not belong to the project code (such as installed dependencies).
21+
* that do not belong to the project code (such as installed dependencies).
2122
*/
2223
predicate isExcluded(RegExpParent parent) {
2324
not exists(parent.getRegex().getLocation().getFile().getRelativePath())
2425
or
2526
// Regexes with many occurrences of ".*" may cause the polynomial ReDoS computation to explode, so
2627
// we explicitly exclude these.
27-
count(int i | exists(parent.getRegex().getText().regexpFind("\\.\\*", i, _)) | i) > 10
28+
strictcount(int i | exists(parent.getRegex().getText().regexpFind("\\.\\*", i, _)) | i) > 10
2829
}
2930

3031
/**

0 commit comments

Comments
 (0)