Skip to content

bytes.ReplaceAll returns []byte(nil) if the first parameter is []byte{} #48872

@novns

Description

@novns

bytes.ReplaceAll returns []byte(nil) if the first parameter is []byte{}

It should return the unmodified slice instead.

This matters for arguments of database queries where []byte(nil) means a NULL value instead of an empty string.
Currently an empty slice is a special case and requires additional checks before replacing things (and a lot of debugging).

What version of Go are you using (go version)?

go version go1.17.1 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/novns/.cache/go-build"
GOENV="/home/novns/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/novns/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/novns/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.17.1"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2838946019=/tmp/go-build -gno-record-gcc-switches"

What did you do?

https://play.golang.org/p/zp168o-BoDt

fmt.Printf("%#v", bytes.ReplaceAll([]byte{}, []byte{1}, []byte{2}))

What did you expect to see?

[]byte{}

What did you see instead?

[]byte(nil)

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