...
The atoi(), atol(), atoll(), and atof() functions convert the initial portion of a string token to int, long int, long long int, and double representation representation, respectively. Except for the behavior on error ([ISO/IEC 9899:2024], s7.24.1.2), they are equivalent to
| Code Block |
|---|
atoi: (int)strtol(nptr, (char **)NULL, 10) atol: strtol(nptr, (char **)NULL, 10) atoll: strtoll(nptr, (char **)NULL, 10) atof: strtod(nptr, (char **)NULL) |
...
- do not need to set
errnoon an error; - have have undefined behavior 16 if the value of the result cannot be represented;
- return 0 (or 0.0) if the string does not represent an integer (or decimal), which is indistinguishable from a correctly formatted, zero-denoting input string.
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
void func(const char *buff) {
char *end;
int si;
errno = 0;
const long sl = strtol(buff, &end, 10);
if (end == buff) {
(void) fprintf(stderr, "%s: not a decimal number\n", buff);
} else if ('\0' != *end) {
(void) fprintf(stderr, "%s: extra characters at end of input: %s\n", buff, end);
} else if ((LONG_MIN == sl || LONG_MAX == sl) && ERANGE == errno) {
(void) fprintf(stderr, "%s out of range of type long\n", buff);
} else if (sl > INT_MAX) {
(void) fprintf(stderr, "%ld greater than INT_MAX\n", sl);
} else if (sl < INT_MIN) {
(void) fprintf(stderr, "%ld less than INT_MIN\n", sl);
} else {
si = (int)sl;
/* Process si */
}
}
|
...
Recommendation | Severity | Likelihood | Detectable | Remediation CostRepairable | Priority | Level |
|---|---|---|---|---|---|---|
ERR34-C | Medium | Unlikely | Yes | YesMedium | P4P6 | L3L2 |
Automated Detection
Tool | Version | Checker | Description | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Axivion Bauhaus Suite |
| CertC-ERR34 | |||||||||||||||||
| Clang |
| cert-err34-c | Checked by clang-tidy | ||||||||||||||||
| CodeSonar |
| BADFUNC.ATOF (customization) | Use of atof Users can add custom checks for uses of other undesirable conversion functions. | ||||||||||||||||
| Compass/ROSE | Can detect violations of this recommendation by flagging invocations of the following functions:
| ||||||||||||||||||
| Helix QAC |
| C5030 C++5016 | |||||||||||||||||
| Klocwork |
| CERT.ERR.CONV.STR_TO_NUM | |||||||||||||||||
| LDRA tool suite |
| 44 S | Fully implemented | ||||||||||||||||
| Parasoft C/C++test |
| CERT_C-ERR34-a | The library functions 'atof', 'atoi and atol from library ', 'atol' and 'atoll' functions from the 'stdlib.h shall ' or 'cstdlib' library should not be used | ||||||||||||||||
| PC-lint Plus |
| 586 | Assistance provided | ||||||||||||||||
| CERT C: Rule ERR34-C | Checks for unsafe conversion from string to numeric value (rule fully covered) | PRQA QA-C | ||||||||||||||||
| Include Page | PRQA QA-C_v | PRQA QA-C_v5030 | Partially implemented | PRQA QA-C++ | |||||||||||||||
| Include Page | cplusplus:PRQA QA-C++_V | cplusplus:PRQA QA-C++_V | |||||||||||||||||
5016 | SonarQube C/C++ Plugin |
| S989 |
...
CWE-391 = Union( ERR34-C, list) where list =
- Failure to errors outside of string-to-number conversion functions
CWE-676 and ERR34-C
- Independent( ENV33-C, CON33-C, STR31-C, EXP33-C, MSC30-C, ERR34-C)
- ERR34-C implies that string-parsing functions (eg atoi() and scanf()) are dangerous.
- CWE-676 = Union( ERR34-C, list) where list =
- Invocation of dangerous functions besides the following:
- atoi(), atol(), atoll(), atof(), The scanf()family
CWE-758 and ERR34-C
Independent( INT34-C, INT36-C, MSC37-C, FLP32-C, EXP33-C, EXP30-C, ERR34-C, ARR32-C)
Intersection( CWE-758, ERR34-C) =
- Undefined behavior arising from a non-representable numeric value being parsed by an ato*() or scanf() function
CWE-758 – ERR34-C =
- Undefined behavior arising from using a function outside of the ato*() or scanf() family
ERR34-C – CWE-758 =
- The ato*() or scanf() family receives input that is not a number when trying to parse one
Bibliography
| [ISO/IEC 9899:20112024] | Subclause 7.2224.1, "Numeric conversion functions" Subclause 7.21.6, "Formatted input/output functions" |
| [Klein 2002] |
...