Skip to content

Commit c0e3186

Browse files
committed
Ruby: Avoid computing Location::toString in full
1 parent f32fa25 commit c0e3186

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

ruby/ql/lib/codeql/Locations.qll

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
import files.FileSystem
44

5+
bindingset[loc]
6+
pragma[inline_late]
7+
private string locationToString(Location loc) {
8+
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
9+
loc.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
10+
result = filepath + "@" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
11+
)
12+
}
13+
514
/**
615
* A location as given by a file, a start line, a start column,
716
* an end line, and an end column.
@@ -28,12 +37,8 @@ class Location extends @location {
2837
int getNumLines() { result = this.getEndLine() - this.getStartLine() + 1 }
2938

3039
/** Gets a textual representation of this element. */
31-
string toString() {
32-
exists(string filepath, int startline, int startcolumn, int endline, int endcolumn |
33-
this.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) and
34-
result = filepath + "@" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn
35-
)
36-
}
40+
pragma[inline]
41+
string toString() { result = locationToString(this) }
3742

3843
/**
3944
* Holds if this element is at the specified location.

ruby/ql/lib/codeql/ruby/frameworks/Rails.qll

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,13 @@ private class CookiesSameSiteProtectionSetting extends Settings::NillableStringl
303303
}
304304
}
305305

306+
pragma[nomagic]
307+
private predicate isPotentialRenderCall(MethodCall renderCall, Location loc, ErbFile erbFile) {
308+
renderCall.getMethodName() = "render" and
309+
loc = renderCall.getLocation() and
310+
RenderCallUtils::getTemplateFile(renderCall) = erbFile
311+
}
312+
306313
// TODO: initialization hooks, e.g. before_configuration, after_initialize...
307314
// TODO: initializers
308315
/** A synthetic global to represent the value passed to the `locals` argument of a render call for a specific ERB file. */
@@ -313,10 +320,11 @@ private class LocalAssignsHashSyntheticGlobal extends SummaryComponent::Syntheti
313320
private MethodCall renderCall;
314321

315322
LocalAssignsHashSyntheticGlobal() {
316-
this = "LocalAssignsHashSyntheticGlobal+" + id and
317-
id = erbFile.getRelativePath() + "+" + renderCall.getLocation() and
318-
renderCall.getMethodName() = "render" and
319-
RenderCallUtils::getTemplateFile(renderCall) = erbFile
323+
exists(Location loc |
324+
this = "LocalAssignsHashSyntheticGlobal+" + id and
325+
isPotentialRenderCall(renderCall, loc, erbFile) and
326+
id = erbFile.getRelativePath() + "+" + loc
327+
)
320328
}
321329

322330
/** Gets the `ErbFile` which this locals hash is accessible from. */

0 commit comments

Comments
 (0)