From f7d7d67238d703cd2633bd11b44a77f43d405140 Mon Sep 17 00:00:00 2001 From: Danko Aleksejevs Date: Tue, 24 Jun 2025 06:52:36 +0200 Subject: [PATCH] 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/.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 Co-authored-by: Danko Aleksejevs Co-committed-by: Danko Aleksejevs --- modules/indexer/issues/internal/qstring.go | 2 +- .../indexer/issues/internal/qstring_test.go | 32 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/modules/indexer/issues/internal/qstring.go b/modules/indexer/issues/internal/qstring.go index 8115fc904f..6b60b4c5f6 100644 --- a/modules/indexer/issues/internal/qstring.go +++ b/modules/indexer/issues/internal/qstring.go @@ -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) diff --git a/modules/indexer/issues/internal/qstring_test.go b/modules/indexer/issues/internal/qstring_test.go index a911b86e2f..835491707c 100644 --- a/modules/indexer/issues/internal/qstring_test.go +++ b/modules/indexer/issues/internal/qstring_test.go @@ -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) + } +}