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

Skip to content

Commit 931100c

Browse files
committed
Python points-to: Add float objects for better backwards compatibility.
1 parent e9f58ba commit 931100c

4 files changed

Lines changed: 55 additions & 8 deletions

File tree

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class IntObjectInternal extends ConstantObjectInternal, TInt {
169169
}
170170

171171
override Builtin getBuiltin() {
172-
result.(Builtin).intValue() = this.intValue()
172+
result.intValue() = this.intValue()
173173
}
174174

175175
override int intValue() {
@@ -188,6 +188,45 @@ class IntObjectInternal extends ConstantObjectInternal, TInt {
188188

189189
}
190190

191+
class FloatObjectInternal extends ConstantObjectInternal, TFloat {
192+
193+
override string toString() {
194+
result = "float " + this.floatValue().toString()
195+
}
196+
197+
override predicate introduced(ControlFlowNode node, PointsToContext context) {
198+
context.appliesTo(node) and
199+
node.getNode().(FloatLiteral).getValue() = this.floatValue()
200+
}
201+
202+
override ObjectInternal getClass() {
203+
result = TBuiltinClassObject(Builtin::special("float"))
204+
}
205+
206+
override Builtin getBuiltin() {
207+
result.getName().toFloat() = this.floatValue()
208+
}
209+
210+
private float floatValue() {
211+
this = TFloat(result)
212+
}
213+
214+
override int intValue() {
215+
this = TFloat(result)
216+
}
217+
218+
override string strValue() {
219+
none()
220+
}
221+
222+
override boolean booleanValue() {
223+
this.floatValue() = 0.0 and result = false
224+
or
225+
this.floatValue() != 0.0 and result = true
226+
}
227+
228+
}
229+
191230

192231
class StringObjectInternal extends ConstantObjectInternal, TString {
193232

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,7 @@ class UnknownInstanceInternal extends TUnknownInstance, ObjectInternal {
221221
}
222222

223223
override predicate introduced(ControlFlowNode node, PointsToContext context) {
224-
context.appliesTo(node) and
225-
this.getClass() = ObjectInternal::builtin("float") and
226-
node.getNode() instanceof FloatLiteral
224+
none()
227225
}
228226

229227
/** Gets the class declaration for this object, if it is a declared class. */

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ newtype TObject =
2222
not bltn.isMethod() and not bltn.isModule() and
2323
not bltn.getClass() = Builtin::special("tuple") and
2424
not exists(bltn.intValue()) and
25+
not exists(bltn.floatValue()) and
2526
not exists(bltn.strValue()) and
2627
not py_special_objects(bltn, _)
2728
}
@@ -58,15 +59,19 @@ newtype TObject =
5859
// And all combinations of flags up to 2^8
5960
n in [0..511] or
6061
// Any number explicitly mentioned in the source code.
61-
exists(Num num |
62-
n = num.getN().toInt() or
62+
exists(IntegerLiteral num |
63+
n = num.getValue() or
6364
exists(UnaryExpr neg | neg.getOp() instanceof USub and neg.getOperand() = num)
6465
and n = -num.getN().toInt()
6566
)
6667
or
6768
n = any(Builtin b).intValue()
6869
}
6970
or
71+
TFloat(float f) {
72+
f = any(FloatLiteral num).getValue()
73+
}
74+
or
7075
TString(string s) {
7176
// Any string explicitly mentioned in the source code.
7277
s = any(StrConst str).getText()
@@ -139,8 +144,6 @@ predicate literal_instantiation(ControlFlowNode n, ClassObjectInternal cls, Poin
139144
or
140145
n instanceof DictNode and cls = ObjectInternal::builtin("dict")
141146
or
142-
n.getNode() instanceof FloatLiteral and cls = ObjectInternal::builtin("float")
143-
or
144147
n.getNode() instanceof ImaginaryLiteral and cls = ObjectInternal::builtin("complex")
145148
)
146149
}
@@ -303,6 +306,8 @@ library class ClassDecl extends @py_object {
303306
name = "MethodType" or
304307
name = "ModuleType"
305308
)
309+
or
310+
this = Builtin::builtin("float")
306311
}
307312

308313
}

python/ql/src/semmle/python/types/Builtins.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ class Builtin extends @py_cobject {
8787
result = this.getName().toInt()
8888
}
8989

90+
float floatValue() {
91+
(this.getClass() = Builtin::special("float")) and
92+
result = this.getName().toFloat()
93+
}
94+
9095
string strValue() {
9196
(this.getClass() = Builtin::special("unicode") or
9297
this.getClass() = Builtin::special("bytes")) and

0 commit comments

Comments
 (0)