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

fix: slice bounds out of range [:5] #98

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion snaps/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func getTestID(b []byte) (string, bool) {
}

// needs to start with [Test and end with ]
if !bytes.Equal(b[0:5], []byte("[Test")) || b[len(b)-1] != ']' {
if !bytes.HasPrefix(b, []byte("[Test")) || b[len(b)-1] != ']' {
return "", false
}

Expand Down
7 changes: 6 additions & 1 deletion snaps/clean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ func TestGetTestID(t *testing.T) {
// must have dash between test name and number
{"[Test something 10]", "", false},
{"[Test/something - not a number]", "", false},
{"s", "", false},
}

for _, tc := range testCases {
Expand All @@ -456,7 +457,11 @@ func TestGetTestID(t *testing.T) {
t.Run(tc.input, func(t *testing.T) {
t.Parallel()

id, ok := getTestID([]byte(tc.input))
// make sure that the capacity of b is len(tc.input), this way
// indexing beyond the capacity will cause test to panic
b := make([]byte, 0, len(tc.input))
b = append(b, []byte(tc.input)...)
gkampitakis marked this conversation as resolved.
Show resolved Hide resolved
id, ok := getTestID(b)

test.Equal(t, tc.valid, ok)
test.Equal(t, tc.expectedID, id)
Expand Down
Loading