mirror of
https://github.com/github/codeql.git
synced 2026-05-01 03:35:13 +02:00
Merge pull request #895 from hvitved/csharp/get-a-thrown-exception
C#: Avoid using `ExceptionClass` in deliberate Cartesian products
This commit is contained in:
@@ -23,7 +23,7 @@ import semmle.code.csharp.frameworks.System
|
||||
* Gets an exception type that may be thrown during the execution of method `m`.
|
||||
* Assumes any exception may be thrown by library types.
|
||||
*/
|
||||
ExceptionClass getAThrownException(Method m) {
|
||||
Class getAThrownException(Method m) {
|
||||
m.fromLibrary() and
|
||||
result = any(SystemExceptionClass sc)
|
||||
or
|
||||
|
||||
@@ -15,7 +15,7 @@ abstract class AssertMethod extends Method {
|
||||
final Parameter getAssertedParameter() { result = this.getParameter(this.getAssertionIndex()) }
|
||||
|
||||
/** Gets the exception being thrown if the assertion fails, if any. */
|
||||
abstract ExceptionClass getExceptionClass();
|
||||
abstract Class getExceptionClass();
|
||||
}
|
||||
|
||||
/** A positive assertion method. */
|
||||
@@ -122,7 +122,7 @@ class SystemDiagnosticsDebugAssertTrueMethod extends AssertTrueMethod {
|
||||
|
||||
override int getAssertionIndex() { result = 0 }
|
||||
|
||||
override ExceptionClass getExceptionClass() {
|
||||
override Class getExceptionClass() {
|
||||
// A failing assertion generates a message box, see
|
||||
// https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.debug.assert
|
||||
none()
|
||||
@@ -182,7 +182,7 @@ class ForwarderAssertMethod extends AssertMethod {
|
||||
|
||||
override int getAssertionIndex() { result = p.getPosition() }
|
||||
|
||||
override ExceptionClass getExceptionClass() {
|
||||
override Class getExceptionClass() {
|
||||
result = this.getUnderlyingAssertMethod().getExceptionClass()
|
||||
}
|
||||
|
||||
|
||||
@@ -178,6 +178,13 @@ private predicate isMatchingConstant(Expr e, boolean value) {
|
||||
)
|
||||
}
|
||||
|
||||
private class Overflowable extends UnaryOperation {
|
||||
Overflowable() {
|
||||
not this instanceof UnaryBitwiseOperation and
|
||||
this.getType() instanceof IntegralType
|
||||
}
|
||||
}
|
||||
|
||||
/** A control flow element that is inside a `try` block. */
|
||||
private class TriedControlFlowElement extends ControlFlowElement {
|
||||
TriedControlFlowElement() { this = any(TryStmt try).getATriedElement() }
|
||||
@@ -185,20 +192,15 @@ private class TriedControlFlowElement extends ControlFlowElement {
|
||||
/**
|
||||
* Gets an exception class that is potentially thrown by this element, if any.
|
||||
*/
|
||||
ExceptionClass getAThrownException() {
|
||||
this = any(UnaryOperation uo |
|
||||
not uo instanceof UnaryBitwiseOperation and
|
||||
uo.getType() instanceof IntegralType and
|
||||
result instanceof SystemOverflowExceptionClass
|
||||
)
|
||||
Class getAThrownException() {
|
||||
this instanceof Overflowable and
|
||||
result instanceof SystemOverflowExceptionClass
|
||||
or
|
||||
this = any(CastExpr ce |
|
||||
ce.getType() instanceof IntegralType and
|
||||
result instanceof SystemOverflowExceptionClass
|
||||
or
|
||||
invalidCastCandidate(ce) and
|
||||
result instanceof SystemInvalidCastExceptionClass
|
||||
)
|
||||
this.(CastExpr).getType() instanceof IntegralType and
|
||||
result instanceof SystemOverflowExceptionClass
|
||||
or
|
||||
invalidCastCandidate(this) and
|
||||
result instanceof SystemInvalidCastExceptionClass
|
||||
or
|
||||
this instanceof Call and
|
||||
result instanceof SystemExceptionClass
|
||||
|
||||
@@ -558,7 +558,7 @@ class ThrowElement extends ControlFlowElement, DotNet::Throw, @throw_element {
|
||||
override Expr getExpr() { result = this.getChild(0) }
|
||||
|
||||
/** Gets the type of exception being thrown. */
|
||||
ExceptionClass getThrownExceptionType() {
|
||||
Class getThrownExceptionType() {
|
||||
result = getExpr().getType()
|
||||
or
|
||||
// Corner case: `throw null`
|
||||
|
||||
@@ -89,7 +89,7 @@ class VSTestAssertClass extends Class {
|
||||
}
|
||||
|
||||
/** The `Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException` class. */
|
||||
class AssertFailedExceptionClass extends ExceptionClass {
|
||||
class AssertFailedExceptionClass extends Class {
|
||||
AssertFailedExceptionClass() {
|
||||
this.getNamespace() instanceof VSTestNamespace and
|
||||
this.hasName("AssertFailedException")
|
||||
|
||||
Reference in New Issue
Block a user