Skip to content

Commit 55cd0fa

Browse files
committed
Move useful helper predicate and types from RangeAnalysis to RangeUtils.
1 parent de3fa8e commit 55cd0fa

2 files changed

Lines changed: 41 additions & 32 deletions

File tree

cpp/ql/src/semmle/code/cpp/rangeanalysis/RangeAnalysis.qll

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -241,38 +241,6 @@ class CondReason extends Reason, TCondReason {
241241
override string toString() { result = getCond().toString() }
242242
}
243243

244-
/**
245-
* Holds if a cast from `fromtyp` to `totyp` can be ignored for the purpose of
246-
* range analysis.
247-
*/
248-
pragma[inline]
249-
private predicate safeCast(IntegralType fromtyp, IntegralType totyp) {
250-
fromtyp.getSize() < totyp.getSize() and
251-
(
252-
fromtyp.isUnsigned()
253-
or
254-
totyp.isSigned()
255-
)
256-
or
257-
fromtyp.getSize() <= totyp.getSize() and
258-
(
259-
fromtyp.isSigned() and
260-
totyp.isSigned()
261-
or
262-
fromtyp.isUnsigned() and
263-
totyp.isUnsigned()
264-
)
265-
}
266-
267-
private class SafeCastInstruction extends ConvertInstruction {
268-
SafeCastInstruction() {
269-
safeCast(getUnary().getResultType(), getResultType())
270-
or
271-
getResultType() instanceof PointerType and
272-
getUnary().getResultType() instanceof PointerType
273-
}
274-
}
275-
276244
/**
277245
* Holds if `typ` is a small integral type with the given lower and upper bounds.
278246
*/

cpp/ql/src/semmle/code/cpp/rangeanalysis/RangeUtils.qll

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,44 @@ predicate backEdge(PhiInstruction phi, PhiInputOperand op) {
8080
phi.getAnOperand() = op and
8181
phi.getBlock() = op.getPredecessorBlock().getBackEdgeSuccessor(_)
8282
}
83+
84+
/**
85+
* Holds if a cast from `fromtyp` to `totyp` can be ignored for the purpose of
86+
* range analysis.
87+
*/
88+
pragma[inline]
89+
private predicate safeCast(IntegralType fromtyp, IntegralType totyp) {
90+
fromtyp.getSize() < totyp.getSize() and
91+
(
92+
fromtyp.isUnsigned()
93+
or
94+
totyp.isSigned()
95+
)
96+
or
97+
fromtyp.getSize() <= totyp.getSize() and
98+
(
99+
fromtyp.isSigned() and
100+
totyp.isSigned()
101+
or
102+
fromtyp.isUnsigned() and
103+
totyp.isUnsigned()
104+
)
105+
}
106+
107+
class PtrToPtrCastInstruction extends ConvertInstruction {
108+
PtrToPtrCastInstruction() {
109+
getResultType() instanceof PointerType and
110+
getUnary().getResultType() instanceof PointerType
111+
}
112+
}
113+
114+
class SafeIntCastInstruction extends ConvertInstruction {
115+
SafeIntCastInstruction() { safeCast(getUnary().getResultType(), getResultType()) }
116+
}
117+
118+
class SafeCastInstruction extends ConvertInstruction {
119+
SafeCastInstruction() {
120+
this instanceof PtrToPtrCastInstruction or
121+
this instanceof SafeIntCastInstruction
122+
}
123+
}

0 commit comments

Comments
 (0)