Skip to content

Commit 62a4879

Browse files
Improve efficiency in FindRenderizableReferenceNumeric and getReferences (#16251)
* Fuzzer finds an NPE due to incorrect URLPrefix The Fuzzer is running on a non-repo urlprefix which is incorrect for RenderRaw * Make FindRenderizableReferenceNumeric and getReferences more efficient Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent 9b33d18 commit 62a4879

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

modules/references/references.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package references
66

77
import (
8+
"bytes"
89
"net/url"
910
"regexp"
1011
"strconv"
@@ -14,6 +15,8 @@ import (
1415
"code.gitea.io/gitea/modules/log"
1516
"code.gitea.io/gitea/modules/markup/mdstripper"
1617
"code.gitea.io/gitea/modules/setting"
18+
19+
"github.com/yuin/goldmark/util"
1720
)
1821

1922
var (
@@ -321,7 +324,7 @@ func FindRenderizableReferenceNumeric(content string, prOnly bool) (bool, *Rende
321324
return false, nil
322325
}
323326
}
324-
r := getCrossReference([]byte(content), match[2], match[3], false, prOnly)
327+
r := getCrossReference(util.StringToReadOnlyBytes(content), match[2], match[3], false, prOnly)
325328
if r == nil {
326329
return false, nil
327330
}
@@ -465,18 +468,17 @@ func findAllIssueReferencesBytes(content []byte, links []string) []*rawReference
465468
}
466469

467470
func getCrossReference(content []byte, start, end int, fromLink bool, prOnly bool) *rawReference {
468-
refid := string(content[start:end])
469-
sep := strings.IndexAny(refid, "#!")
471+
sep := bytes.IndexAny(content[start:end], "#!")
470472
if sep < 0 {
471473
return nil
472474
}
473-
isPull := refid[sep] == '!'
475+
isPull := content[start+sep] == '!'
474476
if prOnly && !isPull {
475477
return nil
476478
}
477-
repo := refid[:sep]
478-
issue := refid[sep+1:]
479-
index, err := strconv.ParseInt(issue, 10, 64)
479+
repo := string(content[start : start+sep])
480+
issue := string(content[start+sep+1 : end])
481+
index, err := strconv.ParseInt(string(issue), 10, 64)
480482
if err != nil {
481483
return nil
482484
}

0 commit comments

Comments
 (0)