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

builtin: append is modifying input #66081

Closed
3052 opened this issue Mar 4, 2024 · 3 comments
Closed

builtin: append is modifying input #66081

3052 opened this issue Mar 4, 2024 · 3 comments

Comments

@3052
Copy link

3052 commented Mar 4, 2024

Go version

go version go1.22.0 windows/amd64

Output of go env in your module/workspace:

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Steven\AppData\Local\go-build
set GOENV=C:\Users\Steven\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Steven\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Steven\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=D:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=D:\go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.22.0
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=D:\git\media\go.mod
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Windows\TEMP\go-build2696231797=/tmp/go-build -gno-record-gcc-switches

What did you do?

running this code:

package main

import (
   "154.pages.dev/protobuf"
   "fmt"
)

func main() {
   b := protobuf.Message{
      protobuf.Field{Number: 3, Type: 2, Value: protobuf.Message{
         protobuf.Field{Number: 1, Type: 2, Value: protobuf.Message{
            protobuf.Field{Number: 1, Type: 2, Value: protobuf.Message{
               protobuf.Field{Number: 1, Type: 2, Value: protobuf.Bytes("\xeb\xec")},
            }},
         }},
      }},
      protobuf.Field{Number: 5, Type: 2, Value: protobuf.Bytes("\x03\x00")},
   }.Encode()
   var m protobuf.Message
   m.Consume(b)
   login_context, _ := m.GetBytes(5)
   m, _ = m.Get(3)
   m, _ = m.Get(1)
   m, _ = m.Get(1)
   prefix, _ := m.GetBytes(1)
   fmt.Printf("%q\n", login_context)
   prefix = append(prefix, 1, 2, 3, 4, 5, 6)
   fmt.Printf("%q\n", login_context)
}

What did you see happen?

outputs:

"\x03\x00"
"\x03\x04"

What did you expect to see?

for some reason append is modifying login_context. why would append be doing this?

@randall77
Copy link
Contributor

Are you sure it isn't the append that is at fault? Separate that out into a different line and print login_context between the append and the sha1.Sum call.

@3052 3052 changed the title crypto/sha1: Sum is modifying input builtin: append is modifying input Mar 4, 2024
@3052 3052 closed this as completed Mar 4, 2024
@3052 3052 reopened this Mar 4, 2024
@seankhliao
Copy link
Member

that's how slices / append works.

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Mar 4, 2024
@3052
Copy link
Author

3052 commented Mar 4, 2024

I was able to fix this by clipping:

prefix = prefix[:len(prefix):len(prefix)]

or with:

prefix = slices.Clip(prefix)

https://godocs.io/slices#Clip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants