AutosarC++17_10-A18.0.2¶
The library functions atof, atoi and atol from library <cstdlib> shall not be used
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
Rationale
These functions have undefined behavior associated with them when the string cannot be converted.
Since C++11 Language Standard, new numeric conversion functions (See: std::stoi, std::stol, std::stoll [15]) were introduced. They guarantee defined behavior.
Example
// $Id: A18-0-2.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdint>
#include <cstdlib>
#include <string>
std::int32_t F1(const char *str) noexcept {
return atoi(str); // Non-compliant - undefined behavior if str can not
// be converted
}
std::int32_t F2(std::string const &str) noexcept(false) {
return std::stoi(str); // Compliant - throws a std::invalid_argument
// exception if str can not be converted
}
See Also
- MISRA C++ 2008 [6]: Rule 18-0-2 The library functions atof, atoi and atol from library <cstdlib> shall not be used.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
forbidden_libheader_symbol_use |
Usage of forbidden entity from <{}>. |
None |
False |
Options¶
This rule shares the following common options: confidence, exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
included_headers¶
included_headers : bool = True
symbol_header¶
symbol_header : set[str] = set()
symbols¶
symbols : set[bauhaus.analysis.config.QualifiedName] = {'atof', 'atoi', 'atol'}
system_symbol_header¶
system_symbol_header : set[str] = {'stdlib'}
translate_header_name¶
translate_header_name : bool = True
stdlib{.h} -> cstdlib).
user_symbol_header¶
user_symbol_header : set[str] = set()