Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bytes: Buffer.ReadBytes() miscalculates size #1499

Closed
gopherbot opened this issue Feb 11, 2011 · 2 comments
Closed

bytes: Buffer.ReadBytes() miscalculates size #1499

gopherbot opened this issue Feb 11, 2011 · 2 comments

Comments

@gopherbot
Copy link

by joelegasse:

What steps will reproduce the problem?

Compiling and running the following program:

package main

import (
  "bytes"
  "fmt"
)

func main() {
  data := bytes.NewBuffer([]byte{1, 2, 3, 4, 5})
  data.Next(2) // buffer now on element 3
  line, _ := data.ReadBytes(3)
  fmt.Println(line)
}

What is the expected output?

[3]

What do you see instead?

Runtime panic.

Which compiler are you using (5g, 6g, 8g, gccgo)?

6g

Which operating system are you using?

linux

Which revision are you using?  (hg identify)

867d37fb41a4+ release.2011-02-01.1/release

Please provide any additional information below.

In src/pkg/bytes/buffer.go, in the ReadBytes() method, the following line needs to be
updated (including the fix for issue #1498).

diff -r 867d37fb41a4 src/pkg/bytes/buffer.go
--- a/src/pkg/bytes/buffer.go   Wed Feb 02 12:09:31 2011 +1100
+++ b/src/pkg/bytes/buffer.go   Fri Feb 11 02:35:19 2011 -0500
@@ -309,13 +309,14 @@
 // delim.
 func (b *Buffer) ReadBytes(delim byte) (line []byte, err os.Error) {
        i := IndexByte(b.buf[b.off:], delim)
-       size := i + 1 - b.off
-       if i < 0 {
+       size := i + 1
+       if size <= 0 {
                size = len(b.buf) - b.off
                err = os.EOF
        }
        line = make([]byte, size)
        copy(line, b.buf[b.off:])
+       b.off += size
        return
 }


This is because IndexByte will return the index into the slice that it was passed
(b.buf[b.off:] in this case), which means that i is actually b.off + i with respect to
b.buf. So, there is no need to take away the current offset when calculating the size. A
zero would represent that the delim is at b.off, so we would want to copy 1 byte out of
ReadBytes(), not -1 as in the sample program provided (b.off == 2, so i + 1 - 2 would be
-1). That -1 is then passed to make(), which doesn't like negative sizes. To fix this, I
also changed the check from 'i < 0', to 'size <= 0'. That way we know that in the
call to make() size is greater than or equal to zero.
@rsc
Copy link
Contributor

rsc commented Feb 11, 2011

Comment 1:

Merge into issue: 1498

Status changed to Duplicate.

@rsc
Copy link
Contributor

rsc commented Feb 11, 2011

Comment 2:

Merged into issue #1498.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants