You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
by joelegasse:
The text was updated successfully, but these errors were encountered: