Skip to content

Commit c3fd272

Browse files
hvitvedhmac
authored andcommitted
Ruby: Simplify getValueText logic for StringlikeLiterals
1 parent 799ec23 commit c3fd272

File tree

3 files changed

+5
-38
lines changed

3 files changed

+5
-38
lines changed

ruby/ql/lib/codeql/ruby/ast/Literal.qll

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,7 @@ private import codeql.ruby.controlflow.CfgNodes
1010
*
1111
* This is the QL root class for all literals.
1212
*/
13-
class Literal extends Expr, TLiteral {
14-
/**
15-
* Gets the source text for this literal, if this is a simple literal.
16-
*
17-
* For complex literals, such as arrays, hashes, and strings with
18-
* interpolations, this predicate has no result.
19-
*/
20-
override string getValueText() { none() }
21-
}
13+
class Literal extends Expr, TLiteral { }
2214

2315
/**
2416
* A numeric literal, i.e. an integer, floating-point, rational, or complex
@@ -411,17 +403,6 @@ class StringlikeLiteral extends Literal, TStringlikeLiteral {
411403
result = ""
412404
}
413405

414-
override string getValueText() {
415-
forall(StringComponent c | c = this.getComponent(_) |
416-
not c instanceof StringInterpolationComponent
417-
) and
418-
result =
419-
concat(StringComponent c, int i | c = this.getComponent(i) | c.getValueText() order by i)
420-
or
421-
exists(this.getComponent(_)) and
422-
result = this.getAControlFlowNode().(ExprNodes::StringlikeLiteralCfgNode).getValueText()
423-
}
424-
425406
override string toString() {
426407
exists(string full, string summary |
427408
full =

ruby/ql/lib/codeql/ruby/controlflow/CfgNodes.qll

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ module ExprNodes {
485485
final override StringlikeLiteral getExpr() { result = super.getExpr() }
486486

487487
/** Gets the `n`th component of this `StringlikeLiteral` */
488-
StringComponentCfgNode getComponent(int n) { result.getNode() = e.getComponent(n) }
488+
StringComponentCfgNode getComponent(int n) { e.hasCfgChild(e.getComponent(n), this, result) }
489489

490490
/** Gets a component of this `StringlikeLiteral` */
491491
StringComponentCfgNode getAComponent() { result = this.getComponent(_) }
@@ -494,26 +494,13 @@ module ExprNodes {
494494
// if all interpolations have a known string value, we will get a result
495495
language[monotonicAggregates]
496496
override string getValueText() {
497-
result = e.getValueText()
498-
or
499497
result =
500-
concat(StringComponent c, int i |
501-
c = e.getComponent(i)
498+
concat(StringComponentCfgNode c, int i |
499+
c = this.getComponent(i)
502500
|
503-
getComponentValueText(c) order by i
501+
c.getValueText() order by i
504502
)
505503
}
506-
507-
/**
508-
* Get the `ValueText()` of a `StringComponent`.
509-
* If the component has a CFG node, defer to that (in order to resolve variables in interpolations).
510-
* Otherwise, defer to the AST node.
511-
*/
512-
private string getComponentValueText(StringComponent c) {
513-
exists(StringComponentCfgNode n | n.getNode() = c | result = n.getValueText())
514-
or
515-
not exists(StringComponentCfgNode n | n.getNode() = c) and result = c.getValueText()
516-
}
517504
}
518505

519506
/** A control-flow node that wraps a `StringLiteral` AST expression. */

ruby/ql/test/library-tests/ast/ValueText.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,6 @@
614614
| modules/modules.rb:55:8:55:30 | "module Foo::Bar again" | module Foo::Bar again |
615615
| modules/modules.rb:56:17:56:17 | 4 | 4 |
616616
| modules/toplevel.rb:1:6:1:12 | "world" | world |
617-
| modules/toplevel.rb:3:12:3:16 | "!!!" | !!! |
618617
| modules/toplevel.rb:5:14:5:20 | "hello" | hello |
619618
| operations/operations.rb:3:5:3:5 | 0 | 0 |
620619
| operations/operations.rb:4:5:4:5 | 0 | 0 |

0 commit comments

Comments
 (0)