Missing return statement
Function with non-void return type does not return value on some
paths
Description
This defect occurs when a function does not return a value on at least one execution path. This defect does not occur if:
The return type of the function is
void.The execution path is terminated by a function that does not return the flow of execution, such as a
[[noreturn]]function.If you use C version above C99 or C++. From C version C99 and above, as well as for C++, the
mainfunction implicitly returns a0if a return value is not specified.
Risk
If a function has a non-void return value in its signature, it
is expected to return a value. The return value of this function can be used in
later computations. If the execution of the function body goes through a path where
a return statement is missing, the function return value is
indeterminate. Computations with this return value can lead to unpredictable
results.
Fix
In most cases, you can fix this defect by
placing the return statement at the end of the function body. If
your code has execution paths that do not return the flow of execution, specify them
by using the attribute [[noreturn]].
Alternatively, you can identify which execution paths through the function body do
not have a return statement and add a return
statement on those paths. Often the result details (or source code tooltips in
Polyspace®
as You Code™) show a sequence of events that led to the defect. You can implement
the fix on any event in the sequence. If the result details do not show this event
history, you can search for previous references of variables relevant to the defect
using right-click options in the source code and find related events. See also Interpret Polyspace Bug Finder Results in Polyspace Platform User Interface or Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access).
See examples of fixes below.
If you do not want to fix the issue, add comments to your result or code to avoid another review. See:
Address Polyspace Results Through Bug Fixes or Justifications if you review results in the Polyspace user interface.
Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access) if you review results in a web browser.
Annotate Code and Hide Known or Acceptable Results if you review results in an IDE.
If the analysis flags a missing return statement on a path
where a process termination function exists, you can make the analysis aware of the
process termination function using the option -termination-functions.
Examples
Result Information
| Group: Data flow |
| Language: C | C++ |
| Default: On |
Command-Line Syntax:
MISSING_RETURN
|
| Impact: Low |
PQL Name:
std.defects.MISSING_RETURN
|
Version History
Introduced in R2013bSee Also
Topics
- Interpret Polyspace Bug Finder Results in Polyspace Platform User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Polyspace Results Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)