-
Notifications
You must be signed in to change notification settings - Fork 374
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: incorrect pointer value comparison (#1601)
This is a fix to #1569 . Thanks to @thehowl for identifying this issue and providing a thorough and insightful analysis. Here is an further analysis based on #1569 : ```go package main func main() { { b := []byte("ABCDEFGHIJKL") a := b println(&a[0] == &a[0]) } } ``` this comparison would be false too. The root cause for this is the way pointer values is obtained and compared, e.g. ```go package main func main() { c := []byte{'A'} a := c println(&c[0]) println(&c[0] == &a[0]) } ``` in this code snippet, the address of the c[0], (&c[0]) is obtained from this: ```go ev := fillValueTV(store, &av.List[ii]) // by reference ``` that is a reference to the element of the underlying list of arrayValue, in case like this: ```go package main func main() { { b := []byte("ABCDEFGHIJKL") a := b println(&a[0] == &a[0]) } } ``` the address is obtained by ```go bv := &TypedValue{ // heap alloc, so need to compare value rather than pointer in isEql(), line 482 T: DataByteType, V: DataByteValue{ Base: av, Index: ii, ElemType: et, }, } ``` that is a new allocated *TV, which implies the value of it would not be same within the first and second & operation. So we should actually compare the concrete value rather than the pointers for this case.
- Loading branch information
1 parent
3b95486
commit ae37e84
Showing
9 changed files
with
108 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package main | ||
|
||
type S struct { | ||
i int | ||
} | ||
|
||
func main() { | ||
sArr := make([]S, 0, 4) | ||
sArr = append(sArr, S{1}, S{2}, S{3}) | ||
|
||
newArr := append(sArr[:0], sArr[0:]...) | ||
|
||
// share same underlying array | ||
println(&sArr[0] == &newArr[0]) | ||
|
||
println(&sArr[1] == &newArr[1]) | ||
|
||
println(&sArr[2] == &newArr[2]) | ||
} | ||
|
||
// Output: | ||
// true | ||
// true | ||
// true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package main | ||
|
||
func main() { | ||
s1 := []int{1, 2} | ||
i0 := &s1[0] | ||
// new array allocated, so they have different base array | ||
s1 = append(s1, 3) | ||
ii0 := &s1[0] | ||
println(i0 == ii0) | ||
} | ||
|
||
// Output: | ||
// false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package main | ||
|
||
func main() { | ||
{ | ||
b := []byte("ABCDEFGHIJKL") | ||
a := b | ||
println(&b[0] == &a[0], b[0], a[0]) | ||
|
||
// modifying the underlying array modifies both a[0] and b[0], | ||
// as it should | ||
a[0] = 11 | ||
println(a[0], b[0]) | ||
} | ||
|
||
{ | ||
b := []byte{1, 2, 3} | ||
a := b | ||
println(&b[0] == &a[0], b[0], a[0]) | ||
|
||
// modifying the underlying array modifies both a[0] and b[0], | ||
// as it should | ||
a[0] = 11 | ||
println(a[0], b[0]) | ||
} | ||
} | ||
|
||
// Output: | ||
// true 65 65 | ||
// 11 11 | ||
// true 1 1 | ||
// 11 11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package main | ||
|
||
func main() { | ||
{ | ||
b := []byte("ABCDEFGHIJKL") | ||
a := b | ||
println(&a[0] == &a[0]) | ||
} | ||
} | ||
|
||
// Output: | ||
// true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package main | ||
|
||
func main() { | ||
c := []byte{'A'} | ||
a := c | ||
println(&c[0]) | ||
println(&c[0] == &a[0]) | ||
} | ||
|
||
// Output: | ||
// &(65 uint8) | ||
// true |