Skip to content

strings: Builder ReadFrom permits creation of mutable strings #23083

Closed
@fweimer

Description

@fweimer

This program allows the creation of mutable strings:

package main

import (
        "fmt"
        "io"
        "strings"
)

var global_buf []byte

type Reader struct{}

func (Reader) Read(buf []byte) (count int, err error) {
        global_buf = buf
        return 0, io.EOF
}

func main() {
        var b strings.Builder
        b.ReadFrom(Reader{})
        fmt.Printf("len(global_buf): %d\n", len(global_buf))
        b.WriteString("foo")
        s := b.String()
        fmt.Printf("string before patching: %#v\n", s)
        copy(global_buf[:3], "bar")
        fmt.Printf("string after patching: %#v\n", s)
}

Output is:

len(global_buf): 512
string before patching: "foo"
string after patching: "bar"

The reason why this works is that the io.Reader Read method is passed a slice which refers to the internal buffer of the builder.

Activity

fweimer

fweimer commented on Dec 11, 2017

@fweimer
ContributorAuthor

Issue #23084 is a different bug resulting in mutable strings.

bradfitz

bradfitz commented on Dec 11, 2017

@bradfitz
Contributor

Also bad. Thanks for the reports! Will fix.

gopherbot

gopherbot commented on Dec 11, 2017

@gopherbot
Contributor

Change https://golang.org/cl/83255 mentions this issue: strings: fix two Builder bugs allowing mutation of strings, remove ReadFrom

added
NeedsFixThe path to resolution is known, but the work has not been done.
on Dec 11, 2017
added this to the Go1.10 milestone on Dec 11, 2017
locked and limited conversation to collaborators on Dec 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @bradfitz@fweimer@dsnet@gopherbot

        Issue actions

          strings: Builder ReadFrom permits creation of mutable strings · Issue #23083 · golang/go