CWE Rule 391
Description
Rule Description
[PLANNED FOR DEPRECATION. SEE MAINTENANCE NOTES AND CONSIDER CWE-252, CWE-248, OR CWE-1069.] Ignoring exceptions and other error conditions may allow an attacker to induce unexpected behavior unnoticed.
Polyspace Implementation
The rule checker checks for Errno not checked.
Examples
Errno not checked
This issue occurs when
you call a function that sets errno
to indicate
error conditions, but do not check errno
after
the call. For these functions, checking errno
is
the only reliable way to determine
if an error occurred.
Functions that set errno
on errors include:
fgetwc
,strtol
, andwcstol
.For a comprehensive list of functions, see documentation about errno.
POSIX®
errno
-setting functions such asencrypt
andsetkey
.
To see if the function call completed without errors, check errno
for
error values.
The return values of these errno
-setting
functions do not indicate errors. The return value can be one of the
following:
void
Even if an error occurs, the return value can be the same as the value from a successful call. Such return values are called in-band error indicators.
You can determine if an error occurred only by checking errno
.
For instance, strtol
converts a string to
a long integer and returns the integer. If the result of conversion
overflows, the function returns LONG_MAX
and sets errno
to ERANGE
.
However, the function can also return LONG_MAX
from
a successful conversion. Only by
checking errno
can you distinguish between an error
and a successful
conversion.
Before calling the function, set errno
to
zero.
After the function call, to see if an error occurred, compare errno
to
zero. Alternatively, compare errno
to known error
indicator values. For instance, strtol
sets errno
to ERANGE
to
indicate errors.
The error message in the Polyspace® result shows the error indicator value that you can compare to.
#include<stdio.h> #include<stdlib.h> #include<errno.h> int main(int argc, char *argv[]) { char *str, *endptr; int base; str = argv[1]; base = 10; long val = strtol(str, &endptr, base); //Noncompliant printf("Return value of strtol() = %ld\n", val); }
You are using the return value of strtol
without
checking errno
.
errno
After CallBefore calling strtol
, set
errno
to zero. After a call to strtol
, check the
return value for LONG_MIN
or LONG_MAX
and
errno
for ERANGE
.
#include<stdlib.h> #include<stdio.h> #include<errno.h> #include<limits.h> int main(int argc, char *argv[]) { char *str, *endptr; int base; str = argv[1]; base = 10; errno = 0; long val = strtol(str, &endptr, base); if((val == LONG_MIN || val == LONG_MAX) && errno == ERANGE) { printf("strtol error"); exit(EXIT_FAILURE); } printf("Return value of strtol() = %ld\n", val); }
Check Information
Category: Error Conditions, Return Values, Status Codes |
Version History
Introduced in R2024a
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 (한국어)