Update message.go

This commit is contained in:
sashaodessa 2026-01-12 12:26:48 +01:00 committed by GitHub
parent 31d5d82ce5
commit 064b6efc53
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -242,10 +242,18 @@ func ExpectMsg(r MsgReader, code uint64, content interface{}) error {
if int(msg.Size) != len(contentEnc) {
return fmt.Errorf("message size mismatch: got %d, want %d", msg.Size, len(contentEnc))
}
actualContent, err := io.ReadAll(msg.Payload)
// Read only up to the declared message size (+1 to detect overlong payloads).
limit := int64(msg.Size)
actualContent, err := io.ReadAll(io.LimitReader(msg.Payload, limit+1))
if err != nil {
return err
}
if int64(len(actualContent)) > limit {
return fmt.Errorf("message payload longer than declared size: got at least %d bytes, declared %d", len(actualContent), msg.Size)
}
if int64(len(actualContent)) < limit {
return fmt.Errorf("message payload shorter than declared size: got %d bytes, declared %d", len(actualContent), msg.Size)
}
if !bytes.Equal(actualContent, contentEnc) {
return fmt.Errorf("message payload mismatch:\ngot: %x\nwant: %x", actualContent, contentEnc)
}