CWE Rule 396
Description
Rule Description
Catching 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 catch for generic exception.
Examples
Declaration of catch for generic exception
This issue occurs when you design catch
blocks to handle generic high
level exceptions. Polyspace® checks the exception specifications of a function and raises a violation if a
catch block handles:
All exceptions by using a catch-all block.
Exception of the class
std::exception
. Becausestd::exception
is the base class for all standard C++ exceptions, a catch block that handlesstd::exception
type matches all derived exceptions.
Using generic catch blocks hides the emergence of unexpected new exceptions and hinders their proper handling. Such generic catch blocks makes the code vulnerable to security issues. For instance:
void foo(void){ try{ //.. }catch(std::exception& e){ //Log error } }
catch
block handles the generic std::exception
class, this code
hides unexpected exceptions or those that need to be handled differently. For instance, If an unexpected std::invalid_argument
exception is raised in foo()
, it might remain undetected by the developer because it is matched with the catch block. Because the catch block is not programmed to handle std::invalid_argument
properly, the poorly handled exception becomes a vulnerability for the code.To fix this defect, avoid catching high-level generic exceptions. Write catch blocks that handle specific exceptions to enable handling different exceptions in different ways. Unexpected or new exceptions are also easily detected when catch blocks are specific.
#include<string> #include<exception> #include<stdexcept> extern void logger(std::string s, std::exception& e); void foo(void){ try{ //.. }catch(std::exception& e){ //Noncompliant logger("foo failed", e); } }
In this example, Polyspace flags the use of the generic catch
block.
To resolve this defect, use specific catch blocks. When you use specific catch blocks, new and unexpected exceptions become unhandled exceptions that are easily detected.
#include<string> #include<exception> extern void logger(std::string s, std::exception& e); void foo(void){ try{ //.. }catch(std::domain_error& e){ logger("Domain Error in foo", e); }catch(std::invalid_argument& e){ logger("Invalid Argument in foo", e); } }
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 (한국어)