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

slices: Replace does not zero trailing elements if if j == len(s) and i > 0 #65669

Closed
joeycumines opened this issue Feb 12, 2024 · 3 comments
Closed
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@joeycumines
Copy link

joeycumines commented Feb 12, 2024

Go version

go version go1.22.0 darwin/arm64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/joeyc/Library/Caches/go-build'
GOENV='/Users/joeyc/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/joeyc/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/joeyc/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/joeyc/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.0.darwin-arm64'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/joeyc/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.0.darwin-arm64/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.22.0'
GCCGO='gccgo'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD='/Users/joeyc/dev/boi/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/_r/v0qs308n49952w5gddyqznbw0000gn/T/go-build2791652445=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

Using golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3, I calledslices.Replace, to shift elements from the end of the slice, and noticed it didn't clear as documented.

For context, I was using Replace like:

noFilterAfterIdx := // ...
filtered := slices.DeleteFunc(data[:noFilterAfterIdx], /* .. */)
if len(filtered) != noFilterAfterIdx {
  data = slices.Replace(data, len(filtered), len(data), data[noFilterAfterIdx:]...)
}

But I found that it wasn't behaving per the docs:

When len(v) < (j-i), Replace zeroes the elements between the new length and the original length.

Debugging the code shows it hit the second guard in the current implementation:

	if j == len(s) {
		return append(s[:i], v...)
	}

And I've confirmed it only appears to be an issue for that one edge case, so it's probably just an oversight / easy to fix bug.

package main

import (
	"fmt"
	"golang.org/x/exp/slices"
)

const l = 10

func input() []int {
	v := make([]int, l)
	for i := range v {
		v[i] = i + 1
	}
	return v
}

func main() {
	fmt.Println("input:", input())
	for i := 0; i < l; i++ {
		for j := i; j <= l; j++ {
			for k := i; k <= l; k++ {
				s := input()
				v := s[k:]
				n := len(slices.Replace(s, i, j, v...))
				// "When len(v) < (j-i), Replace zeroes the elements between the new length and the original length."
				if len(v) < (j - i) {
					expected := len(s) - n
					var actual int
					for _, v := range s[n:] {
						if v == 0 {
							actual++
						}
					}
					if actual != expected {
						fmt.Printf("replaced %d:%d with %d:%d but failed to zero %d/%d: %v\n", i, j, k, l, expected-actual, expected, s)
					}
				}
			}
		}
	}
}

What did you see happen?

Elements in the range (specified by i and j) weren't one of: overwritten, zeroed. Specifically, they weren't zeroed.

From the runnable example:

