Skip to content

Commit 3ceb96a

Browse files
committed
C#: Eliminate Completion.qll.
1 parent e928c22 commit 3ceb96a

3 files changed

Lines changed: 103 additions & 125 deletions

File tree

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

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ private import codeql.controlflow.ControlFlowGraph
77
private import codeql.controlflow.SuccessorType
88
private import semmle.code.csharp.commons.Compilation
99
private import semmle.code.csharp.controlflow.internal.NonReturning as NonReturning
10-
private import semmle.code.csharp.controlflow.internal.Completion as Completion
1110

1211
private module Cfg0 = Make0<Location, Ast>;
1312

@@ -315,6 +314,108 @@ private module Initializers {
315314
}
316315
}
317316

317+
private module Exceptions {
318+
private import semmle.code.csharp.commons.Assertions
319+
private import semmle.code.csharp.frameworks.System
320+
321+
private class Overflowable extends UnaryOperation {
322+
Overflowable() {
323+
not this instanceof UnaryBitwiseOperation and
324+
this.getType() instanceof IntegralType
325+
}
326+
}
327+
328+
/** Holds if `cfe` is a control flow element that may throw an exception. */
329+
predicate mayThrowException(ControlFlowElement cfe) {
330+
exists(cfe.(TriedControlFlowElement).getAThrownException())
331+
or
332+
cfe instanceof Assertion
333+
}
334+
335+
/** A control flow element that is inside a `try` block. */
336+
private class TriedControlFlowElement extends ControlFlowElement {
337+
TriedControlFlowElement() {
338+
this = any(TryStmt try).getATriedElement() and
339+
not this instanceof NonReturning::NonReturningCall
340+
}
341+
342+
/**
343+
* Gets an exception class that is potentially thrown by this element, if any.
344+
*/
345+
Class getAThrownException() {
346+
this instanceof Overflowable and
347+
result instanceof SystemOverflowExceptionClass
348+
or
349+
this.(CastExpr).getType() instanceof IntegralType and
350+
result instanceof SystemOverflowExceptionClass
351+
or
352+
invalidCastCandidate(this) and
353+
result instanceof SystemInvalidCastExceptionClass
354+
or
355+
this instanceof Call and
356+
result instanceof SystemExceptionClass
357+
or
358+
this =
359+
any(MemberAccess ma |
360+
not ma.isConditional() and
361+
ma.getQualifier() = any(Expr e | not e instanceof TypeAccess) and
362+
result instanceof SystemNullReferenceExceptionClass
363+
)
364+
or
365+
this instanceof DelegateCreation and
366+
result instanceof SystemOutOfMemoryExceptionClass
367+
or
368+
this instanceof ArrayCreation and
369+
result instanceof SystemOutOfMemoryExceptionClass
370+
or
371+
this =
372+
any(AddOperation ae |
373+
ae.getType() instanceof StringType and
374+
result instanceof SystemOutOfMemoryExceptionClass
375+
or
376+
ae.getType() instanceof IntegralType and
377+
result instanceof SystemOverflowExceptionClass
378+
)
379+
or
380+
this =
381+
any(SubOperation se |
382+
se.getType() instanceof IntegralType and
383+
result instanceof SystemOverflowExceptionClass
384+
)
385+
or
386+
this =
387+
any(MulOperation me |
388+
me.getType() instanceof IntegralType and
389+
result instanceof SystemOverflowExceptionClass
390+
)
391+
or
392+
this =
393+
any(DivOperation de |
394+
not de.getDenominator().getValue().toFloat() != 0 and
395+
result instanceof SystemDivideByZeroExceptionClass
396+
)
397+
or
398+
this instanceof RemOperation and
399+
result instanceof SystemDivideByZeroExceptionClass
400+
or
401+
this instanceof DynamicExpr and
402+
result instanceof SystemExceptionClass
403+
}
404+
}
405+
406+
pragma[nomagic]
407+
private ValueOrRefType getACastExprBaseType(CastExpr ce) {
408+
result = ce.getType().(ValueOrRefType).getABaseType()
409+
or
410+
result = getACastExprBaseType(ce).getABaseType()
411+
}
412+
413+
pragma[nomagic]
414+
private predicate invalidCastCandidate(CastExpr ce) {
415+
ce.getExpr().getType() = getACastExprBaseType(ce)
416+
}
417+
}
418+
318419
private module Input implements InputSig1, InputSig2 {
319420
predicate cfgCachedStageRef() { CfgCachedStage::ref() }
320421

@@ -368,7 +469,7 @@ private module Input implements InputSig1, InputSig2 {
368469
c.asSimpleAbruptCompletion() instanceof ReturnSuccessor and
369470
always = true
370471
or
371-
Completion::mayThrowException(ast) and
472+
Exceptions::mayThrowException(ast) and
372473
n.isIn(ast) and
373474
c.asSimpleAbruptCompletion() instanceof ExceptionSuccessor and
374475
always = false

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ private import ControlFlow
77
private import semmle.code.csharp.commons.Assertions
88
private import semmle.code.csharp.commons.ComparisonTest
99
private import semmle.code.csharp.commons.StructuralComparison as SC
10-
private import semmle.code.csharp.controlflow.internal.Completion
1110
private import semmle.code.csharp.frameworks.System
1211
private import semmle.code.csharp.frameworks.system.Linq
1312
private import semmle.code.csharp.frameworks.system.Collections

csharp/ql/lib/semmle/code/csharp/controlflow/internal/Completion.qll

Lines changed: 0 additions & 122 deletions
This file was deleted.

0 commit comments

Comments
 (0)