From cbf7c6abccf7375fec3547f09304168e11af8da5 Mon Sep 17 00:00:00 2001 From: zhongweikang Date: Wed, 6 Nov 2024 15:48:49 +0800 Subject: [PATCH] bug: fix memory corruption for Conn.Next (#649) Fixes #648 --------- Co-authored-by: zhongweikang --- connection_unix.go | 6 +++--- connection_windows.go | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/connection_unix.go b/connection_unix.go index cfce3a928..3ccac4ab1 100644 --- a/connection_unix.go +++ b/connection_unix.go @@ -325,11 +325,11 @@ func (c *conn) Next(n int) (buf []byte, err error) { } head, tail := c.inboundBuffer.Peek(n) defer c.inboundBuffer.Discard(n) //nolint:errcheck - if len(head) >= n { - return head[:n], err - } c.loop.cache.Reset() c.loop.cache.Write(head) + if len(head) >= n { + return c.loop.cache.Bytes(), err + } c.loop.cache.Write(tail) if inBufferLen >= n { return c.loop.cache.Bytes(), err diff --git a/connection_windows.go b/connection_windows.go index b32675002..745028434 100644 --- a/connection_windows.go +++ b/connection_windows.go @@ -164,11 +164,11 @@ func (c *conn) Next(n int) (buf []byte, err error) { } head, tail := c.inboundBuffer.Peek(n) defer c.inboundBuffer.Discard(n) //nolint:errcheck - if len(head) >= n { - return head[:n], err - } c.loop.cache.Reset() c.loop.cache.Write(head) + if len(head) >= n { + return c.loop.cache.Bytes(), err + } c.loop.cache.Write(tail) if inBufferLen >= n { return c.loop.cache.Bytes(), err