MISRA C++:2008 Rule 18-0-2
The library functions atof, atoi and atol from library <cstdlib> shall not be used
Description
Rule Definition
The library functions atof, atoi and atol from library <cstdlib> shall not be used.1
Rationale
Converting strings to a numeric value by using the functions atof
,
atoi
and atol
from the library <cstdlib> might
result in error conditions. For instance, using the preceding functions might cause an error
when the input string:
Does not contain a number
Contains a number, but is out of range
Contains additional data after a number
When using the preceding functions, failure to convert a string to a numeric value 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.
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 |
Version History
Introduced in R2013b
1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.
The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:
MISRA C:2004
MISRA C:2012
MISRA C:2023
MISRA C++:2008
MISRA C++:2023
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.