[go] bytes: fix panic in bytes.Buffer.Peek

6 views
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
Nov 4, 2025, 2:06:24 AM (7 days ago) Nov 4
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gerrit Bot has uploaded the change for review

Commit message

bytes: fix panic in bytes.Buffer.Peek

This change fixed the overlooked offset in bytes.Buffer.Peek.
Otherwise, it will either return wrong result or panic with `runtime error: slice bounds out of range`.
Change-Id: Ic42fd8a27fb9703c51430f298933b91cf0d45451
GitHub-Last-Rev: 2537aff75a24ebc98a41018f2c578f61e7c4b438
GitHub-Pull-Request: golang/go#76165

Change diff

diff --git a/src/bytes/buffer.go b/src/bytes/buffer.go
index 3eb5b35..6cb4d6a 100644
--- a/src/bytes/buffer.go
+++ b/src/bytes/buffer.go
@@ -86,7 +86,7 @@
if b.Len() < n {
return b.buf[b.off:], io.EOF
}
- return b.buf[b.off:n], nil
+ return b.buf[b.off : b.off+n], nil
}

// empty reports whether the unread portion of the buffer is empty.
diff --git a/src/bytes/buffer_test.go b/src/bytes/buffer_test.go
index 5f5cc48..49f6016 100644
--- a/src/bytes/buffer_test.go
+++ b/src/bytes/buffer_test.go
@@ -533,19 +533,24 @@

var peekTests = []struct {
buffer string
+ skip int
n int
expected string
err error
}{
- {"", 0, "", nil},
- {"aaa", 3, "aaa", nil},
- {"foobar", 2, "fo", nil},
- {"a", 2, "a", io.EOF},
+ {"", 0, 0, "", nil},
+ {"aaa", 0, 3, "aaa", nil},
+ {"foobar", 0, 2, "fo", nil},
+ {"a", 0, 2, "a", io.EOF},
+ {"helloworld", 4, 3, "owo", nil},
+ {"helloworld", 5, 5, "world", nil},
+ {"helloworld", 5, 6, "world", io.EOF},
}

