From ff1da23d5a6b6dad655cb1197f3b543d248303b3 Mon Sep 17 00:00:00 2001 From: Georg Reinke Date: Sun, 9 Apr 2023 13:35:53 +0200 Subject: [PATCH 1/3] message: don't do a complete encode to check validity --- message.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/message.go b/message.go index 91caefc..5ab6e9d 100644 --- a/message.go +++ b/message.go @@ -188,7 +188,7 @@ func DecodeMessageWithFDs(rd io.Reader, fds []int) (msg *Message, err error) { } } - if err = msg.IsValid(); err != nil { + if err = msg.validateHeader(); err != nil { return nil, err } sig, _ := msg.Headers[FieldSignature].value.(Signature) From bac911b9e91cb6863c01efd8503573e95e1cd45f Mon Sep 17 00:00:00 2001 From: marselester Date: Thu, 9 Feb 2023 10:51:03 -0500 Subject: [PATCH 2/3] Do not check message validity after decoding Encoding a message with msg.EncodeTo to check whether the message is valid right after it was decoded looks redundant and incurs 13.4kB, 140 allocs per operation. If the message was corrupted, it would have failed at the decoding stage in ReadMessage. --- transport_unix.go | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/transport_unix.go b/transport_unix.go index a5e6db5..ef81b48 100644 --- a/transport_unix.go +++ b/transport_unix.go @@ -200,7 +200,7 @@ func (t *unixTransport) ReadMessage() (*Message, error) { return nil, err } dec.Reset(r, order, fds) - if err = decodeMessageBody(msg, dec, b); err != nil { + if err = decodeMessageBody(msg, dec); err != nil { return nil, err } // substitute the values in the message body (which are indices for the @@ -227,25 +227,19 @@ func (t *unixTransport) ReadMessage() (*Message, error) { } dec.Reset(r, order, nil) - if err = decodeMessageBody(msg, dec, b); err != nil { + if err = decodeMessageBody(msg, dec); err != nil { return nil, err } return msg, nil } -func decodeMessageBody(msg *Message, dec *decoder, b *bytes.Buffer) error { - // Check whether message is valid. - b.Reset() - err := msg.EncodeTo(b, nativeEndian) - if err != nil { - return err - } - +func decodeMessageBody(msg *Message, dec *decoder) error { sig, _ := msg.Headers[FieldSignature].value.(Signature) if sig.str == "" { return nil } + var err error msg.Body, err = dec.Decode(sig) return err } From 001f80fad8ca00c6c3dc2a108c150d564d4557d7 Mon Sep 17 00:00:00 2001 From: Georg Reinke Date: Sun, 9 Apr 2023 13:39:34 +0200 Subject: [PATCH 3/3] transport_unix: check message header validity when decoding --- transport_unix.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/transport_unix.go b/transport_unix.go index ef81b48..6840387 100644 --- a/transport_unix.go +++ b/transport_unix.go @@ -234,6 +234,10 @@ func (t *unixTransport) ReadMessage() (*Message, error) { } func decodeMessageBody(msg *Message, dec *decoder) error { + if err := msg.validateHeader(); err != nil { + return err + } + sig, _ := msg.Headers[FieldSignature].value.(Signature) if sig.str == "" { return nil