Skip to content

Incorrect use of sizeof() on malloced pointer type

Description

The code calls sizeof() on a malloced pointer type, which always returns the wordsize/8. This can produce an unexpected result if the programmer intended to determine how much memory has been allocated.

Examples

Insecure Code

c
int *ptr = malloc(10); size_t size = sizeof(ptr);

Secure Code

c
int *ptr = malloc(10); size_t size = 10;

Remediation

Use a different method to determine the allocated memory size, such as maintaining a separate variable to track the allocated size.

Rule Details

FieldValue
IDCODE-0186
CategoryGeneric
SeverityMEDIUM
CWECWE-467
ConfidenceMEDIUM
ImpactMEDIUM
LikelihoodMEDIUM
ExploitabilityMODERATE
Tagssizeof, malloc
OWASPN/A

References