Skip to content

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

@gopherbot

Description

@gopherbot

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")

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions