Skip to content

strings: Copying Builders allows string mutation #23084

Closed
@fweimer

Description

@fweimer

This program:

package main

import (
        "fmt"
        "strings"
)

func main() {
        var b1 strings.Builder
        b1.Grow(3)
        b2 := b1
        b1.WriteString("foo")
        s := b1.String()
        fmt.Printf("string before patching: %#v\n", s)
        b2.WriteString("bar")
        fmt.Printf("string after patching: %#v\n", s)
}

Prints:

string before patching: "foo"
string after patching: "bar"

I think another level of indirection is needed in the implementation, so that the internal buffer is shared even after a copy has been made.

Activity

bradfitz

bradfitz commented on Dec 11, 2017

@bradfitz
Contributor

Ouch, thanks. 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: Copying Builders allows string mutation · Issue #23084 · golang/go