CertC-STR03

Do not inadvertently truncate a string

Required inputs: IR

Alternative functions that limit the number of bytes copied are often recommended to mitigate buffer overflow vulnerabilities . Examples include

  • strncpy() instead of strcpy()
  • strncat() instead of strcat()
  • fgets() instead of gets()
  • snprintf() instead of sprintf()

These functions truncate strings that exceed the specified limits. Additionally, some functions, such as strncpy() , do not guarantee that the resulting character sequence is null-terminated. (See STR32-C. Do not pass a non-null-terminated character sequence to a library function that expects a string .)

Unintentional truncation results in a loss of data and in some cases leads to software vulnerabilities.

Noncompliant Code Example

The standard functions strncpy() and strncat() copy a specified number of characters n from a source string to a destination array. In the case of strncpy() , if there is no null character in the first n characters of the source array, the result will not be null-terminated and any remaining characters are truncated.

char *string_data;
char a[16];
/* ... */
strncpy(a, string_data, sizeof(a));
Compliant Solution (Adequate Space)

Either the strcpy() or strncpy() function can be used to copy a string and a null character to a destination buffer, provided there is enough space. The programmer must be careful to ensure that the destination buffer is large enough to hold the string to be copied and the null byte to prevent errors, such as data truncation and buffer overflow.

char *string_data = NULL;
char a[16];

/* ... */

if (string_data == NULL) {
  /* Handle null pointer error */
}
else if (strlen(string_data) >= sizeof(a)) {
  /* Handle overlong string error */
}
else {
  strcpy(a, string_data);
}

This solution requires that string_data is null-terminated; that is, a null byte can be found within the bounds of the referenced character array. Otherwise, strlen() will stray into other objects before finding a null byte.

Exceptions

STR03-C-EX1: The intent of the programmer is to purposely truncate the string.

Risk Assessment

Truncating strings can lead to a loss of data.

Recommendation Severity Likelihood Detectable Repairable Priority Level
STR03-C Medium Probable No No P4 L3
Related Guidelines
SEI CERT C++ Coding Standard VOID STR03-CPP. Do not inadvertently truncate a null-terminated character array
ISO/IEC TR 24772:2013 String Termination [CJM]
MITRE CWE CWE-170 , Improper null termination
CWE-464 , Addition of data structure sentinel
Bibliography
[ Seacord 2013 ] Chapter 2, "Strings"
Excerpt from SEI CERT C Coding Standard: Rules for Developing Safe, Reliable, and Secure Systems (2016 Edition) and SEI CERT C Coding Standard [https://cmu-sei.github.io/secure-coding-standards/sei-cert-c-coding-standard/recommendations/characters-and-strings-str/str03-c], Copyright (C) 1995-2026 Carnegie Mellon University. See 9.4 3rd-Party Licenses in the documentation for full details.

Possible Messages

Key

Text

Severity

Disabled

literal_truncation

The string literal is longer than the size argument and is truncated

None

False

unchecked_truncation

The string may be truncated, the length of the source is not checked before the call

None

False

Options

length_functions

length_functions : list[str] = ['strlen', 'strnlen', 'strnlen_s', 'wcslen', 'wcsnlen', 'wcsnlen_s']

Functions whose call on the source string counts as a length check.
 

truncating_functions

truncating_functions : list[str] = ['strncpy', 'strncat', 'wcsncpy', 'wcsncat']

Functions that truncate the source string to the given size.