func TestPeek(t *testing.T) {
for _, test := range peekTests {
buf := NewBufferString(test.buffer)
+ buf.Next(test.skip)
bytes, err := buf.Peek(test.n)
if string(bytes) != test.expected {
t.Errorf("expected %q, got %q", test.expected, bytes)
@@ -553,8 +558,8 @@
if err != test.err {
t.Errorf("expected error %v, got %v", test.err, err)
}
- if buf.Len() != len(test.buffer) {
- t.Errorf("bad length after peek: %d, want %d", buf.Len(), len(test.buffer))
+ if buf.Len() != len(test.buffer)-test.skip {
+ t.Errorf("bad length after peek: %d, want %d", buf.Len(), len(test.buffer)-test.skip)
}
}
}

Change information

Files:
  • M src/bytes/buffer.go
  • M src/bytes/buffer_test.go
Change size: S
Delta: 2 files changed, 12 insertions(+), 7 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Ic42fd8a27fb9703c51430f298933b91cf0d45451
Gerrit-Change-Number: 717640
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Gopher Robot (Gerrit)

unread,
Nov 4, 2025, 2:06:27 AM (7 days ago) Nov 4
to Gerrit Bot, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gopher Robot added 1 comment

Patchset-level comments
File-level comment, Patchset 1 (Latest):
Gopher Robot . unresolved

I spotted some possible problems with your PR:

  1. You have a long 103 character line in the commit message body. Please add line breaks to long lines that should be wrapped. Lines in the commit message body should be wrapped at ~76 characters unless needed for things like URLs or tables. (Note: GitHub might render long lines as soft-wrapped, so double-check in the Gerrit commit message shown above.)
2. It looks like you are using markdown in the commit message. If so, please remove it. Be sure to double-check the plain text shown in the Gerrit commit message above for any markdown backticks, markdown links, or other markdown formatting.
3. You usually need to reference a bug number for all but trivial or cosmetic fixes. For this repo, the format is usually 'Fixes #12345' or 'Updates #12345' at the end of the commit message. Should you have a bug reference?

Please address any problems by updating the GitHub PR.

When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above. These findings are based on heuristics; if a finding does not apply, briefly reply here saying so.

To update the commit title or commit message body shown here in Gerrit, you must edit the GitHub PR title and PR description (the first comment) in the GitHub web interface using the 'Edit' button or 'Edit' menu entry there. Note: pushing a new commit to the PR will not automatically update the commit message used by Gerrit.

For more details, see:

(In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)

Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ic42fd8a27fb9703c51430f298933b91cf0d45451
    Gerrit-Change-Number: 717640
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Comment-Date: Tue, 04 Nov 2025 07:06:22 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Gopher Robot (Gerrit)

    unread,
    Nov 4, 2025, 2:09:57 AM (7 days ago) Nov 4
    to Gerrit Bot, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Message from Gopher Robot

    Congratulations on opening your first change. Thank you for your contribution!

    Next steps:
    A maintainer will review your change and provide feedback. See
    https://go.dev/doc/contribute#review for more info and tips to get your
    patch through code review.

    Most changes in the Go project go through a few rounds of revision. This can be
    surprising to people new to the project. The careful, iterative review process
    is our way of helping mentor contributors and ensuring that their contributions
    have a lasting impact.

    During May-July and Nov-Jan the Go project is in a code freeze, during which
    little code gets reviewed or merged. If a reviewer responds with a comment like
    R=go1.11 or adds a tag like "wait-release", it means that this CL will be
    reviewed as part of the next development cycle. See https://go.dev/s/release
    for more details.

    Open in Gerrit

    Related details

    Attention set is empty
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Ic42fd8a27fb9703c51430f298933b91cf0d45451
    Gerrit-Change-Number: 717640
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Comment-Date: Tue, 04 Nov 2025 07:09:51 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Aaron Chen (Gerrit)

    unread,
    Nov 4, 2025, 2:35:37 AM (7 days ago) Nov 4
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Brad Fitzpatrick, Ian Lance Taylor, Gopher Robot, golang-co...@googlegroups.com
    Attention needed from Brad Fitzpatrick and Ian Lance Taylor

    Aaron Chen added 1 comment

    Patchset-level comments
    Gopher Robot . resolved

    I spotted some possible problems with your PR:

      1. You have a long 103 character line in the commit message body. Please add line breaks to long lines that should be wrapped. Lines in the commit message body should be wrapped at ~76 characters unless needed for things like URLs or tables. (Note: GitHub might render long lines as soft-wrapped, so double-check in the Gerrit commit message shown above.)
    2. It looks like you are using markdown in the commit message. If so, please remove it. Be sure to double-check the plain text shown in the Gerrit commit message above for any markdown backticks, markdown links, or other markdown formatting.
    3. You usually need to reference a bug number for all but trivial or cosmetic fixes. For this repo, the format is usually 'Fixes #12345' or 'Updates #12345' at the end of the commit message. Should you have a bug reference?

    Please address any problems by updating the GitHub PR.

    When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above. These findings are based on heuristics; if a finding does not apply, briefly reply here saying so.

    To update the commit title or commit message body shown here in Gerrit, you must edit the GitHub PR title and PR description (the first comment) in the GitHub web interface using the 'Edit' button or 'Edit' menu entry there. Note: pushing a new commit to the PR will not automatically update the commit message used by Gerrit.

    For more details, see:

    (In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)

    Aaron Chen

    Done

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Brad Fitzpatrick
    • Ian Lance Taylor
    Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Ic42fd8a27fb9703c51430f298933b91cf0d45451
      Gerrit-Change-Number: 717640
      Gerrit-PatchSet: 1
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
      Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
      Gerrit-CC: Aaron Chen <aaronch...@gmail.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
      Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
      Gerrit-Comment-Date: Tue, 04 Nov 2025 07:35:33 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      Comment-In-Reply-To: Gopher Robot <go...@golang.org>
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Gerrit Bot (Gerrit)

      unread,
      Nov 4, 2025, 2:39:32 AM (7 days ago) Nov 4
      to Aaron Chen, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
      Attention needed from Brad Fitzpatrick and Ian Lance Taylor

      Gerrit Bot uploaded new patchset

      Gerrit Bot uploaded patch set #2 to this change.
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Brad Fitzpatrick
      • Ian Lance Taylor
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: newpatchset
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Ic42fd8a27fb9703c51430f298933b91cf0d45451
      Gerrit-Change-Number: 717640
      Gerrit-PatchSet: 2
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      qiu laidongfeng (Gerrit)

      unread,
      Nov 6, 2025, 9:44:34 AM (5 days ago) Nov 6
      to Aaron Chen, Gerrit Bot, goph...@pubsubhelper.golang.org, Brad Fitzpatrick, Ian Lance Taylor, Gopher Robot, golang-co...@googlegroups.com
      Attention needed from Brad Fitzpatrick and Ian Lance Taylor

      qiu laidongfeng voted Commit-Queue+1

      Commit-Queue+1
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Brad Fitzpatrick
      • Ian Lance Taylor
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Ic42fd8a27fb9703c51430f298933b91cf0d45451
      Gerrit-Change-Number: 717640
      Gerrit-PatchSet: 2
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
      Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
      Gerrit-Reviewer: qiu laidongfeng <26454...@qq.com>
      Gerrit-CC: Aaron Chen <aaronch...@gmail.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
      Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
      Gerrit-Comment-Date: Thu, 06 Nov 2025 14:44:26 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Sean Liao (Gerrit)

      unread,
      Nov 6, 2025, 6:04:39 PM (4 days ago) Nov 6
      to Aaron Chen, Gerrit Bot, goph...@pubsubhelper.golang.org, Go LUCI, qiu laidongfeng, Brad Fitzpatrick, Ian Lance Taylor, Gopher Robot, golang-co...@googlegroups.com
      Attention needed from Brad Fitzpatrick and Ian Lance Taylor

      Sean Liao voted

      Auto-Submit+1
      Code-Review+2
      Commit-Queue+1
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Brad Fitzpatrick
      • Ian Lance Taylor
      Submit Requirements:
      • requirement satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Ic42fd8a27fb9703c51430f298933b91cf0d45451
      Gerrit-Change-Number: 717640
      Gerrit-PatchSet: 2
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
      Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
      Gerrit-Reviewer: Sean Liao <se...@liao.dev>
      Gerrit-Reviewer: qiu laidongfeng <26454...@qq.com>
      Gerrit-CC: Aaron Chen <aaronch...@gmail.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
      Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
      Gerrit-Comment-Date: Thu, 06 Nov 2025 23:04:29 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      satisfied_requirement
      unsatisfied_requirement
      open
      diffy

      Aaron Chen (Gerrit)

      unread,
      Nov 7, 2025, 2:15:23 AM (4 days ago) Nov 7
      to Gerrit Bot, goph...@pubsubhelper.golang.org, Go LUCI, qiu laidongfeng, Brad Fitzpatrick, Ian Lance Taylor, Gopher Robot, golang-co...@googlegroups.com
      Attention needed from Brad Fitzpatrick and Ian Lance Taylor

      Aaron Chen voted Code-Review+1

      Code-Review+1
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Brad Fitzpatrick
      • Ian Lance Taylor
      Submit Requirements:
      • requirement satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Ic42fd8a27fb9703c51430f298933b91cf0d45451
      Gerrit-Change-Number: 717640
      Gerrit-PatchSet: 2
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Aaron Chen <aaronch...@gmail.com>
      Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
      Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
      Gerrit-Reviewer: Sean Liao <se...@liao.dev>
      Gerrit-Reviewer: qiu laidongfeng <26454...@qq.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
      Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
      Gerrit-Comment-Date: Fri, 07 Nov 2025 07:15:18 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      satisfied_requirement
      unsatisfied_requirement
      open
      diffy

      Aaron Chen (Gerrit)

      unread,
      Nov 7, 2025, 2:15:48 AM (4 days ago) Nov 7
      to Gerrit Bot, goph...@pubsubhelper.golang.org, Go LUCI, qiu laidongfeng, Brad Fitzpatrick, Ian Lance Taylor, Gopher Robot, golang-co...@googlegroups.com
      Attention needed from Brad Fitzpatrick and Ian Lance Taylor

      Aaron Chen removed a vote from this change

      Removed Code-Review+1 by Aaron Chen <aaronch...@gmail.com>
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Brad Fitzpatrick
      • Ian Lance Taylor
      Submit Requirements:
      • requirement satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: deleteVote
      satisfied_requirement
      unsatisfied_requirement
      open
      diffy

      Junyang Shao (Gerrit)

      unread,
      12:47 PM (10 hours ago) 12:47 PM
      to Aaron Chen, Gerrit Bot, goph...@pubsubhelper.golang.org, Go LUCI, qiu laidongfeng, Brad Fitzpatrick, Ian Lance Taylor, Gopher Robot, golang-co...@googlegroups.com
      Attention needed from Brad Fitzpatrick and Ian Lance Taylor

      Junyang Shao voted Code-Review+1

      Code-Review+1
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Brad Fitzpatrick
      • Ian Lance Taylor
      Submit Requirements:
      • requirement satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Ic42fd8a27fb9703c51430f298933b91cf0d45451
      Gerrit-Change-Number: 717640
      Gerrit-PatchSet: 2
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
      Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
      Gerrit-Reviewer: Junyang Shao <shaoj...@google.com>
      Gerrit-Reviewer: Sean Liao <se...@liao.dev>
      Gerrit-Reviewer: qiu laidongfeng <26454...@qq.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-Attention: Ian Lance Taylor <ia...@golang.org>
      Gerrit-Attention: Brad Fitzpatrick <brad...@golang.org>
      Gerrit-Comment-Date: Mon, 10 Nov 2025 17:47:32 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      satisfied_requirement
      unsatisfied_requirement
      open
      diffy
      Reply all
      Reply to author
      Forward
      0 new messages