CodeQL documentation

Dead code due to goto or break statement

ID: cpp/dead-code-goto
Kind: problem
Severity: warning
Precision: high
Tags:
   - maintainability
   - external/cwe/cwe-561
Query suites:
   - cpp-security-and-quality.qls

Click to see the query in the CodeQL repository

Code immediately following a goto or break 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 the goto or break statement to after the code. Otherwise, delete the unreachable code.

Example

goto err1;
free(pointer); // BAD: this line is unreachable
err1: return -1;

free(pointer); // GOOD: this line is reachable
goto err2;
err2: return -1;

References