AutosarC++19_03-A27.0.4ΒΆ

C-style strings shall not be used

Required inputs: IR

Rationale

It is required that an underlying buffer for a C-style string needs to be of sufficient size to hold character data and a null terminator. In addition, a C-style string implies all other disadvantages of built-in arrays (see A18-1-1 in section 6.18.1). Using std::

string provides correct memory allocation, copying, gradual expansion and iteration. It is self-explanatory in terms of ownership and offers more readable interface.

Example
// $Id: A27-0-4.cpp 311495 2018-03-13 13:02:54Z michal.szczepankiewicz $
    #include <iostream>
    #include <string>
    #include <list>

    void F1()
    {
        std::string string1;
          std::string string2;
          std::cin >> string1 >> string2;    // Compliant - no buffer overflows
    }

    std::list<std::string> F2(const std::string& terminator)
    {
        std::list<std::string> ret;
        //read a single word until it is different from the given terminator sequence
        for (std::string s; std::cin >> s && s != terminator; )
        {
            ret.push_back(s);
        }
        return ret;
    }
See Also
  • C++ Core Guidelines [11]: SL.str.1: Use std::string to own character sequences.

Possible Messages

Key

Text

Severity

Disabled

c_string_used

C-style strings should not be used.

None

False

Options