Skip to content

Unterminated String in strncpy or stpncpy

Description

If there is no NUL character byte in the first n bytes of the source string, strncpy() and stpncpy() do not NUL-terminate the destination buffer. If the program does not explicitly terminate the destination buffer, this will almost certainly result in information disclosure, and possibly a buffer overflow condition.

Examples

Insecure Code

c
strncpy(dst, src, 10);

Secure Code

c
strncpy(dst, src, 10); dst[10] = '\0';

Remediation

Ensure the destination buffer is explicitly NUL-terminated after calling strncpy() or stpncpy().

Rule Details

FieldValue
IDCODE-0673
CategoryInjection
SeverityHIGH
CWECWE-170
ConfidenceMEDIUM
ImpactHIGH
LikelihoodMEDIUM
ExploitabilityEASY
Tagsbuffer overflow, information disclosure
OWASPN/A

References