Skip to content

Commit 20c3cfb

Browse files
author
Dave Bartolomeo
committed
Squash a few sign analysis diffs due to range analysis fixes
1 parent afa3399 commit 20c3cfb

File tree

2 files changed

+60
-9
lines changed

2 files changed

+60
-9
lines changed

java/ql/lib/semmle/code/java/semantic/analysis/SignAnalysisCommon.qll

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,25 +204,68 @@ private class BinarySignExpr extends FlowSignExpr {
204204
}
205205
}
206206

207+
/**
208+
* A `Convert`, `Box`, or `Unbox` expression.
209+
*/
210+
private class SemCastExpr extends SemUnaryExpr {
211+
SemCastExpr() {
212+
this instanceof SemConvertExpr
213+
or
214+
this instanceof SemBoxExpr
215+
or
216+
this instanceof SemUnboxExpr
217+
}
218+
}
219+
207220
/** A unary expression whose sign is computed from the sign of its operand. */
208221
private class UnarySignExpr extends FlowSignExpr {
209222
SemUnaryExpr unary;
210223

211-
UnarySignExpr() { unary = this }
224+
UnarySignExpr() { unary = this and not this instanceof SemCastExpr }
212225

213226
override Sign getSignRestriction() {
214227
result = semExprSign(unary.getOperand()).applyUnaryOp(unary.getOpcode())
215228
}
216229
}
217230

218231
/**
219-
* A `Convert` expression, whose sign is computed based on sign of its operand and the source and
220-
* destination types.
232+
* A `Convert`, `Box`, or `Unbox` expression, whose sign is computed based on
233+
* the sign of its operand and the source and destination types.
221234
*/
222-
private class ConvertSignExpr extends UnarySignExpr {
223-
override SemConvertExpr unary;
235+
abstract private class CastSignExpr extends FlowSignExpr {
236+
SemUnaryExpr cast;
237+
238+
CastSignExpr() { cast = this and cast instanceof SemCastExpr }
239+
240+
override Sign getSignRestriction() { result = semExprSign(cast.getOperand()) }
241+
}
224242

225-
override Sign getSignRestriction() { result = semExprSign(unary.getOperand()) }
243+
/**
244+
* A `Convert` expression.
245+
*/
246+
private class ConvertSignExpr extends CastSignExpr {
247+
override SemConvertExpr cast;
248+
}
249+
250+
/**
251+
* A `Box` expression.
252+
*/
253+
private class BoxSignExpr extends CastSignExpr {
254+
override SemBoxExpr cast;
255+
}
256+
257+
/**
258+
* An `Unbox` expression.
259+
*/
260+
private class UnboxSignExpr extends CastSignExpr {
261+
override SemUnboxExpr cast;
262+
263+
UnboxSignExpr() {
264+
exists(SemType fromType | fromType = getTrackedType(cast.getOperand()) |
265+
// Only numeric source types are handled here.
266+
fromType instanceof SemNumericType
267+
)
268+
}
226269
}
227270

228271
private predicate unknownSign(SemExpr e) { e instanceof UnknownSignExpr }

java/ql/lib/semmle/code/java/semantic/analysis/SignAnalysisSpecific.qll

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,16 @@ private class ChooseSignExpr extends CustomSignExpr {
148148
private class CastSignExpr extends CustomSignExpr {
149149
CastExpr cast;
150150

151-
CastSignExpr() { cast = getJavaExpr(this) }
151+
CastSignExpr() {
152+
// The core already handles numeric conversions, boxing, and unboxing.
153+
// We need to handle any casts between reference types that we want to track
154+
// here.
155+
cast = getJavaExpr(this) and
156+
cast.getType() instanceof RefType and
157+
cast.getExpr().getType() instanceof RefType
158+
}
152159

153160
override Sign getSignRestriction() {
154-
// REVIEW: Should only apply to trackable operations
155161
result = semExprSign(getSemanticExpr(cast.getExpr()))
156162
or
157163
semAnySign(result) and not cast.getExpr().getType() instanceof NumericOrCharType
@@ -175,7 +181,9 @@ predicate ignoreTypeRestrictions(SemExpr e) {
175181
*/
176182
predicate trackUnknownNonNumericExpr(SemExpr e) {
177183
// REVIEW: Only needed to match original Java results.
178-
e = getEnhancedForInitExpr(_) or getJavaExpr(e) instanceof VarAccess
184+
e = getEnhancedForInitExpr(_) or
185+
getJavaExpr(e) instanceof VarAccess or
186+
getJavaExpr(e) instanceof CastExpr
179187
}
180188

181189
/**

0 commit comments

Comments
 (0)