1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2025-06-27 16:35:57 +00:00

fix: Token.ParseIssueReference crashing on empty string (#8260)

A fix for a bug introduced by me earlier, where attempting to parse an issue reference in an empty token would crash.

An empty token occurs if the search string is `\` or `"` (among other scenarios, probably). I'll make another PR that avoids having empty tokens (seems like a good idea).

### Tests

- I added test coverage for Go changes...
  - [x] in their respective `*_test.go` for unit tests.

### Documentation

- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.

### Release notes

- [x] I do not want this change to show in the release notes.
- [ ] I want the title to show in the release notes with a link to this pull request.
- [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/8260
Reviewed-by: Shiny Nematoda <snematoda@noreply.codeberg.org>
Co-authored-by: Danko Aleksejevs <danko@very.lv>
Co-committed-by: Danko Aleksejevs <danko@very.lv>
This commit is contained in:
Danko Aleksejevs 2025-06-24 06:52:36 +02:00 committed by Earl Warren
parent 0b24915271
commit f7d7d67238
2 changed files with 33 additions and 1 deletions

View file

@ -25,7 +25,7 @@ type Token struct {
func (tk *Token) ParseIssueReference() (int64, error) {
term := tk.Term
if term[0] == '#' || term[0] == '!' {
if len(term) > 1 && (term[0] == '#' || term[0] == '!') {
term = term[1:]
}
return strconv.ParseInt(term, 10, 64)

View file

@ -169,3 +169,35 @@ func TestIssueQueryString(t *testing.T) {
})
}
}
func TestToken_ParseIssueReference(t *testing.T) {
var tk Token
{
tk.Term = "123"
id, err := tk.ParseIssueReference()
require.NoError(t, err)
assert.Equal(t, int64(123), id)
}
{
tk.Term = "#123"
id, err := tk.ParseIssueReference()
require.NoError(t, err)
assert.Equal(t, int64(123), id)
}
{
tk.Term = "!123"
id, err := tk.ParseIssueReference()
require.NoError(t, err)
assert.Equal(t, int64(123), id)
}
{
tk.Term = "text"
_, err := tk.ParseIssueReference()
require.Error(t, err)
}
{
tk.Term = ""
_, err := tk.ParseIssueReference()
require.Error(t, err)
}
}