AutosarC++18_03-A27.0.4ΒΆ
C-style strings shall not be used
Required inputs: IR
| Level | required |
|---|---|
| Target | implementation |
| Enforcement | automated |
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 <list>
#include <string>
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 [10]: 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
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
This rule has no individual options.