...
Although the 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 196.
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <string.h>
enum { STR_SIZE = 32 };
size_t func(const char *source) {
char c_str[STR_SIZE];
size_t ret = 0;
if (source) {
c_str[sizeof(c_str) - 1] = '\0';
strncpy(c_str, source, sizeof(c_str));
ret = strlen(c_str);
} else {
/* Handle null pointer */
}
return ret;
}
|
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
#include <string.h>
enum { STR_SIZE = 32 };
size_t func(const char *source) {
char c_str[STR_SIZE];
size_t ret = 0;
if (source) {
strncpy(c_str, source, sizeof(c_str) - 1);
c_str[sizeof(c_str) - 1] = '\0';
ret = strlen(c_str);
} else {
/* Handle null pointer */
}
return ret;
} |
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.
| Code Block | ||||
|---|---|---|---|---|
| ||||
#define __STDC_WANT_LIB_EXT1__ 1 #include <string.h> #include <string.h> enum { STR_SIZE = 32 }; size_t func(const char *source) { char ac_str[STR_SIZE]; size_t ret = 0; if (source) { if errno_t err = strncpy_s((strnlen(source, sizeof(c_str)) < sizeof(c_str)) { astrcpy(c_str, sizeof(a), source, strlen(source) source); ret = strlen(c_str); if (err != 0)} else { /* Handle errorstring-too-large */ } } else { /* Handle retnull = strnlen_s(a, sizeof(a)); } } else { /* Handle null pointer *pointer */ } return ret; } |
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.
...
| bgColor | #ccccff |
|---|---|
| lang | c |
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.
...
Risk Assessment
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.
Rule | Severity | Likelihood | Detectable | Remediation CostRepairable | Priority | Level |
|---|---|---|---|---|---|---|
STR32-C | High | Probable | No | MediumYes | P12 | L1 |
Automated Detection
Tool | Version | Checker | Description | |||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Astrée |
| stdlib-string-termination | Partially checked + soundly supported | |||||||||||||||||||||||
| Axivion Bauhaus Suite |
| CertC-STR32 | Partially implemented: can detect some violation of the rule | |||||||||||||||||||||||
| CodeSonar |
| MISC.MEM.NTERM.CSTRING | Unterminated C String | |||||||||||||||||||||||
| Compass/ROSE | Can detect some violations of this rule | |||||||||||||||||||||||||
| Coverity |
| STRING_NULL | Fully | implementedimplemented | ||||||||||||||||||||||
| Cppcheck Premium |
| premium-cert-str32-c | ||||||||||||||||||||||||
| Helix QAC |
| C2835DF2835, | C2836, C2839C++2835, C++2836, C++2839DF2836, DF2839 | |||||||||||||||||||||||
| Klocwork |
| NNTS.MIGHT | ||||||||||||||||||||||||
| LDRA tool suite |
| 404 S, 600 S | Partially implemented | |||||||||||||||||||||||
| Parasoft C/C++test |
| CERT_C-STR32-a | Avoid overflow due to reading a not zero terminated string | |||||||||||||||||||||||
| Polyspace Bug Finder |
| Checks for:
Rule partially covered. | PRQA QA||||||||||||||||||||||||
| PVS- | CStudio |
| PRQA QA
| C
| v
| PRQA QA
| C
| v2835, 2836, 2839 | PRQA QA-C++
| V692 | ||||||||||||||||
| RuleChecker |
| cplusplus:PRQA QA-C++
| cplusplus:PRQA QA-C++
| 0145 | stdlib-string-termination | Partially checked | ||||||||||||||||||||
| Security Reviewer - Static Reviewer | PVS-Studio
| PVS
| Studio
| PVS
| Studio
| shiftTooManyBits | V692Fully implemented | |||||||||||||||||||
| TrustInSoft Analyzer |
| match format and arguments | Partially verified. |
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
...