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
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.
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)