Skip to content

Commit c1b011d

Browse files
committed
better type resolution of add expressions
1 parent 2a5d567 commit c1b011d

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

ql/src/codeql_ql/ast/Ast.qll

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,6 +1794,7 @@ class Negation extends TNegation, Formula {
17941794

17951795
/** An expression, such as `x+4`. */
17961796
class Expr extends TExpr, AstNode {
1797+
cached
17971798
Type getType() { none() }
17981799
}
17991800

@@ -1874,15 +1875,14 @@ class AddSubExpr extends TAddSubExpr, BinOpExpr {
18741875
result = this.getLeftOperand().getType() and
18751876
result = this.getRightOperand().getType()
18761877
or
1877-
// Both operands are subtypes of `int`
1878-
result.getName() = "int" and
1879-
result = this.getLeftOperand().getType().getASuperType*() and
1880-
result = this.getRightOperand().getType().getASuperType*()
1878+
// Both operands are subtypes of `int` / `string` / `float`
1879+
exprOfPrimitiveAddType(result) = this.getLeftOperand() and
1880+
exprOfPrimitiveAddType(result) = this.getRightOperand()
18811881
or
18821882
// Coercion to from `int` to `float`
18831883
exists(PrimitiveType i | result.getName() = "float" and i.getName() = "int" |
18841884
this.getAnOperand().getType() = result and
1885-
this.getAnOperand().getType().getASuperType*() = i
1885+
this.getAnOperand() = exprOfPrimitiveAddType(i)
18861886
)
18871887
or
18881888
// Coercion to `string`
@@ -1899,6 +1899,18 @@ class AddSubExpr extends TAddSubExpr, BinOpExpr {
18991899
}
19001900
}
19011901

1902+
pragma[noinline]
1903+
private Expr exprOfPrimitiveAddType(PrimitiveType t) {
1904+
result.getType() = getASubTypeOfAddPrimitive(t)
1905+
}
1906+
1907+
private Type getASubTypeOfAddPrimitive(PrimitiveType prim) {
1908+
result = prim and
1909+
result.getName() = ["int", "string", "float"]
1910+
or
1911+
result.getASuperType() = getASubTypeOfAddPrimitive(prim)
1912+
}
1913+
19021914
/**
19031915
* An addition expression, such as `x + y`.
19041916
*/

ql/test/type/type.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
| Test.qll:8:67:8:69 | Float | file://:0:0:0:0 | float |
2929
| Test.qll:11:37:11:42 | result | file://:0:0:0:0 | string |
3030
| Test.qll:11:46:11:46 | a | Test.qll:3:1:5:1 | Strings |
31+
| Test.qll:11:46:11:50 | AddExpr | file://:0:0:0:0 | string |
3132
| Test.qll:11:50:11:50 | b | Test.qll:3:1:5:1 | Strings |
3233
| Test.qll:13:36:13:41 | result | file://:0:0:0:0 | float |
3334
| Test.qll:13:45:13:45 | a | Test.qll:7:1:9:1 | Floats |
35+
| Test.qll:13:45:13:49 | AddExpr | file://:0:0:0:0 | float |
3436
| Test.qll:13:49:13:49 | b | Test.qll:7:1:9:1 | Floats |

0 commit comments

Comments
 (0)