Created
February 24, 2023 03:37
-
-
Save nikolaydubina/db2e98ba2398e57ebb975ff20abc116c to your computer and use it in GitHub Desktop.
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
// https://go.dev/play/p/PX6EWqZsXR4 | |
// race condition in interface cast 😵💫 | |
package main | |
import "fmt" | |
type I interface{ method() string } | |
type A struct{ s string } | |
type B struct { | |
u uint32 | |
s string | |
} | |
func (a A) method() string { return a.s } | |
func (b B) method() string { return b.s } | |
func main() { | |
a := A{s: "..."} | |
b := B{u: ^uint32(0), s: "..."} | |
var i I = a | |
go func() { | |
for { | |
i = a | |
i = b | |
} | |
}() | |
for { | |
if s := i.method(); len(s) > 3 { | |
fmt.Printf("len(s)=%d\n", len(s)) | |
fmt.Println(s) | |
return | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment