Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

These functions truncate strings that exceed the specified limits. Additionally, some functions, such as strncpy(), do not guarantee that the resulting string character sequence is null-terminated. (See rule STR32-C. Null-terminate strings as requiredDo 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.

...

The standard functions strncpy() and strncat() copy a specified number of characters n from a source string to a destination array. If 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.

Code Block
bgColor#FFcccc
langc

char *string_data;
char a[16];
/* ... */
strncpy(a, string_data, sizeof(a));

...

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.

Code Block
bgColor#ccccff
langc

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.

Compliant Solution

...

(strcpy_s(), C11 Annex K)

The Wiki MarkupThe {{strcpy_s()}} function defined in \[in C11 Annex K  [ISO/IEC TR 24731-1:2007|AA. Bibliography#ISO/IEC TR 24731-1-2007]\] provides additional safeguards, including accepting the size of the destination buffer as an additional argument. (See recommendation [9899:2011] provides additional safeguards, including accepting the size of the destination buffer as an additional argument. (See STR07-C. Use TR 24731 for remediation of existing string manipulation code].) Also {{Use the bounds-checking interfaces for string manipulation.) Also, strnlen_s()}} accepts a maximum-length argument for strings that may not be null-terminated.

Code Block
bgColor#ccccff
langc

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

/* ... */

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

If a runtime-constraint error is detected by either the call to either strnlen_s() or strcpy_s(), the currently registered runtime-constraint handler is invoked. See recommendation ERR03-C. Use runtime-constraint handlers when calling functions defined by TR24731-1the bounds-checking interfaces for more information on using runtime-constraint handlers with TR 24731-1 C11 Annex K functions.

Exceptions

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

Risk Assessment

Truncating strings can lead to a loss of data.

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

STR03-C

medium

Medium

probable

Probable

medium

Medium

P8

L2

Automated Detection

LDRA tool suite

Tool

Version

Checker

Description

Section
CodeSonar
Include Page
c:LDRA
CodeSonar_V
c:LDRA
CodeSonar_V

 

 

Section

Fortify SCA

Section

V. 5.0

 

Section

can detect violations of this rule with CERT C Rule Pack

MISC.MEM.NTERMNo Space For Null Terminator
section Sectioncould
Compass/ROSE

 

 



Could detect violations in the following manner:

All

all calls to strncpy() and the other functions should be

follwed

followed by an assignment of a terminating character to null-terminate the string

section

GCC8.1-Wstringop-truncationDetects string truncation by strncat and strncpy.
Klocwork
Include Page
c:
Klocwork_V
c:
Klocwork_V
Section

NNTS

NNTS.MIGHT
NNTS.MUST


LDRA tool suite
Include Page
LDRA_V
LDRA_V

115 S, 44 S

Partially implemented

Parasoft C/C++test
Include Page
Parasoft_V
Parasoft_V

CERT_C-STR03-a

Avoid overflow due to reading a not zero terminated string

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V
Polyspace Bug Finder_V

CERT C: Rec. STR03-C

Checks for invalid use of standard library string routine (rec. partially supported)

 

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

Related Guidelines

...

...

...

ISO/IEC TR 24772 "CJM String Termination"

ISO/IEC TR 24731-1:2007

...

TR 24772:2013String Termination [CJM]
MITRE CWE

...

Improper

...

null termination
CWE-464,

...

Addition of

...

data structure sentinel

Bibliography

...

...

...

2013]Chapter 2,

...

"Strings"


...

Image Removed      07. Characters and Strings (STR)      Image Added Image Added Image Modified