Dead code due to goto or break statement¶
ID: cpp/dead-code-gotoKind: problemSecurity severity: Severity: warningPrecision: highTags: - maintainability - external/cwe/cwe-561Query suites: - cpp-security-and-quality.qls
Click to see the query in the CodeQL repository
Code immediately following agoto orbreak statement will not be executed, unless there is a label or switch case. When the code is necessary, this leads to logical errors or resource leaks. If the code is unnecessary, it may confuse readers.
Recommendation¶
If the unreachable code is necessary, move thegoto orbreak statement to after the code. Otherwise, delete the unreachable code.
Example¶
gotoerr1;free(pointer);// BAD: this line is unreachableerr1:return-1;free(pointer);// GOOD: this line is reachablegotoerr2;err2:return-1;
References¶
The CERT C Secure Coding Standard:MSC12-C. Detect and remove code that has no effect or is never executed.
Common Weakness Enumeration:CWE-561.