From d24560ad65d5059b09c70337d2113667ea6d084e Mon Sep 17 00:00:00 2001 From: iteyelmp Date: Mon, 6 Jul 2026 15:44:10 +0800 Subject: [PATCH] Add ReadRemaining helper (#27) * Add ReadRemaining helper * Add readBytes helper * Add size check --- qkc/serialize/bytebuffer.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/qkc/serialize/bytebuffer.go b/qkc/serialize/bytebuffer.go index 3356747882..9703a5243e 100644 --- a/qkc/serialize/bytebuffer.go +++ b/qkc/serialize/bytebuffer.go @@ -24,6 +24,9 @@ func (bb *ByteBuffer) GetOffset() int { } func (bb *ByteBuffer) getBytes(size int) ([]byte, error) { + if size < 0 { + return nil, fmt.Errorf("deser: negative size %d is invalid", size) + } if size > bb.size-bb.position { return nil, fmt.Errorf("deser: buffer is shorter than expected") } @@ -120,3 +123,14 @@ func (bb *ByteBuffer) GetVarBytes(byteSizeOfSliceLen int) ([]byte, error) { func (bb *ByteBuffer) Remaining() int { return bb.size - bb.position } + +// ReadRemaining returns all unread bytes in the buffer. +// Used by types that need to consume the rest of the buffer (e.g. opaque +// placeholder types for forward-compatible deserialization). +func (bb *ByteBuffer) ReadRemaining() ([]byte, error) { + return bb.getBytes(bb.Remaining()) +} + +func (bb *ByteBuffer) ReadBytes(n int) ([]byte, error) { + return bb.getBytes(n) +}