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

bufio: Reader.ReadLine reusing memory of []byte output #3906

Closed
gopherbot opened this issue Aug 4, 2012 · 3 comments
Closed

bufio: Reader.ReadLine reusing memory of []byte output #3906

gopherbot opened this issue Aug 4, 2012 · 3 comments

Comments

@gopherbot
Copy link

by adriansinclair2007:

I have this code (I don't think that the messing with tty buffering is related):
exec.Command("/bin/stty", "-f", "/dev/tty",
"sane").Run()
fmt.Println("What file should I archive the level to?")
path, _, error := stdin.ReadLine()
fmt.Println("Rename the new level:")
name, _, error := stdin.ReadLine()
exec.Command("/bin/stty", "-f", "/dev/tty",
"-icanon", "min", "1").Run()

where stdin is defined as follows:
bufio.NewReader(os.stdin)

It was having strange problems so I logged the `path' variable and it showed me nothing
like what I input the first time, but showed me a somewhat corrupted version of my input
to the second prompt.

I fixed this by converting the []byte to a string (resulting in a copy) before I
prompted for the next input, so it seems clear that the memory allocated for `path' was
being prematurely reused. Maybe a bug in the bufio package, or maybe a bug in the
garbage collector (seems less likely).

I have go1, and I think it compiled with 6g (`go env' reports GOCHAR="6")
@remyoudompheng
Copy link
Contributor

Comment 1:

The reusing behaviour is specified in the bufio documentation:
"The returned buffer is only valid until the next call to ReadLine."

Status changed to WorkingAsIntended.

@rsc
Copy link
Contributor

rsc commented Aug 5, 2012

Comment 2:

Use ReadString or ReadBytes instead.

@rsc
Copy link
Contributor

rsc commented Aug 5, 2012

Comment 3:

This issue was closed by revision ea392b8.

Status changed to Fixed.

rsc added a commit that referenced this issue May 11, 2015
««« backport 9e2011d9ed4b
bufio: discourage use of ReadLine

Too many people use it without reading what it does.
Those people want ReadBytes or ReadString.

Fixes #3906.

R=golang-dev, iant, r
CC=golang-dev
https://golang.org/cl/6442087

»»»
@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

3 participants