AutosarC++18_03-A18.0.2

The error state of a conversion from string to a numeric value shall be checked

Required inputs: IR

Rationale

Error conditions of a string-to-number conversion must be detected and properly handled. Such errors can happen when an input string:

  • does not contain a number;
  • contains a number, but it is out of range;
  • contains additional data after a number.

Some functions for string-to-number conversion from the C Standard library have undefined behavior when a string cannot be converted to a number, e.g. atoi() . Since the C++11 Language Standard, new numeric conversion functions are available (see: std::stoi(), std::stol(), std::stoll() [15]). These guarantee defined behavior. Moreover, errors shall be checked also for formatted input stream functions (e.g. istream::operator>>()), by using basic_ios::fail().

Example
// $Id: A18-0-2.cpp 312092 2018-03-16 15:47:01Z jan.babst $
#include <cstdint>
#include <cstdlib>
#include <iostream>
#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
}

std::uint16_t ReadFromStdin1() // non-compliant
{
  std::uint16_t a;
  std::cin >> a; // no error detection
  return a;
}

std::uint16_t ReadFromStdin2() // compliant
{
  std::uint16_t a;

  std::cin.clear(); // clear all flags
  std::cin >> a;
  if (std::cin.fail()) {
    throw std::runtime_error{"unable to read an integer"};
  }
  std::cin.clear(); // clear all flags for subsequent operations
  return a;
}
See Also
  • MISRA C++ 2008 [6]: 18-0-2: The library functions atof, atoi and atol from library <cstdlib> shall not be used.
  • SEI CERT C++ Coding Standard [9]: ERR34-C: Detect errors when converting a string to a number
  • SEI CERT C++ Coding Standard [9]: ERR62-CPP: Detect errors when converting a string to a number

Possible Messages

Key

Text

Severity

Disabled

forbidden_libheader_symbol_use

Usage of forbidden entity from <{}>.

None

False

missing_basic_ios_fail

Use basic_ios::fail() after reading from input streams.

None

False

Options

check_functions

check_functions

Type: set[bauhaus.analysis.config.QualifiedName]

Default: {'std::basic_ios::fail', 'std::basic_ios::good', 'std::basic_ios::operator bool', 'std::basic_ios::operator void *', 'std::basic_ios::operator!', 'std::ios_base::fail', 'std::ios_base::good', 'std::ios_base::operator bool', 'std::ios_base::operator void *', 'std::ios_base::operator!'}

Fully qualified names of functions to check the state of the input stream.
 

functions_to_check

functions_to_check : set[bauhaus.analysis.config.QualifiedName] = {'std::basic_istream::operator>>'}

Fully qualified names of functions after which a call to one of the check functions is required.
 

included_headers

included_headers : bool = True

Whether the rule should also look in headers included by the configured symbol_header.
 

symbol_header

symbol_header : set[str] = set()

Name of the system or user header files of which the symbols should not be used.
 

symbols

symbols : set[bauhaus.analysis.config.QualifiedName] = {'atof', 'atoi', 'atol', 'atoll'}

Names of symbols which are forbidden.
 

system_symbol_header

system_symbol_header : set[str] = {'stdlib'}

Name of the system header files of which the symbols should not be used.
 

translate_header_name

translate_header_name : bool = True

Whether to auto-translate the symbol_header (e.g. stdlib{.h} -> cstdlib).
 

user_symbol_header

user_symbol_header : set[str] = set()

Name of the user header files of which the symbols should not be used.