Skip to content

Commit 73aa302

Browse files
committed
Python: only expose lengths of quote and prefix
1 parent d25b93d commit 73aa302

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

python/ql/lib/semmle/python/AstExtended.qll

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,15 @@ class StringPart extends StringPart_, AstNode {
155155

156156
override Location getLocation() { result = StringPart_.super.getLocation() }
157157

158-
/** Holds if the content of string `StringPart` is surrounded by `prefix` and `quote`. */
159-
predicate context(string prefix, string quote) {
158+
/**
159+
* Holds if the content of string `StringPart` is surrounded by
160+
* a prefix (including a quote) of length `prefixLength` and
161+
* a quote of length `quoteLength`.
162+
*/
163+
predicate contextSize(int prefixLength, int quoteLength) {
160164
exists(int occurrenceOffset |
161-
quote = this.getText().regexpFind("\"{3}|\"{1}|'{3}|'{1}", 0, occurrenceOffset) and
162-
prefix = this.getText().prefix(occurrenceOffset + quote.length())
165+
quoteLength = this.getText().regexpFind("\"{3}|\"{1}|'{3}|'{1}", 0, occurrenceOffset).length() and
166+
prefixLength = occurrenceOffset + quoteLength
163167
)
164168
}
165169

@@ -168,8 +172,8 @@ class StringPart extends StringPart_, AstNode {
168172
* See `context` for obtaining the prefix and the quote.
169173
*/
170174
int getContentLength() {
171-
exists(string prefix, string quote | this.context(prefix, quote) |
172-
result = this.getText().length() - prefix.length() - quote.length()
175+
exists(int prefixLength, int quoteLength | this.contextSize(prefixLength, quoteLength) |
176+
result = this.getText().length() - prefixLength - quoteLength
173177
)
174178
}
175179
}

python/ql/lib/semmle/python/regexp/RegexTreeView.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ module Impl implements RegexTreeViewSig {
230230
index > 0 and
231231
exists(int previousOffset | previousOffset = this.getPartOffset(index - 1) |
232232
result =
233-
previousOffset + re.(StrConst).getImplicitlyConcatenatedPart(index - 1).getContentlength()
233+
previousOffset + re.(StrConst).getImplicitlyConcatenatedPart(index - 1).getContentLength()
234234
)
235235
}
236236

@@ -241,7 +241,7 @@ module Impl implements RegexTreeViewSig {
241241
StringPart getPart(int localOffset) {
242242
exists(int index, int prefixLength | index = max(int i | this.getPartOffset(i) < start) |
243243
result = re.(StrConst).getImplicitlyConcatenatedPart(index) and
244-
exists(string prefix | result.context(prefix, _) | prefixLength = prefix.length()) and
244+
result.contextSize(prefixLength, _) and
245245
// Example:
246246
// re.compile('...' r"""...this..""")
247247
// - `start` is the offset from `(` to `this` as counted after concatenating all parts.

0 commit comments

Comments
 (0)