Many library functions accept a string or wide string argument with the constraint that the string they receive is properly null-terminated. Passing a character sequence or wide character sequence that is not null-terminated to such a function can result in accessing memory that is outside the bounds of the object. Do not pass a character sequence or wide character sequence that is not null-terminated to a library function that expects a string or wide string argument.
Noncompliant Code Example
This code example is noncompliant because the character sequence
c_str will not be null-terminated when passed as an argument to
printf(). (See STR11-C. Do not specify the bound of a character array initialized with a string literal on how to properly initialize character arrays.)
This compliant solution does not specify the bound of the character array in the array declaration. If the array bound is omitted, the compiler allocates sufficient storage to store the entire string literal, including the terminating null character.
Noncompliant Code Example
This code example is noncompliant because the wide character sequence
cur_msg will not be null-terminated when passed to
wcslen(). This will occur if
lessen_memory_usage() is invoked while
cur_msg_size still has its initial value of 1024.
In this compliant solution,
cur_msg will always be null-terminated when passed to
Noncompliant Code Example (
strncpy() function takes a string as input, it does not guarantee that the resulting value is still null-terminated. In the following noncompliant code example, if no null character is contained in the first
n characters of the
source array, the result will not be null-terminated. Passing a non-null-terminated character sequence to
strlen() is undefined behavior.
Compliant Solution (Truncation)
This compliant solution is correct if the programmer's intent is to truncate the string:
Compliant Solution (Truncation, strncpy_s())
The C Standard, Annex K
strncpy_s() function can also be used to copy with truncation. The
strncpy_s() function copies up to
n characters from the source array to a destination array. If no null character was copied from the source array, then the
nth position in the destination array is set to a null character, guaranteeing that the resulting string is null-terminated.
Compliant Solution (Copy without Truncation)
If the programmer's intent is to copy without truncation, this compliant solution copies the data and guarantees that the resulting array is null-terminated. If the string cannot be copied, it is handled as an error condition.
Note that this code is not bulletproof. It gracefully handles the case where
source is NULL, when it is a valid string, and when
source is not null-terminated, but at least the first 32 bytes are valid. However, in cases where
source is not NULL, but points to invalid memory, or any of the first 32 bytes are invalid memory, the first call to
strnlen() will access this invalid memory, and the resulting behavior is undefined. Unfortunately, standard C provides no way to prevent or even detect this condition without some external knowledge about the memory
source points to.
Failure to properly null-terminate a character sequence that is passed to a library function that expects a string can result in buffer overflows and the execution of arbitrary code with the permissions of the vulnerable process. Null-termination errors can also result in unintended information disclosure.
Astrée supports the implementation of library stubs to fully verify this guideline.
|Axivion Bauhaus Suite|
|CertC-STR32||Partially implemented: can detect some violation of the rule|
|MISC.MEM.NTERM.CSTRING||Unterminated C String|
Can detect some violations of this rule
DF2835, DF2836, DF2839
|LDRA tool suite|
404 S, 600 S
Avoid overflow due to reading a not zero terminated string
|Polyspace Bug Finder|
Rule partially covered.
|match format and arguments||Partially verified.|
Key here (explains table format and definitions)
|ISO/IEC TR 24772:2013||String Termination [CMJ]||Prior to 2018-01-12: CERT: Unspecified Relationship|
|ISO/IEC TS 17961:2013||Passing a non-null-terminated character sequence to a library function that expects a string [strmod]||Prior to 2018-01-12: CERT: Unspecified Relationship|
|CWE 2.11||CWE-119, Improper Restriction of Operations within the Bounds of a Memory Buffer||2017-05-18: CERT: Rule subset of CWE|
|CWE 2.11||CWE-123, Write-what-where Condition||2017-06-12: CERT: Partial overlap|
|CWE 2.11||CWE-125, Out-of-bounds Read||2017-05-18: CERT: Rule subset of CWE|
|CWE 2.11||CWE-170, Improper Null Termination||2017-06-13: CERT: Exact|
CERT-CWE Mapping Notes
Key here for mapping notes
CWE-119 and STR32-C
Independent( ARR30-C, ARR38-C, ARR32-C, INT30-C, INT31-C, EXP39-C, EXP33-C, FIO37-C) STR31-C = Subset( Union( ARR30-C, ARR38-C)) STR32-C = Subset( ARR38-C)
CWE-119 = Union( STR32-C, list) where list =
- Out-of-bounds reads or writes that do not involve non-null-terminated byte strings.
CWE-125 and STR32-C
Independent( ARR30-C, ARR38-C, EXP39-C, INT30-C) STR31-C = Subset( Union( ARR30-C, ARR38-C)) STR32-C = Subset( ARR38-C)
CWE-125 = Union( STR32-C, list) where list =
- Out-of-bounds reads that do not involve non-null-terminated byte strings.
CWE-123 and STR32-C
Independent(ARR30-C, ARR38-C) STR31-C = Subset( Union( ARR30-C, ARR38-C)) STR32-C = Subset( ARR38-C)
Intersection( CWE-123, STR32-C) =
- Buffer overflow from passing a non-null-terminated byte string to a standard C library copying function that expects null termination, and that overwrites an (unrelated) pointer
STR32-C - CWE-123 =
- Buffer overflow from passing a non-null-terminated byte string to a standard C library copying function that expects null termination, but it does not overwrite an (unrelated) pointer
CWE-123 – STR31-C =
- Arbitrary writes that do not involve standard C library copying functions, such as strcpy()