From b935472cdf9caeeabb9d071d43e8a957729b58ef Mon Sep 17 00:00:00 2001 From: zeripath Date: Sat, 26 Jun 2021 08:31:03 +0100 Subject: [PATCH] Improve efficiency in FindRenderizableReferenceNumeric and getReference (#16251) (#16255) * Improve efficiency in FindRenderizableReferenceNumeric and getReferences (#16251) * 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 Co-authored-by: techknowlogick * as per comment on original pr Signed-off-by: Andrew Thornton Co-authored-by: techknowlogick --- modules/references/references.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/modules/references/references.go b/modules/references/references.go index c243f25f5d..b795225b14 100644 --- a/modules/references/references.go +++ b/modules/references/references.go @@ -5,6 +5,7 @@ package references import ( + "bytes" "net/url" "regexp" "strconv" @@ -14,6 +15,8 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup/mdstripper" "code.gitea.io/gitea/modules/setting" + + "github.com/yuin/goldmark/util" ) var ( @@ -321,7 +324,7 @@ func FindRenderizableReferenceNumeric(content string, prOnly bool) (bool, *Rende return false, nil } } - r := getCrossReference([]byte(content), match[2], match[3], false, prOnly) + r := getCrossReference(util.StringToReadOnlyBytes(content), match[2], match[3], false, prOnly) if r == nil { return false, nil } @@ -465,17 +468,16 @@ func findAllIssueReferencesBytes(content []byte, links []string) []*rawReference } func getCrossReference(content []byte, start, end int, fromLink bool, prOnly bool) *rawReference { - refid := string(content[start:end]) - sep := strings.IndexAny(refid, "#!") + sep := bytes.IndexAny(content[start:end], "#!") if sep < 0 { return nil } - isPull := refid[sep] == '!' + isPull := content[start+sep] == '!' if prOnly && !isPull { return nil } - repo := refid[:sep] - issue := refid[sep+1:] + repo := string(content[start : start+sep]) + issue := string(content[start+sep+1 : end]) index, err := strconv.ParseInt(issue, 10, 64) if err != nil { return nil