Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add magic number to avoid unexpected collison #235

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion agent/agent.go
Expand Up @@ -207,6 +207,14 @@ func formatBytes(val uint64) string {
}

func handle(conn io.ReadWriter, msg []byte) error {
reader := bufio.NewReader(conn)
magic, err := binary.ReadVarint(reader)
if err != nil {
return err
}
if magic != internal.MAGIC {
return fmt.Errorf("gops: invalid magic number: %v", magic)
}
switch msg[0] {
case signal.StackTrace:
return pprof.Lookup("goroutine").WriteTo(conn, 2)
Expand Down Expand Up @@ -286,7 +294,7 @@ func handle(conn io.ReadWriter, msg []byte) error {
time.Sleep(5 * time.Second)
trace.Stop()
case signal.SetGCPercent:
perc, err := binary.ReadVarint(bufio.NewReader(conn))
perc, err := binary.ReadVarint(reader)
if err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/shared.go
Expand Up @@ -306,6 +306,9 @@ func cmdLazy(addr net.TCPAddr, c byte, params ...byte) (io.Reader, error) {
return nil, err
}
buf := []byte{c}
magic := make([]byte, binary.MaxVarintLen64)
binary.PutVarint(magic, internal.MAGIC)
buf = append(buf, magic...)
buf = append(buf, params...)
if _, err := conn.Write(buf); err != nil {
return nil, err
Expand Down
5 changes: 4 additions & 1 deletion internal/internal.go
Expand Up @@ -13,7 +13,10 @@ import (
"strings"
)

const gopsConfigDirEnvKey = "GOPS_CONFIG_DIR"
const (
goipsConfigDirEnvKey = "GOPS_CONFIG_DIR"
MAGIC = 0xabcdefedcba
)

func ConfigDir() (string, error) {
if configDir := os.Getenv(gopsConfigDirEnvKey); configDir != "" {
Expand Down