# Concurrent Slice Access - ID: go-concurrent-slice-access - Severity: HIGH - CWE: Race Condition (CWE-362) - Languages: Go ## Description Concurrent access to slices (especially append) without synchronization can cause data races, lost data, or panics. Slices in Go are not thread-safe. ## Detection Message Concurrent slice access without synchronization causes data races. append() is NOT atomic - multiple goroutines appending can: - Overwrite each other's data - Cause lost elements - Corrupt slice header ## Remediation Option 1: Protect with mutex ```go var mu sync.Mutex var results []string go func() { mu.Lock() results = append(results, item) mu.Unlock() }() ``` Option 2: Use channels (preferred for Go) ```go resultsCh := make(chan string, n) for i := 0; i < n; i++ { go func() { resultsCh <- processItem() }() } // Collect safely var results []string for i := 0; i < n; i++ { results = append(results, <-resultsCh) } ``` Option 3: Pre-allocate with unique indices ```go results := make([]string, n) // Pre-allocate for i := 0; i < n; i++ { i := i // Capture loop variable go func() { results[i] = process() // Safe: unique index per goroutine }() } ``` ## Documentation [object Object] ## Related Rules - **Direct Map Access on Thread-Safe Struct** [HIGH]: - **Potential Race Condition** [MEDIUM]: - **WaitGroup Misuse** [HIGH]: - **Race Condition in Concurrent Operations** [HIGH]: - **Potential Race Condition** [MEDIUM]: