Skip to content

Commit

Permalink
bug: Fix decode panic for invalid input
Browse files Browse the repository at this point in the history
  • Loading branch information
peterhellberg committed Sep 6, 2023
1 parent d65ffba commit d580058
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion sqids.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,11 @@ func (s *Sqids) Decode(id string) []uint64 {
}
}

rid = joinRuneSlices(chunks[1:], separator)
if len(chunks) > 0 {
rid = joinRuneSlices(chunks[1:], separator)
} else {
return []uint64{}
}
}

return ret
Expand Down
15 changes: 15 additions & 0 deletions sqids_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,18 @@ func TestCalculateOffset(t *testing.T) {
}
}
}

func TestDecode(t *testing.T) {
s, err := New()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

if got, want := len(s.Decode("Re")), 0; got != want {
t.Fatalf(`len(s.Decode("Re")) = %d, want %d`, got, want)
}

if got, want := len(s.Decode("U9")), 1; got != want {
t.Fatalf(`len(s.Decode("U9")) = %d, want %d`, got, want)
}
}

0 comments on commit d580058

Please sign in to comment.