Noexcept function might exit with an exception
Functions specified as noexcept
,
noexcept(true)
or noexcept(<true condition>)
exits with an exception, which causes abnormal termination of program execution, leading to
resource leak and security vulnerability
Since R2020b
Description
This defect occurs when a callable entity that
is specified by using noexcept
, noexcept(true)
, or
noexcept(<true condition>)
might exit with an exception.
When a callable entity invokes other callable entities, Polyspace® makes certain assumptions to calculate whether there might be unhandled exceptions.
Functions: When a
noexcept
function calls another function, Polyspace checks whether the called function might raise an exception only if it is specified asnoexcept(<false>)
.If the called function is specified asnoexcept
, Polyspace assumes that it does not raise an exception. Some standard library functions, such as the constructor ofstd::string
, use pointers to functions to perform memory allocation, which might raise exceptions. Because these functions are not specified asnoexcept(<false>)
, Polyspace does not flag a function that calls these standard library functions.External function: When a
noexcept
function calls an external function, Polyspace flags the function declaration if the external function is specified asnoexcept(<false>)
.Virtual function: When a function calls a virtual function, Polyspace flags the function declaration if the virtual function is specified as
noexcept(<false>)
in a derived class. For instance, if anoexcept
function calls a virtual function that is declared asnoexcept(<true>)
in the base class, andnoexcept(<false>)
in a subsequent derived class, Polyspace flags the declaration of thenoexcept
function.Pointers to function: When a
noexcept
function invokes a pointer to a function, Polyspace assumes that the pointer to function does not raise exceptions.
When analyzing whether a function raises unhandled exceptions, Polyspace ignores:
Exceptions raised in destructors
Exceptions raised in
atexit()
operationsCompiler generated functions
Polyspace also ignores the dynamic context when checking for exceptions. For instance, a function might raise unhandled exceptions that arise only in certain dynamic contexts. Polyspace flags such a function even if the exception might not be raised.
Risk
You can specify that a callable entity does not exit with an exception by specifying it
as noexcept
, noexcept(true)
, or
noexcept(<true condition>)
. The compiler omits the exception
handing process for noexcept
entities. When such an entity exits with an
exception,the compiler implicitly invokes std::terminate()
.
Depending on the hardware and software you use, the function
std::terminate()
might invoke std::abort()
to
abnormally abort the execution without unwinding the stack. If the stack is not unwound
before program termination, then the destructors of the variables in the stack are not
invoked, leading to resource leak and security vulnerabilities.
Fix
Specify functions as noexcept
or noexcept(true)
only when you know the functions raise no exceptions. If you cannot determine the exception
specification of a function, specify it by using noexcept(false)
Examples
Result Information
Group: C++ Exception |
Language: C++ |
Default: On for handwritten code, off for generated code |
Command-Line Syntax:
NOEXCEPT_FUNCTION_THROWS
|
Impact: High |
Version History
Introduced in R2020bSee Also
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)