Click to see the query in the CodeQL repository
This rule identifies calls to malloc that call strlen to determine the required buffer size, but do not allocate space for the zero terminator.
The highlighted code segment creates a buffer without ensuring it’s large enough to accommodate the copied data. This leaves the code susceptible to a buffer overflow attack, which could lead to anything from program crashes to malicious code execution.
Increase the size of the buffer being allocated by one or replace malloc, strcpy pairs with a call to strdup
CERT C Coding Standard: MEM35-C. Allocate sufficient memory for an object.
Common Weakness Enumeration: CWE-131.
Common Weakness Enumeration: CWE-120.
Common Weakness Enumeration: CWE-122.