AUTOSAR C++14 Rule A18-0-2
The error state of a conversion from string to a numeric value shall be checked
Description
Rule Definition
The error state of a conversion from string to a numeric value shall be checked.
Rationale
Converting strings to a numeric value might result in error conditions, for instance, when the input string:
Does not contain a number
Contains a number, but is out of range
Contains additional data after a number
If you use C standard library functions such as atoi()
,
the preceding input errors might result in undefined behavior. To avoid undefined behavior
and undetected errors, check the error state of output when converting strings to a numeric
value. Avoid using C standard library function. Use C++ standard library functions, such as
std::stoi()
, std::stof()
, and
std::stol()
instead.
Polyspace Implementation
Polyspace® flags the C standard library string-to-number functions of
atoi()
, atol()
, and atof()
.
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Language Support Library |
Category: Required, Automated |
Version History
Introduced in R2019a