fix(pdf): bound a split's every by the page count so it cannot overflow

This commit is contained in:
Julien Neuhart
2026-11-25 08:27:34 +01:00
parent 1312db9bf2
commit 8b4a38bcf4
2 changed files with 19 additions and 1 deletions

View File

@@ -612,7 +612,11 @@ func (p *Processor) splitOne(ctx *api.Context, s *splitStage, d document, passwo
func splitGroups(s *splitStage, pageCount int) iter.Seq2[[]int, error] {
return func(yield func([]int, error) bool) {
if s.Every != nil {
every := *s.Every
// A part holds the whole document at most, and the bound keeps
// start+every from overflowing: every = MaxInt wrapped start
// negative, and the loop appended page numbers until memory ran
// out (GHSA-5w6x-29mm-wm9w).
every := min(*s.Every, pageCount)
for start := 1; start <= pageCount; start += every {
var pages []int

View File

@@ -264,6 +264,20 @@ func TestSplitGroups(t *testing.T) {
}
}
// TestSplitGroupsEveryMaxInt pins that a part size past the page count, up to
// the largest int, yields the whole document once (GHSA-5w6x-29mm-wm9w).
func TestSplitGroupsEveryMaxInt(t *testing.T) {
for _, every := range []int{5, 6, math.MaxInt - 1, math.MaxInt} {
groups, err := collectSplitGroups(&splitStage{Every: new(every)}, 5)
if err != nil {
t.Fatal(err)
}
if len(groups) != 1 || !slices.Equal(groups[0], []int{1, 2, 3, 4, 5}) {
t.Fatalf("every %d of 5 = %v, want one part of every page", every, groups)
}
}
}
// TestSplitGroupsResolvesLazily pins that a group is resolved only when the
// loop reaches it: stopping after the first of many open ranges on a large
// document resolves that one alone.