Skip to content

Iteration over a possibly empty map

Description

Iteration over a possibly empty map is likely a bug or redundant code. This could lead to unexpected behavior or errors in the program.

Examples

Insecure Code

go
m := make(map[string]string)
for k := range m {
    fmt.Println(k)
}

Secure Code

go
m := make(map[string]string)
if len(m) > 0 {
    for k := range m {
        fmt.Println(k)
    }
}

Remediation

Check if the map is empty before iterating over it to avoid potential issues.

Rule Details

FieldValue
IDCODE-0742
CategoryGeneric
SeverityLOW
CWECWE-665
ConfidenceMEDIUM
ImpactLOW
LikelihoodLOW
ExploitabilityCOMPLEX
Tagsredundant code, empty map
OWASPN/A

References