Skip to content

Commit e095294

Browse files
authored
Merge pull request #21701 from aschackmull/csharp/intvalue
C#: Introduce Expr.getIntValue.
2 parents 7458674 + d3e580f commit e095294

File tree

12 files changed

+38
-40
lines changed

12 files changed

+38
-40
lines changed

csharp/ql/examples/snippets/integer_literal.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
import csharp
1010

1111
from IntegerLiteral literal
12-
where literal.getValue().toInt() = 0
12+
where literal.getIntValue() = 0
1313
select literal

csharp/ql/lib/semmle/code/csharp/Conversion.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ private class SignedIntegralConstantExpr extends Expr {
713713
}
714714

715715
private predicate convConstantIntExpr(SignedIntegralConstantExpr e, SimpleType toType) {
716-
exists(int n | n = e.getValue().toInt() |
716+
exists(int n | n = e.getIntValue() |
717717
toType = any(SByteType t | n in [t.minValue() .. t.maxValue()])
718718
or
719719
toType = any(ByteType t | n in [t.minValue() .. t.maxValue()])
@@ -730,7 +730,7 @@ private predicate convConstantIntExpr(SignedIntegralConstantExpr e, SimpleType t
730730

731731
private predicate convConstantLongExpr(SignedIntegralConstantExpr e) {
732732
e.getType() instanceof LongType and
733-
e.getValue().toInt() >= 0
733+
e.getIntValue() >= 0
734734
}
735735

736736
/** 6.1.10: Implicit reference conversions involving type parameters. */

csharp/ql/lib/semmle/code/csharp/commons/ComparisonTest.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ private newtype TComparisonTest =
161161
compare.getComparisonKind().isCompare() and
162162
outerKind = outer.getComparisonKind() and
163163
outer.getAnArgument() = compare.getExpr() and
164-
i = outer.getAnArgument().getValue().toInt()
164+
i = outer.getAnArgument().getIntValue()
165165
|
166166
outerKind.isEquality() and
167167
(

csharp/ql/lib/semmle/code/csharp/commons/Constants.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ private module ConstantComparisonOperation {
3232

3333
private int maxValue(Expr expr) {
3434
if convertedType(expr) instanceof IntegralType and exists(expr.getValue())
35-
then result = expr.getValue().toInt()
35+
then result = expr.getIntValue()
3636
else result = convertedType(expr).maxValue()
3737
}
3838

3939
private int minValue(Expr expr) {
4040
if convertedType(expr) instanceof IntegralType and exists(expr.getValue())
41-
then result = expr.getValue().toInt()
41+
then result = expr.getIntValue()
4242
else result = convertedType(expr).minValue()
4343
}
4444

csharp/ql/lib/semmle/code/csharp/controlflow/Guards.qll

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,16 @@ private module GuardsInput implements
6060
override boolean asBooleanValue() { boolConst(this, result) }
6161
}
6262

63-
private predicate intConst(Expr e, int i) {
64-
e.getValue().toInt() = i and
65-
(
66-
e.getType() instanceof Enum
67-
or
68-
e.getType() instanceof IntegralType
69-
)
70-
}
71-
7263
private class IntegerConstant extends ConstantExpr {
73-
IntegerConstant() { intConst(this, _) }
64+
IntegerConstant() { exists(this.getIntValue()) }
7465

75-
override int asIntegerValue() { intConst(this, result) }
66+
override int asIntegerValue() { result = this.getIntValue() }
7667
}
7768

7869
private class EnumConst extends ConstantExpr {
7970
EnumConst() { this.getType() instanceof Enum and this.hasValue() }
8071

81-
override int asIntegerValue() { result = this.getValue().toInt() }
72+
override int asIntegerValue() { result = this.getIntValue() }
8273
}
8374

8475
private class StringConstant extends ConstantExpr instanceof StringLiteral {
@@ -517,35 +508,35 @@ class EnumerableCollectionExpr extends Expr {
517508
|
518509
// x.Length == 0
519510
ct.getComparisonKind().isEquality() and
520-
ct.getAnArgument().getValue().toInt() = 0 and
511+
ct.getAnArgument().getIntValue() = 0 and
521512
branch = isEmpty
522513
or
523514
// x.Length == k, k > 0
524515
ct.getComparisonKind().isEquality() and
525-
ct.getAnArgument().getValue().toInt() > 0 and
516+
ct.getAnArgument().getIntValue() > 0 and
526517
branch = true and
527518
isEmpty = false
528519
or
529520
// x.Length != 0
530521
ct.getComparisonKind().isInequality() and
531-
ct.getAnArgument().getValue().toInt() = 0 and
522+
ct.getAnArgument().getIntValue() = 0 and
532523
branch = isEmpty.booleanNot()
533524
or
534525
// x.Length != k, k != 0
535526
ct.getComparisonKind().isInequality() and
536-
ct.getAnArgument().getValue().toInt() != 0 and
527+
ct.getAnArgument().getIntValue() != 0 and
537528
branch = false and
538529
isEmpty = false
539530
or
540531
// x.Length > k, k >= 0
541532
ct.getComparisonKind().isLessThan() and
542-
ct.getFirstArgument().getValue().toInt() >= 0 and
533+
ct.getFirstArgument().getIntValue() >= 0 and
543534
branch = true and
544535
isEmpty = false
545536
or
546537
// x.Length >= k, k > 0
547538
ct.getComparisonKind().isLessThanEquals() and
548-
ct.getFirstArgument().getValue().toInt() > 0 and
539+
ct.getFirstArgument().getIntValue() > 0 and
549540
branch = true and
550541
isEmpty = false
551542
)

csharp/ql/lib/semmle/code/csharp/dataflow/internal/rangeanalysis/ConstantUtils.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ predicate systemArrayLengthAccess(PropertyAccess pa) {
2323
* - a read of the `Length` of an array with `val` lengths.
2424
*/
2525
private predicate constantIntegerExpr(ExprNode e, int val) {
26-
e.getValue().toInt() = val
26+
e.getExpr().getIntValue() = val
2727
or
2828
exists(ExprNode src |
2929
e = getAnExplicitDefinitionRead(src) and

csharp/ql/lib/semmle/code/csharp/exprs/Expr.qll

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ class Expr extends ControlFlowElement, @expr {
5757
/** Gets the value of this expression, if any */
5858
string getValue() { expr_value(this, result) }
5959

60+
/** Gets the integer value of this expression, if any. */
61+
cached
62+
int getIntValue() {
63+
result = this.getValue().toInt() and
64+
(this.getType() instanceof IntegralType or this.getType() instanceof Enum)
65+
}
66+
6067
/** Holds if this expression has a value. */
6168
final predicate hasValue() { exists(this.getValue()) }
6269

csharp/ql/lib/semmle/code/csharp/frameworks/system/runtime/CompilerServices.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class SystemRuntimeCompilerServicesInlineArrayAttribute extends Attribute {
8181
/**
8282
* Gets the length of the inline array.
8383
*/
84-
int getLength() { result = this.getConstructorArgument(0).getValue().toInt() }
84+
int getLength() { result = this.getConstructorArgument(0).getIntValue() }
8585
}
8686

8787
/** An attribute of type `System.Runtime.CompilerServices.OverloadResolutionPriority`. */
@@ -94,5 +94,5 @@ class SystemRuntimeCompilerServicesOverloadResolutionPriorityAttribute extends A
9494
/**
9595
* Gets the priority number.
9696
*/
97-
int getPriority() { result = this.getConstructorArgument(0).getValue().toInt() }
97+
int getPriority() { result = this.getConstructorArgument(0).getIntValue() }
9898
}

csharp/ql/src/Likely Bugs/BadCheckOdd.ql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import csharp
1414

1515
predicate isDefinitelyPositive(Expr e) {
16-
e.getValue().toInt() >= 0 or
16+
e.getIntValue() >= 0 or
1717
e.(PropertyAccess).getTarget().hasName("Length") or
1818
e.(MethodCall).getTarget().hasUndecoratedName("Count")
1919
}
@@ -23,12 +23,12 @@ where
2323
t.getLeftOperand() = lhs and
2424
t.getRightOperand() = rhs and
2525
not isDefinitelyPositive(lhs.getLeftOperand().stripCasts()) and
26-
lhs.getRightOperand().(IntegerLiteral).getValue() = "2" and
26+
lhs.getRightOperand().(IntegerLiteral).getIntValue() = 2 and
2727
(
28-
t instanceof EQExpr and rhs.getValue() = "1" and parity = "oddness"
28+
t instanceof EQExpr and rhs.getIntValue() = 1 and parity = "oddness"
2929
or
30-
t instanceof NEExpr and rhs.getValue() = "1" and parity = "evenness"
30+
t instanceof NEExpr and rhs.getIntValue() = 1 and parity = "evenness"
3131
or
32-
t instanceof GTExpr and rhs.getValue() = "0" and parity = "oddness"
32+
t instanceof GTExpr and rhs.getIntValue() = 0 and parity = "oddness"
3333
)
3434
select t, "Possibly invalid test for " + parity + ". This will fail for negative numbers."

csharp/ql/src/Likely Bugs/MishandlingJapaneseEra.ql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ predicate isExactEraStartDateCreation(ObjectCreation cr) {
2727
cr.getType().hasFullyQualifiedName("System", "DateTime") or
2828
cr.getType().hasFullyQualifiedName("System", "DateTimeOffset")
2929
) and
30-
isEraStart(cr.getArgument(0).getValue().toInt(), cr.getArgument(1).getValue().toInt(),
31-
cr.getArgument(2).getValue().toInt())
30+
isEraStart(cr.getArgument(0).getIntValue(), cr.getArgument(1).getIntValue(),
31+
cr.getArgument(2).getIntValue())
3232
}
3333

3434
predicate isDateFromJapaneseCalendarToDateTime(MethodCall mc) {
@@ -44,7 +44,7 @@ predicate isDateFromJapaneseCalendarToDateTime(MethodCall mc) {
4444
mc.getNumberOfArguments() = 7 // implicitly current era
4545
or
4646
mc.getNumberOfArguments() = 8 and
47-
mc.getArgument(7).getValue() = "0"
47+
mc.getArgument(7).getIntValue() = 0
4848
) // explicitly current era
4949
}
5050

0 commit comments

Comments
 (0)