Why is this an issue?
<stdlib.h>
's atof
, atoi
, and atol
functions, which convert strings to numbers, have
undefined behavior when the strings cannot be converted, and should therefore be avoided.
Noncompliant code example
int converter (const char * numstr) {
return atoi(numstr); // Noncompliant
}
Compliant solution
int converter (const char * numstr) {
return strtol(numstr, NULL, 10);
}
Resources
- MISRA C:2004, 20.10 - The library functions atof, atoi and atol from library <stdlib.h> shall not be used.
- MISRA C++:2008, 18-0-2 - The library functions atof, atoi and atol from library <cstdlib> shall not be used.
- MISRA C:2012, 21.7 - The atof, atoi, atol and atoll functions of <stdlib.h> shall not be used
- CERT, ERR34-C. - Detect errors when converting a string to a number