Skip to content

Commit

Permalink
fix: slice bounds out of range [:5] (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
zregvart committed Apr 23, 2024
1 parent e31ee30 commit f98a2f9
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
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)...)
id, ok := getTestID(b)

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

0 comments on commit f98a2f9

Please sign in to comment.