- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- Administrator's Guide
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: go-security/range-memory-aliasing
Language: Go
Severity: Warning
Category: Security
Implicit memory aliasing in for loops refers to a scenario in Go programming when two or more pointers reference the same location in memory, creating unexpected side effects. This often results in a common mistake amongst Go programmers due to the ‘range’ clause.
Consider this example, where a slice of pointers is created in a loop:
data := []int{1, 2, 3}
pointers := make([]*int, 3)
for i, v := range data {
pointers[i] = &v
}
You might expect the ‘pointers’ slice to hold addresses of elements in ‘data’ slice, but that’s not the case. In each iteration of the loop, variable ‘v’ gets a new value but its memory address doesn’t change because it’s a loop variable. As a result, each element in ‘pointers’ slice points to the same memory location - the address of the loop variable ‘v’. The final value of ‘v’ is ‘3’, and since all pointers point to ‘v’, dereferencing the pointers would yield ‘3’ regardless of the pointer’s index in the slice.
To avoid implicit memory aliasing in for loops in Go, you should address the actual elements in the original data structure, like so:
data := []int{1, 2, 3}
pointers := make([]*int, 3)
for i := range data {
pointers[i] = &data[i]
}
In this example, each pointer in the ‘pointers’ slice correctly points to the respective element in the ‘data’ slice.
import (
"fmt"
)
func main() {
data := []int{1, 2, 3}
pointers := make([]*int, 3)
for i, v := range data {
pointers[i] = &v
}
}
import (
"fmt"
)
func main() {
for _, n := range []int{1, 2, 3, 4} {
v = &n
v := &n
}
}
import (
"fmt"
)
func main() {
data := []int{1, 2, 3}
pointers := make([]*int, 3)
for i := range data {
pointers[i] = &data[i]
}
}
import (
"fmt"
)
func main() {
for _, n := range []int{1, 2, 3, 4} {
fmt.Printf("%p\n", n)
v = ++n
v = foo(--n)
v := n
}
}