From 2fffc40249777040d74233649b16ae50a3e3a6ff Mon Sep 17 00:00:00 2001 From: Edoardo Spadolini Date: Fri, 15 Nov 2024 10:47:20 +0100 Subject: [PATCH] Fix lib/proxy/peer/quic for 32-bit builds --- lib/proxy/peer/quic/quic.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/proxy/peer/quic/quic.go b/lib/proxy/peer/quic/quic.go index 06dfda648c1f4..26b5ab8bf2394 100644 --- a/lib/proxy/peer/quic/quic.go +++ b/lib/proxy/peer/quic/quic.go @@ -168,14 +168,23 @@ const ( // marshalSized returns the wire encoding of the given [proto.Message] prefixed // by its length encoded as a little endian 32-bit integer. func marshalSized(m proto.Message) ([]byte, error) { + var size int = proto.MarshalOptions{}.Size(m) + // we're going to allocate 4+size, so size can't exceed MaxInt-4 + // (overflowing would be confusing at best, a runtime panic at worst) + if size > math.MaxInt-4 { + return nil, trace.LimitExceeded("oversized message") + } // we're going to store the size in the first four bytes of the buffer, so // we leave them as zeroes at first, then fix it later - buf := make([]byte, 4, 4+proto.MarshalOptions{}.Size(m)) + buf := make([]byte, 4, 4+size) buf, err := proto.MarshalOptions{UseCachedSize: true}.MarshalAppend(buf, m) if err != nil { return nil, trace.Wrap(err) } - if len(buf)-4 > math.MaxUint32 { + // the int64 cast is a noop on 64-bit archs but it's necessary to build on + // 32-bit (even though the condition is always false there, since slices + // can't be bigger than MaxInt32 which is smaller than MaxUint32) + if int64(len(buf))-4 > math.MaxUint32 { return nil, trace.LimitExceeded("oversized message") } binary.LittleEndian.PutUint32(buf, uint32(len(buf)-4))