Skip to content

Return of Stack Variable Address

Description

A function returns the address of a stack variable, which will cause unintended program behavior, typically in the form of a crash.

Examples

Insecure Code

c
int* foo() { int x; return &x; }

Secure Code

c
int* foo() { int* x = malloc(sizeof(int)); return x; }

Remediation

Instead of returning the address of a stack variable, consider dynamically allocating memory for the variable or re-designing the function to avoid returning addresses of local variables.

Rule Details

FieldValue
IDCODE-0662
CategoryGeneric
SeverityCRITICAL
CWECWE-562
ConfidenceLOW
ImpactHIGH
LikelihoodHIGH
ExploitabilityEASY
Tagsmemory-safety, stack-variable
OWASPN/A

References