input: [1 2 3 4 5 6 7 8 9 10]
replaced 0:10 with 1:10 but failed to zero 1/1: [2 3 4 5 6 7 8 9 10 10]
replaced 0:10 with 2:10 but failed to zero 2/2: [3 4 5 6 7 8 9 10 9 10]
replaced 0:10 with 3:10 but failed to zero 3/3: [4 5 6 7 8 9 10 8 9 10]
replaced 0:10 with 4:10 but failed to zero 4/4: [5 6 7 8 9 10 7 8 9 10]
replaced 0:10 with 5:10 but failed to zero 5/5: [6 7 8 9 10 6 7 8 9 10]
replaced 0:10 with 6:10 but failed to zero 6/6: [7 8 9 10 5 6 7 8 9 10]
replaced 0:10 with 7:10 but failed to zero 7/7: [8 9 10 4 5 6 7 8 9 10]
replaced 0:10 with 8:10 but failed to zero 8/8: [9 10 3 4 5 6 7 8 9 10]
replaced 0:10 with 9:10 but failed to zero 9/9: [10 2 3 4 5 6 7 8 9 10]
replaced 0:10 with 10:10 but failed to zero 10/10: [1 2 3 4 5 6 7 8 9 10]
replaced 1:10 with 2:10 but failed to zero 1/1: [1 3 4 5 6 7 8 9 10 10]
replaced 1:10 with 3:10 but failed to zero 2/2: [1 4 5 6 7 8 9 10 9 10]
replaced 1:10 with 4:10 but failed to zero 3/3: [1 5 6 7 8 9 10 8 9 10]
replaced 1:10 with 5:10 but failed to zero 4/4: [1 6 7 8 9 10 7 8 9 10]
replaced 1:10 with 6:10 but failed to zero 5/5: [1 7 8 9 10 6 7 8 9 10]
replaced 1:10 with 7:10 but failed to zero 6/6: [1 8 9 10 5 6 7 8 9 10]
replaced 1:10 with 8:10 but failed to zero 7/7: [1 9 10 4 5 6 7 8 9 10]
replaced 1:10 with 9:10 but failed to zero 8/8: [1 10 3 4 5 6 7 8 9 10]
replaced 1:10 with 10:10 but failed to zero 9/9: [1 2 3 4 5 6 7 8 9 10]
replaced 2:10 with 3:10 but failed to zero 1/1: [1 2 4 5 6 7 8 9 10 10]
replaced 2:10 with 4:10 but failed to zero 2/2: [1 2 5 6 7 8 9 10 9 10]
replaced 2:10 with 5:10 but failed to zero 3/3: [1 2 6 7 8 9 10 8 9 10]
replaced 2:10 with 6:10 but failed to zero 4/4: [1 2 7 8 9 10 7 8 9 10]
replaced 2:10 with 7:10 but failed to zero 5/5: [1 2 8 9 10 6 7 8 9 10]
replaced 2:10 with 8:10 but failed to zero 6/6: [1 2 9 10 5 6 7 8 9 10]
replaced 2:10 with 9:10 but failed to zero 7/7: [1 2 10 4 5 6 7 8 9 10]
replaced 2:10 with 10:10 but failed to zero 8/8: [1 2 3 4 5 6 7 8 9 10]
replaced 3:10 with 4:10 but failed to zero 1/1: [1 2 3 5 6 7 8 9 10 10]
replaced 3:10 with 5:10 but failed to zero 2/2: [1 2 3 6 7 8 9 10 9 10]
replaced 3:10 with 6:10 but failed to zero 3/3: [1 2 3 7 8 9 10 8 9 10]
replaced 3:10 with 7:10 but failed to zero 4/4: [1 2 3 8 9 10 7 8 9 10]
replaced 3:10 with 8:10 but failed to zero 5/5: [1 2 3 9 10 6 7 8 9 10]
replaced 3:10 with 9:10 but failed to zero 6/6: [1 2 3 10 5 6 7 8 9 10]
replaced 3:10 with 10:10 but failed to zero 7/7: [1 2 3 4 5 6 7 8 9 10]
replaced 4:10 with 5:10 but failed to zero 1/1: [1 2 3 4 6 7 8 9 10 10]
replaced 4:10 with 6:10 but failed to zero 2/2: [1 2 3 4 7 8 9 10 9 10]
replaced 4:10 with 7:10 but failed to zero 3/3: [1 2 3 4 8 9 10 8 9 10]
replaced 4:10 with 8:10 but failed to zero 4/4: [1 2 3 4 9 10 7 8 9 10]
replaced 4:10 with 9:10 but failed to zero 5/5: [1 2 3 4 10 6 7 8 9 10]
replaced 4:10 with 10:10 but failed to zero 6/6: [1 2 3 4 5 6 7 8 9 10]
replaced 5:10 with 6:10 but failed to zero 1/1: [1 2 3 4 5 7 8 9 10 10]
replaced 5:10 with 7:10 but failed to zero 2/2: [1 2 3 4 5 8 9 10 9 10]
replaced 5:10 with 8:10 but failed to zero 3/3: [1 2 3 4 5 9 10 8 9 10]
replaced 5:10 with 9:10 but failed to zero 4/4: [1 2 3 4 5 10 7 8 9 10]
replaced 5:10 with 10:10 but failed to zero 5/5: [1 2 3 4 5 6 7 8 9 10]
replaced 6:10 with 7:10 but failed to zero 1/1: [1 2 3 4 5 6 8 9 10 10]
replaced 6:10 with 8:10 but failed to zero 2/2: [1 2 3 4 5 6 9 10 9 10]
replaced 6:10 with 9:10 but failed to zero 3/3: [1 2 3 4 5 6 10 8 9 10]
replaced 6:10 with 10:10 but failed to zero 4/4: [1 2 3 4 5 6 7 8 9 10]
replaced 7:10 with 8:10 but failed to zero 1/1: [1 2 3 4 5 6 7 9 10 10]
replaced 7:10 with 9:10 but failed to zero 2/2: [1 2 3 4 5 6 7 10 9 10]
replaced 7:10 with 10:10 but failed to zero 3/3: [1 2 3 4 5 6 7 8 9 10]
replaced 8:10 with 9:10 but failed to zero 1/1: [1 2 3 4 5 6 7 8 10 10]
replaced 8:10 with 10:10 but failed to zero 2/2: [1 2 3 4 5 6 7 8 9 10]
replaced 9:10 with 10:10 but failed to zero 1/1: [1 2 3 4 5 6 7 8 9 10]

What did you expect to see?

When len(v) < (j-i), Replace zeroes the elements between the new length and the original length.

The runnable example should have only printed input: [1 2 3 4 5 6 7 8 9 10].

@gopherbot gopherbot added this to the Unreleased milestone Feb 12, 2024
@ianlancetaylor
Copy link
Contributor

CC @Deleplace

@Deleplace
Copy link
Contributor

Deleplace commented Feb 12, 2024

Good catch!

This does look like a bug. I could confirm and wrote a small repro: https://go.dev/play/p/FQKUFDUD4ox

Thank you @joeycumines for writing this issue with sample code, and for pinpointing the culprit in the stdlib source. It does indeed look like the second guard is taking a shortcut that incorrectly ignores the recent zeroing policy.

@seankhliao seankhliao changed the title x/exp/slices: Replace does not zero trailing elements if if j == len(s) and i > 0 slices: Replace does not zero trailing elements if if j == len(s) and i > 0 Feb 12, 2024
@thanm thanm added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 12, 2024
@gopherbot
Copy link

Change https://go.dev/cl/566237 mentions this issue: slices: Delete clears the tail when j == len(s)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

5 participants