AUTOSAR C++14 Rule A27-0-2
A C-style string shall guarantee sufficient space for data and the null terminator
Since R2020b
Description
Rule Definition
A C-style string shall guarantee sufficient space for data and the null terminator.
Rationale
C-style strings not only require space for the character data written but require one explicit character at the end for the additional null terminator. Failure to accommodate for the space required causes buffer overflow, leading to memory corruption, security vulnerabilities, and other issues.
Polyspace Implementation
The checker looks for these issues:
Use of a dangerous standard function.
This issue occurs when you use C functions such as
gets
andstrcpy
, which write data to a buffer but do not inherently provide controls on the length of data written.For a more complete list of functions and their safer alternatives, see
Use of dangerous standard function
.Buffer overflow from incorrect string format specifier.
This issue occurs when the format specifier argument for C functions such as
sscanf
leads to an overflow or underflow in the memory buffer argument.Destination buffer overflow in string manipulation.
This issue occurs when certain C string manipulation functions write to their destination buffer argument at an offset greater than the buffer size.
For instance, when calling the function
sprintf(char* buffer, const char* format)
, you use a constant stringformat
of greater size thanbuffer
.Insufficient destination buffer size
This occurs when the destination buffer in a
strcpy
operation cannot accommodate the source buffer and a null terminator. The issue is reported if the size of the source buffer is unknown.
Troubleshooting
If you expect a rule violation but Polyspace® does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: Input/output library |
Category: Advisory, Automated |