Main Content

CERT C++: MSC53-CPP

Do not return from a function declared [[noreturn]]

Since R2020b

Description

Rule Definition

Do not return from a function declared [[noreturn]]1

Polyspace Implementation

The rule checker checks for [[noreturn]] functions returning to caller.

Examples

expand all

Issue

This defect occurs when a [[noreturn]] function eventually returns the flow of execution to the caller function. The compiler expects that a function declared by using the [[noreturn]] attribute does not return the flow of execution. That is, if a [[noreturn]] function f() is called from main(), then the compiler expects that the flow of execution is not returned to main(). If such a function eventually returns the flow of execution, it leads to undefined behavior.

Risk

If a [[noreturn]] function eventually returns the flow of execution, it leads to undefined behavior, which can be exploited to cause data integrity violations.

Fix

If a function has no return statement, then the final closing brace of a function implies an implicit return. Omitting a return statement in the function does not prevent the flow of execution from returning. A [[noreturn]] function can prohibit returning the flow of execution to the calling function by:

  • Entering an infinite loop

  • Raising an exception

  • Calling another [[noreturn]] function

Example

Consider the following code containing the function noncompliant(), which is declared as [[noreturn]].

#include <cstdlib>
[[noreturn]] void bad_f(int i)
{
	if (i > 0)
	throw "Received positive input";
	else if (i < 0)
	std::exit(0);
} //Noncompliant

When the input i is zero, the flow of execution skips the if-else-if block of code and returns to the caller implicitly. Because the [[noreturn]] function returns the flow of execution in a code path, this function is noncompliant with this rule.

Correction

A [[noreturn]] function must not return the flow of execution in a code path. You can prevent returning in several ways. Consider the following code where the [[noreturn]] function does not return the flow of execution in a code path.

#include <cstdlib>
[[noreturn]] void compliant(int i)
{
	if (i > 0)
	throw "Received positive input";
	else if (i < 0)
	std::exit(0);
	else if(i==0)
	while(true){
		//...
	}
}//Compliant

This function is compliant with this rule because:

  • When i > 0, the function raises an exception.

  • When i < 0, the function calls the [[noreturn]] function std::exit().

  • When i==0, the function enters an infinite loop.

Because the [[noreturn]] function does not return the flow of execution in a call path, it is compliant with this rule.

Check Information

Group: 49. Miscellaneous (MSC)

Version History

Introduced in R2020b


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.