CWE Rule 397
Description
Rule Description
Throwing overly broad exceptions promotes complex error handling code that is more likely to contain security vulnerabilities.
Polyspace Implementation
The rule checker checks for Declaration of throw for generic exception.
Examples
Declaration of throw for generic exception
This issue occurs when a function throws a generic exception. Polyspace® checks the throw statements, dynamic exception specifications, or the documentation of a function and raises this defect if any of these conditions is true:
The function contains
throw
statements that raise generic exceptions.The dynamic exception specification contains generic exceptions.
The function is documented with a
@throw
comment that contains generic exceptions.
Polyspace considers exceptions of the class std::exception
as
generic.These are base exception classes that have many derived exception classes.
Raising generic exceptions hinders proper error handling and error recovery. For instance:
void foo(void) throw(std::exception){ //... } void caller(void){ try{ //... foo(); }catch(std::exception& e){ // Need to figure out what the error is. } }
foo()
, determining what errors might happen in foo()
is handled by caller()
. Because the exception specification of foo()
is broad, the emergence of new or unexpected exceptions might not be detected. Handling and recovering from errors in foo()
becomes more complicated for caller()
.To resolve this defect, use specific exceptions when documenting the exception in a function. Caller functions are then able to address specific exceptions by using specific error-handling code. Unexpected exceptions are easily detected.
#include<exception> #include<stdexcept> #include<iostream> void foo(void) throw(std::exception) //Noncompliant { //... } // @throw std::exception void bar() //Noncompliant { //... throw std::exception(); //Noncompliant } void foo1() { //... throw std::exception(); //Noncompliant } int main(void) { try { //... foo(); } catch(std::exception& e) { // Need to figure out what the error is. } try { //... bar(); } catch(std::exception& e) { // Need to figure out what the error is. } try { //... foo1(); } catch(std::exception& e) { // Need to figure out what the error is. } }
In this example, Polyspace flags the generic throw
specifications. For instance:
Polyspace flags the dynamic exception specification of
foo()
because it usesstd::exception
.Polyspace flags the function
bar()
because it is documented to raise the generic exceptionstd::exception
. Polyspace also flags the genericthrow
statement in this function.Polyspace flags the generic
throw
statement infoo1()
.
Because these functions raise generic exceptions, identifying the exceptions
is handled by the caller function main()
. Because the caller does not
know which specific exception are expected, it might fail to detect unexpected
exceptions.
To fix this issue, document specific exceptions that might arise from a function. This specific documentation enables the caller function to address specific exceptions. Unexpected exceptions become unhandled exceptions and are easily detected.
#include<exception> #include<stdexcept> #include<iostream> void foo(void) throw(std::overflow_error) { //... } // @throw std::invalid_argument void bar() { //... } void foo1() { //... throw std::range_error("MSG"); } int main(void) { try { //... foo(); } catch(std::overflow_error& e) { // Expecting an overflow. } try { //... bar(); } catch(std::invalid_argument& e) { // Expecting an invalid argument. } try { //... foo1(); } catch(std::range_error& e) { // Expecting a range error. } }
Check Information
Category: Error Conditions, Return Values, Status Codes |
Version History
Introduced in R2023a
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)