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

Wrong append behavior when appending from bufio.Reader #6048

Closed
gopherbot opened this issue Aug 5, 2013 · 2 comments
Closed

Wrong append behavior when appending from bufio.Reader #6048

gopherbot opened this issue Aug 5, 2013 · 2 comments
Milestone

Comments

@gopherbot
Copy link
Contributor

by borja.roux:

What steps will reproduce the problem?
Execute from a machine with internet access this code:
http://play.golang.org/p/F2zKRm_qW1

What is the expected output?
Word read: aboriginally
Last word: aboriginally
FirstWord: aa
Word count: 447
Word read: aboriginals
Last word: aboriginals
FirstWord: aa
Word count: 448
Word read: aborigine
Last word: aborigine
FirstWord: aa
Word count: 449
Word read: aborigines
Last word: aborigines
FirstWord: aa
Word count: 450

What do you see instead?
Word read: aboriginally
Last word: aboriginally
FirstWord: aa
Word count: 447
Word read: aboriginals
Last word: aboriginals
FirstWord: aa
Word count: 448
Word read: aborigine
Last word: aborigine
FirstWord: ab
Word count: 449
Word read: aborigines
Last word: aborigines
FirstWord: ab
Word count: 450


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

Which operating system are you using?
Windows7 x64

Which version are you using?  (run 'go version')
go version go1.1.1 windows/amd64

Please provide any additional information below.
Program reads words separated by '\n' from a file and appends them to an array. append
work ok until iteration 449. At this point it seems the contents are stored in position
0 (instead of appending), but the size of element 0 is not changed (see attached debug
information for details).
I have reproduced the problem when reading from a local file, but not when generating
contents inside the program.

Attachments:

  1. debug_data.txt (3321 bytes)
@bradfitz
Copy link
Contributor

bradfitz commented Aug 5, 2013

Comment 1:

bufio.Reader.ReadLine has docs about the lifetime of its returned value:
"The returned buffer is only valid until the next call to ReadLine. ReadLine either
returns a non-nil line or it returns an error, never both."
Use ReadString or ReadBytes instead.

Status changed to Invalid.

@DanielMorsing
Copy link
Contributor

Comment 2:

Even if ReadLine is being misused, we should put a s[i:j:k] slice in there to protect
the buffer.

@rsc rsc added this to the Go1.2 milestone Apr 14, 2015
@rsc rsc removed the go1.2maybe label Apr 14, 2015
@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

4 participants