Skip to content

Commit 828f214

Browse files
jackcclaude
andcommitted
Fix message length parsing on 32-bit platforms
On 32-bit platforms, int(uint32) for values above MaxInt32 silently wraps negative, bypassing the < 4 length check and producing a negative bodyLen. Cast through int32 first so that large uint32 values become negative and are properly rejected. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent e196a39 commit 828f214

2 files changed

Lines changed: 3 additions & 3 deletions

File tree

pgproto3/backend.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func (b *Backend) ReceiveStartupMessage() (FrontendMessage, error) {
123123
if err != nil {
124124
return nil, err
125125
}
126-
msgSize := int(binary.BigEndian.Uint32(buf) - 4)
126+
msgSize := int(int32(binary.BigEndian.Uint32(buf)) - 4)
127127

128128
if msgSize < minStartupPacketLen || msgSize > maxStartupPacketLen {
129129
return nil, fmt.Errorf("invalid length of startup packet: %d", msgSize)
@@ -176,7 +176,7 @@ func (b *Backend) Receive() (FrontendMessage, error) {
176176

177177
b.msgType = header[0]
178178

179-
msgLength := int(binary.BigEndian.Uint32(header[1:]))
179+
msgLength := int(int32(binary.BigEndian.Uint32(header[1:])))
180180
if msgLength < 4 {
181181
return nil, fmt.Errorf("invalid message length: %d", msgLength)
182182
}

pgproto3/frontend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ func (f *Frontend) Receive() (BackendMessage, error) {
313313

314314
f.msgType = header[0]
315315

316-
msgLength := int(binary.BigEndian.Uint32(header[1:]))
316+
msgLength := int(int32(binary.BigEndian.Uint32(header[1:])))
317317
if msgLength < 4 {
318318
return nil, fmt.Errorf("invalid message length: %d", msgLength)
319319
}

0 commit comments

Comments
 (0)