Skip to content

Incorrect use of strncpy, stpncpy, strlcpy, or strscpy

Description

The software uses the size of a source buffer when reading from or writing to a destination buffer, which may cause it to access memory that is outside of the bounds of the buffer.

Examples

Insecure Code

c
char src[10]; char dst[5]; strncpy(dst, src, 10);

Secure Code

c
char src[10]; char dst[5]; strncpy(dst, src, 5);

Remediation

Use the size of the destination buffer instead of the source buffer when calling strncpy, stpncpy, strlcpy, or strscpy to prevent buffer overflow.

Rule Details

FieldValue
IDCODE-0511
CategoryInjection
SeverityHIGH
CWECWE-806
ConfidenceMEDIUM
ImpactHIGH
LikelihoodMEDIUM
ExploitabilityMODERATE
Tagsbuffer overflow, memory safety
OWASPN/A

References