Skip to content

Commit

Permalink
add magic number to avoid unexpected collison
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangbo1882 committed Nov 14, 2023
1 parent aed64a2 commit 2973de6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
10 changes: 9 additions & 1 deletion agent/agent.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 2973de6

Please sign in to comment.