Skip to content

Commit 7efebc3

Browse files
silverwindclaude
andcommitted
Fix CodeQL code scanning alerts
Address CodeQL alerts by handling integer conversions at callee level using generics and bounding allocations: - Make paginator.New and NewPagination generic [T ~int | ~int64] to safely accept both int and int64, clamping internally - Change SetLinkHeader to accept int64 (methods can't be generic in Go) - Use strconv.Atoi(fmt.Sprint(...)) in htmlrenderer to avoid int64 intermediate - Clamp regex match indices in escape_stream to prevent allocation-size-overflow - Fix over-allocation in SanitizeCredentialURLs - Cap slice pre-allocation in GetIssueDependencies Co-Authored-By: Claude (Opus 4.6) <noreply@anthropic.com>
1 parent a0996cb commit 7efebc3

38 files changed

Lines changed: 74 additions & 68 deletions

modules/charset/escape_stream.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ func (e *escapeStreamer) Text(data string) error {
6161
until = len(data)
6262
next = until
6363
} else {
64-
until, next = nextIdxs[0]+pos, nextIdxs[1]+pos
64+
until = min(nextIdxs[0]+pos, len(data))
65+
next = min(nextIdxs[1]+pos, len(data))
6566
}
6667

6768
// from pos until we know that the runes are not \r\t\n or even ' '
68-
runes := make([]rune, 0, next-until)
69-
positions := make([]int, 0, next-until+1)
69+
n := next - until
70+
runes := make([]rune, 0, n)
71+
positions := make([]int, 0, n+1)
7072

7173
for pos < until {
7274
r, sz := utf8.DecodeRune(dataBytes[pos:])

modules/paginator/paginator.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
package paginator
66

7-
import "code.gitea.io/gitea/modules/util"
7+
import (
8+
"math"
9+
10+
"code.gitea.io/gitea/modules/util"
11+
)
812

913
/*
1014
In template:
@@ -44,15 +48,16 @@ type Paginator struct {
4448
}
4549

4650
// New initialize a new pagination calculation and returns a Paginator as result.
47-
func New(total, pagingNum, current, numPages int) *Paginator {
51+
func New[T ~int | ~int64](total T, pagingNum, current, numPages int) *Paginator {
4852
pagingNum = max(pagingNum, 1)
49-
totalPages := util.Iif(total == -1, -1, (total+pagingNum-1)/pagingNum)
50-
if total >= 0 {
53+
t := int(min(int64(total), int64(math.MaxInt)))
54+
totalPages := util.Iif(t == -1, -1, (t+pagingNum-1)/pagingNum)
55+
if t >= 0 {
5156
current = min(current, totalPages)
5257
}
5358
current = max(current, 1)
5459
return &Paginator{
55-
total: total,
60+
total: t,
5661
totalPages: totalPages,
5762
current: current,
5863
pagingNum: pagingNum,

modules/templates/htmlrenderer.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"code.gitea.io/gitea/modules/log"
2222
"code.gitea.io/gitea/modules/setting"
2323
"code.gitea.io/gitea/modules/templates/scopedtmpl"
24-
"code.gitea.io/gitea/modules/util"
2524
)
2625

2726
type TemplateExecutor scopedtmpl.TemplateExecutor
@@ -159,15 +158,15 @@ func (p *templateErrorPrettier) makeDetailedError(errMsg, tmplName string, lineN
159158
if err != nil {
160159
return fmt.Sprintf("template error: %s, and unable to find template file %q", errMsg, tmplName)
161160
}
162-
line, err := util.ToInt64(lineNum)
161+
line, err := strconv.Atoi(fmt.Sprint(lineNum))
163162
if err != nil {
164163
return fmt.Sprintf("template error: %s, unable to parse template %q line number %q", errMsg, tmplName, lineNum)
165164
}
166-
pos, err := util.ToInt64(posNum)
165+
pos, err := strconv.Atoi(fmt.Sprint(posNum))
167166
if err != nil {
168167
return fmt.Sprintf("template error: %s, unable to parse template %q pos number %q", errMsg, tmplName, posNum)
169168
}
170-
detail := extractErrorLine(code, int(line), int(pos), target)
169+
detail := extractErrorLine(code, line, pos, target)
171170

172171
var msg string
173172
if pos >= 0 {

modules/util/sanitize.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func SanitizeCredentialURLs(s string) string {
3636
if schemeSepPos == -1 || bytes.IndexByte(bs[schemeSepPos:], '@') == -1 {
3737
return s // fast return if there is no URL scheme or no userinfo
3838
}
39-
out := make([]byte, 0, len(bs)+len(userPlaceholder))
39+
out := make([]byte, 0, len(bs))
4040
for schemeSepPos != -1 {
4141
schemeSepPos += 3 // skip the "://"
4242
sepAtPos := -1 // the possible '@' position: "https://foo@[^here]host"

routers/api/v1/admin/email.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func GetAllEmails(ctx *context.APIContext) {
5151
results[i] = convert.ToEmailSearch(emails[i])
5252
}
5353

54-
ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
54+
ctx.SetLinkHeader(maxResults, listOptions.PageSize)
5555
ctx.SetTotalCountHeader(maxResults)
5656
ctx.JSON(http.StatusOK, &results)
5757
}

routers/api/v1/admin/hooks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func ListHooks(ctx *context.APIContext) {
7777
}
7878
hooks[i] = h
7979
}
80-
ctx.SetLinkHeader(int(total), listOptions.PageSize)
80+
ctx.SetLinkHeader(total, listOptions.PageSize)
8181
ctx.SetTotalCountHeader(total)
8282
ctx.JSON(http.StatusOK, hooks)
8383
}

routers/api/v1/admin/org.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func GetAllOrgs(ctx *context.APIContext) {
117117
orgs[i] = convert.ToOrganization(ctx, organization.OrgFromUser(users[i]))
118118
}
119119

120-
ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
120+
ctx.SetLinkHeader(maxResults, listOptions.PageSize)
121121
ctx.SetTotalCountHeader(maxResults)
122122
ctx.JSON(http.StatusOK, &orgs)
123123
}

routers/api/v1/admin/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ func SearchUsers(ctx *context.APIContext) {
534534
results[i] = convert.ToUser(ctx, users[i], ctx.Doer)
535535
}
536536

537-
ctx.SetLinkHeader(int(maxResults), listOptions.PageSize)
537+
ctx.SetLinkHeader(maxResults, listOptions.PageSize)
538538
ctx.SetTotalCountHeader(maxResults)
539539
ctx.JSON(http.StatusOK, &results)
540540
}

routers/api/v1/notify/repo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func ListRepoNotifications(ctx *context.APIContext) {
125125
return
126126
}
127127

128-
ctx.SetLinkHeader(int(totalCount), opts.PageSize)
128+
ctx.SetLinkHeader(totalCount, opts.PageSize)
129129
ctx.SetTotalCountHeader(totalCount)
130130
ctx.JSON(http.StatusOK, convert.ToNotifications(ctx, nl))
131131
}

routers/api/v1/notify/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func ListNotifications(ctx *context.APIContext) {
8686
return
8787
}
8888

89-
ctx.SetLinkHeader(int(totalCount), opts.PageSize)
89+
ctx.SetLinkHeader(totalCount, opts.PageSize)
9090
ctx.SetTotalCountHeader(totalCount)
9191
ctx.JSON(http.StatusOK, convert.ToNotifications(ctx, nl))
9292
}

0 commit comments

Comments
 (0)