1
0
mirror of https://github.com/google/gops.git synced 2024-11-24 08:22:25 +02:00

add magic number to avoid unexpected collison

This commit is contained in:
Zhang Bo 2023-11-10 01:28:12 -07:00 committed by Aaron.Zhang
parent aed64a20c4
commit 2973de6fe1
3 changed files with 16 additions and 2 deletions

View File

@ -207,6 +207,14 @@ func formatBytes(val uint64) string {
} }
func handle(conn io.ReadWriter, msg []byte) error { 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] { switch msg[0] {
case signal.StackTrace: case signal.StackTrace:
return pprof.Lookup("goroutine").WriteTo(conn, 2) return pprof.Lookup("goroutine").WriteTo(conn, 2)
@ -286,7 +294,7 @@ func handle(conn io.ReadWriter, msg []byte) error {
time.Sleep(5 * time.Second) time.Sleep(5 * time.Second)
trace.Stop() trace.Stop()
case signal.SetGCPercent: case signal.SetGCPercent:
perc, err := binary.ReadVarint(bufio.NewReader(conn)) perc, err := binary.ReadVarint(reader)
if err != nil { if err != nil {
return err return err
} }

View File

@ -306,6 +306,9 @@ func cmdLazy(addr net.TCPAddr, c byte, params ...byte) (io.Reader, error) {
return nil, err return nil, err
} }
buf := []byte{c} buf := []byte{c}
magic := make([]byte, binary.MaxVarintLen64)
binary.PutVarint(magic, internal.MAGIC)
buf = append(buf, magic...)
buf = append(buf, params...) buf = append(buf, params...)
if _, err := conn.Write(buf); err != nil { if _, err := conn.Write(buf); err != nil {
return nil, err return nil, err

View File

@ -13,7 +13,10 @@ import (
"strings" "strings"
) )
const gopsConfigDirEnvKey = "GOPS_CONFIG_DIR" const (
goipsConfigDirEnvKey = "GOPS_CONFIG_DIR"
MAGIC = 0xabcdefedcba
)
func ConfigDir() (string, error) { func ConfigDir() (string, error) {
if configDir := os.Getenv(gopsConfigDirEnvKey); configDir != "" { if configDir := os.Getenv(gopsConfigDirEnvKey); configDir != "" {