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

Skip to content

Commit aa9cfeb

Browse files
committed
Ruby: Replace getValueText with getConstantValue
1 parent fede7dd commit aa9cfeb

37 files changed

Lines changed: 732 additions & 280 deletions

ruby/ql/lib/codeql/ruby/Concepts.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ module HTTP {
239239
string getUrlPattern() {
240240
exists(CfgNodes::ExprNodes::StringlikeLiteralCfgNode strNode |
241241
this.getUrlPatternArg().getALocalSource() = DataFlow::exprNode(strNode) and
242-
result = strNode.getExpr().getValueText()
242+
result = strNode.getExpr().getConstantValue().getString()
243243
)
244244
}
245245

@@ -364,7 +364,7 @@ module HTTP {
364364
string getMimetype() {
365365
exists(CfgNodes::ExprNodes::StringlikeLiteralCfgNode strNode |
366366
this.getMimetypeOrContentTypeArg().getALocalSource() = DataFlow::exprNode(strNode) and
367-
result = strNode.getExpr().getValueText().splitAt(";", 0)
367+
result = strNode.getExpr().getConstantValue().getString().splitAt(";", 0)
368368
)
369369
or
370370
not exists(this.getMimetypeOrContentTypeArg()) and

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Call extends Expr instanceof CallImpl {
4141
final Expr getKeywordArgument(string keyword) {
4242
exists(Pair p |
4343
p = this.getAnArgument() and
44-
p.getKey().(SymbolLiteral).getValueText() = keyword and
44+
p.getKey().(SymbolLiteral).getConstantValue().isString(keyword) and
4545
result = p.getValue()
4646
)
4747
}

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

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,93 @@
11
private import codeql.ruby.AST
22
private import internal.AST
3+
private import internal.Constant
34
private import internal.Module
45
private import internal.Variable
56
private import internal.TreeSitter
67

8+
/** A constant value. */
9+
class ConstantValue extends TConstantValue {
10+
/** Gets a textual representation of this constant value. */
11+
final string toString() {
12+
result = this.getInt().toString()
13+
or
14+
result = this.getFloat().toString()
15+
or
16+
exists(int numerator, int denominator |
17+
this.isRational(numerator, denominator) and
18+
result = numerator + "/" + denominator
19+
)
20+
or
21+
exists(float real, float imaginary |
22+
this.isComplex(real, imaginary) and
23+
result = real + "+" + imaginary + "i"
24+
)
25+
or
26+
result = this.getString()
27+
or
28+
result = this.getBoolean().toString()
29+
or
30+
this.isNil() and result = "nil"
31+
}
32+
33+
/** Gets the integer value, if this is an integer. */
34+
int getInt() { this = TInt(result) }
35+
36+
/** Holds if this is the integer value `i`. */
37+
predicate isInt(int i) { i = this.getInt() }
38+
39+
/** Gets the float value, if this is a float. */
40+
float getFloat() { this = TFloat(result) }
41+
42+
/** Holds if this is the float value `f`. */
43+
predicate isFloat(float f) { f = this.getFloat() }
44+
45+
/** Holds if this is the rational value `numerator / denominator`. */
46+
predicate isRational(int numerator, int denominator) { this = TRational(numerator, denominator) }
47+
48+
/** Holds if this is the complex value `real + imaginary * i`. */
49+
predicate isComplex(float real, float imaginary) { this = TComplex(real, imaginary) }
50+
51+
/** Gets the string value, if this is a string. */
52+
string getString() { this = TString(result) }
53+
54+
/** Holds if this is the string value `s`. */
55+
predicate isString(string s) { s = this.getString() }
56+
57+
/** Gets the Boolean value, if this is a Boolean. */
58+
boolean getBoolean() { this = TBoolean(result) }
59+
60+
/** Holds if this is the Boolean value `b`. */
61+
predicate isBoolean(boolean b) { b = this.getBoolean() }
62+
63+
/** Holds if this is the `nil` value. */
64+
predicate isNil() { this = TNil() }
65+
}
66+
67+
/** Provides different sub classes of `ConstantValue`. */
68+
module ConstantValue {
69+
/** A constant integer value. */
70+
class ConstantIntegerValue extends ConstantValue, TInt { }
71+
72+
/** A constant float value. */
73+
class ConstantFloatValue extends ConstantValue, TFloat { }
74+
75+
/** A constant rational value. */
76+
class ConstantRationalValue extends ConstantValue, TRational { }
77+
78+
/** A constant complex value. */
79+
class ConstantComplexValue extends ConstantValue, TComplex { }
80+
81+
/** A constant string value. */
82+
class ConstantStringValue extends ConstantValue, TString { }
83+
84+
/** A constant Boolean value. */
85+
class ConstantBooleanValue extends ConstantValue, TBoolean { }
86+
87+
/** A constant `nil` value. */
88+
class ConstantNilValue extends ConstantValue, TNil { }
89+
}
90+
791
/** An access to a constant. */
892
class ConstantAccess extends Expr, TConstantAccess {
993
/** Gets the name of the constant being accessed. */
@@ -139,7 +223,7 @@ class ConstantReadAccess extends ConstantAccess {
139223
result = lookupConst(resolveConstantReadAccess(this.getScopeExpr()), this.getName())
140224
}
141225

142-
override string getValueText() { result = this.getValue().getValueText() }
226+
final override ConstantValue getConstantValue() { result = this.getValue().getConstantValue() }
143227

144228
final override string getAPrimaryQlClass() { result = "ConstantReadAccess" }
145229
}

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
private import codeql.ruby.AST
22
private import codeql.ruby.CFG
3+
private import codeql.ruby.ast.Constant
34
private import internal.AST
45
private import internal.Expr
56
private import internal.TreeSitter
@@ -10,9 +11,16 @@ private import internal.TreeSitter
1011
* This is the root QL class for all expressions.
1112
*/
1213
class Expr extends Stmt, TExpr {
13-
/** Gets the textual (constant) value of this expression, if any. */
14-
string getValueText() {
15-
forex(CfgNodes::ExprCfgNode n | n = this.getAControlFlowNode() | result = n.getValueText())
14+
/**
15+
* DEPRECATED: Use `getConstantValue` instead.
16+
*
17+
* Gets the textual (constant) value of this expression, if any.
18+
*/
19+
deprecated string getValueText() { result = this.getConstantValue().toString() }
20+
21+
/** Gets the constant value of this expression, if any. */
22+
ConstantValue getConstantValue() {
23+
forex(CfgNodes::ExprCfgNode n | n = this.getAControlFlowNode() | result = n.getConstantValue())
1624
}
1725
}
1826

@@ -456,10 +464,10 @@ class StringConcatenation extends Expr, TStringConcatenation {
456464
* ```
457465
*/
458466
final string getConcatenatedValueText() {
459-
forall(StringLiteral c | c = this.getString(_) | exists(c.getValueText())) and
467+
forall(StringLiteral c | c = this.getString(_) | exists(c.getConstantValue().getString())) and
460468
result =
461469
concat(string valueText, int i |
462-
valueText = this.getString(i).getValueText()
470+
valueText = this.getString(i).getConstantValue().getString()
463471
|
464472
valueText order by i
465473
)

0 commit comments

Comments
 (0)