Main Content

CERT C++: MSC37-C

Ensure that control never reaches the end of a non-void function

Description

Rule Definition

Ensure that control never reaches the end of a non-void function.1

Polyspace Implementation

The rule checker checks for Missing return statement.

Examples

expand all

Issue

Missing return statement occurs when a function does not return a value along at least one execution path. If the return type of the function is void, this error does not occur.

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.

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 show a sequence of events that indicate this execution path. You can add a return statement at an appropriate point in the path. If the result details do not show the event history, you can trace back using right-click options in the source code and see previous related events. See also Interpret Bug Finder Results in Polyspace Desktop User Interface.

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:

Example - Missing or invalid return statement error
int AddSquares(int n)
 {
   int i=0;
   int sum=0;
   
   if(n!=0) 
    {
     for(i=1;i<=n;i++)
        {
         sum+=i^2;
        }
     return(sum);
    }
 }  //Noncompliant
/* Defect: No return value if n is not 0*/

If n is equal to 0, the code does not enter the if statement. Therefore, the function AddSquares does not return a value if n is 0.

Correction — Place Return Statement on Every Execution Path

One possible correction is to return a value in every branch of the if...else statement.

 int AddSquares(int n)
 {
   int i=0;
   int sum=0;
   
   if(n!=0) 
    {
     for(i=1;i<=n;i++)
        {
         sum+=i^2;
        }
     return(sum);
    } 
   
   /*Fix: Place a return statement on branches of if-else */
   else 
     return 0;  
  }

Check Information

Group: 49. Miscellaneous (MSC)

Version History

Introduced in R2019a


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.