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

Skip to content

Commit 96b8d78

Browse files
committed
Adds modernized files.
1 parent 2df3fe8 commit 96b8d78

3 files changed

Lines changed: 75 additions & 6 deletions

File tree

python/ql/src/Expressions/TruncatedDivision.ql

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,20 @@ where
1818
// Only relevant for Python 2, as all later versions implement true division
1919
major_version() = 2
2020
and
21-
exists(BinaryExprNode bin, Object lobj, Object robj |
21+
exists(BinaryExprNode bin, Value lobj, Value robj |
2222
bin = div.getAFlowNode()
2323
and bin.getNode().getOp() instanceof Div
24-
and bin.getLeft().refersTo(lobj, theIntType(), left)
25-
and bin.getRight().refersTo(robj, theIntType(), right)
24+
and bin.getLeft().pointsTo(lobj, left)
25+
and lobj.getClass() = ClassValue::int_()
26+
and bin.getRight().pointsTo(robj, right)
27+
and robj.getClass() = ClassValue::int_()
2628
// Ignore instances where integer division leaves no remainder
27-
and not lobj.(NumericObject).intValue() % robj.(NumericObject).intValue() = 0
29+
and not lobj.(NumericValue).intValue() % robj.(NumericValue).intValue() = 0
2830
and not bin.getNode().getEnclosingModule().hasFromFuture("division")
2931
// Filter out results wrapped in `int(...)`
30-
and not exists(CallNode c, ClassObject cls |
32+
and not exists(CallNode c, ClassValue cls |
3133
c.getAnArg() = bin
32-
and c.getFunction().refersTo(cls)
34+
and c.getFunction().pointsTo(cls)
3335
and cls.getName() = "int"
3436
)
3537
)

python/ql/src/semmle/python/objects/ObjectAPI.qll

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ class Value extends TObject {
7878
predicate isBuiltin() {
7979
this.(ObjectInternal).isBuiltin()
8080
}
81+
82+
/** INTERNAL -- Do not use */
83+
Builtin asBuiltin() {
84+
this = TBuiltinClassObject(result) or
85+
this = TBuiltinFunctionObject(result) or
86+
this = TBuiltinMethodObject(result) or
87+
this = TBuiltinModuleObject(result) or
88+
this = TBuiltinOpaqueObject(result) or
89+
this = TBuiltinTuple(result)
90+
}
8191

8292
predicate hasLocationInfo(string filepath, int bl, int bc, int el, int ec) {
8393
this.(ObjectInternal).getOrigin().getLocation().hasLocationInfo(filepath, bl, bc, el, ec)
@@ -134,6 +144,62 @@ class Value extends TObject {
134144
}
135145
}
136146

147+
/** Numeric values (ints and floats).
148+
* Includes those occurring in the source as a literal
149+
* or in a builtin module as a value.
150+
*/
151+
class NumericValue extends Value {
152+
153+
NumericValue() {
154+
this.asBuiltin().getClass() = theIntType().asBuiltin() or
155+
this.asBuiltin().getClass() = theLongType().asBuiltin() or
156+
this.asBuiltin().getClass() = theFloatType().asBuiltin()
157+
}
158+
159+
/** Gets the Boolean value that this object
160+
* would evaluate to in a Boolean context,
161+
* such as `bool(x)` or `if x: ...`
162+
*/
163+
override boolean booleanValue() {
164+
this.intValue() != 0 and result = true
165+
or
166+
this.intValue() = 0 and result = false
167+
or
168+
this.floatValue() != 0 and result = true
169+
or
170+
this.floatValue() = 0 and result = false
171+
}
172+
173+
/** Gets the value of this object if it is a constant integer and it fits in a QL int */
174+
int intValue() {
175+
(
176+
this.asBuiltin().getClass() = theIntType().asBuiltin() or
177+
this.asBuiltin().getClass() = theLongType().asBuiltin()
178+
)
179+
and
180+
result = this.asBuiltin().getName().toInt()
181+
}
182+
183+
/** Gets the value of this object if it is a constant float */
184+
float floatValue() {
185+
this.asBuiltin().getClass() = theFloatType().asBuiltin()
186+
and
187+
result = this.asBuiltin().getName().toFloat()
188+
}
189+
190+
/** Gets the string representation of this object, equivalent to calling repr() in Python */
191+
string repr() {
192+
exists(string s |
193+
s = this.asBuiltin().getName() |
194+
if this.asBuiltin().getClass() = theLongType().asBuiltin() then
195+
result = s + "L"
196+
else
197+
result = s
198+
)
199+
}
200+
201+
}
202+
137203
/** Class representing modules in the Python program
138204
* Each `ModuleValue` represents a module object in the Python program.
139205
*/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Expressions/TruncatedDivision.ql

0 commit comments

Comments
 (0)