91 lines
1.9 KiB
Plaintext
91 lines
1.9 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Using ``++return++``, ``++break++``, ``++throw++``, and so on from a ``++finally++`` block suppresses the propagation of any unhandled ``++Exception++`` which was thrown in the ``++try++`` or ``++catch++`` block.
|
|
|
|
|
|
This rule raises an issue when a jump statement (``++break++``, ``++continue++``, ``++return++``, ``++throw++``, and ``++goto++``) would force control flow to leave a ``++finally++`` block.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,dart]
|
|
----
|
|
class ReturnInFinally {
|
|
int nonCompliantMethod(int n) {
|
|
for (int i = 0; i < n; ++i) {
|
|
try {
|
|
functionThrowingException(i);
|
|
} catch (e) {
|
|
print(e);
|
|
} finally {
|
|
return 1; // Noncompliant
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
----
|
|
|
|
[source,dart]
|
|
----
|
|
class ContinueInFinally {
|
|
int nonCompliantMethod(int n) {
|
|
for (int i = 0; i < n; ++i) {
|
|
try {
|
|
functionThrowingException(i);
|
|
} catch (e) {
|
|
print(e);
|
|
} finally {
|
|
continue; // Noncompliant
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
----
|
|
|
|
[source,dart]
|
|
----
|
|
class BreakInFinally {
|
|
int nonCompliantMethod(int n) {
|
|
for (int i = 0; i < n; ++i) {
|
|
try {
|
|
functionThrowingException(i);
|
|
} catch (e) {
|
|
print(e);
|
|
} finally {
|
|
break; // Noncompliant
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,dart]
|
|
----
|
|
class Ok {
|
|
int nonCompliantMethod(int n) {
|
|
for (int i = 0; i < n; ++i) {
|
|
try {
|
|
functionThrowingException(i);
|
|
} catch (e) {
|
|
print(e);
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
== Resources
|
|
|
|
* https://dart.dev/tools/linter-rules/control_flow_in_finally[Dart Lint rule]
|
|
* CWE - https://cwe.mitre.org/data/definitions/584[CWE-584 - Return Inside Finally Block]
|
|
* https://wiki.sei.cmu.edu/confluence/x/BTdGBQ[CERT, ERR04-J.] - Do not complete abruptly from a finally block
|