Skip to content

Commit aeb130f

Browse files
authored
fix integer overflow (#618)
1 parent 06a31fe commit aeb130f

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

cmd/render.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"image"
66
"io/ioutil"
7-
"math"
87
"os"
98
"strings"
109

@@ -159,8 +158,8 @@ func render(cmd *cobra.Command, args []string) error {
159158

160159
var buf []byte
161160

162-
if screens.ShowFullAnimation || maxDuration == 0 {
163-
maxDuration = math.MaxInt
161+
if screens.ShowFullAnimation {
162+
maxDuration = 0
164163
}
165164

166165
if renderGif {

encode/encode.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,19 @@ func (s *Screens) EncodeWebP(maxDuration int, filters ...ImageFilter) ([]byte, e
119119
remainingDuration := time.Duration(maxDuration) * time.Millisecond
120120
for _, im := range images {
121121
frameDuration := time.Duration(s.delay) * time.Millisecond
122-
if frameDuration > remainingDuration {
123-
frameDuration = remainingDuration
122+
123+
if maxDuration > 0 {
124+
if frameDuration > remainingDuration {
125+
frameDuration = remainingDuration
126+
}
127+
remainingDuration -= frameDuration
124128
}
125-
remainingDuration -= frameDuration
126129

127130
if err := anim.AddFrame(im, frameDuration); err != nil {
128131
return nil, errors.Wrap(err, "adding frame")
129132
}
130133

131-
if remainingDuration <= 0 {
134+
if maxDuration > 0 && remainingDuration <= 0 {
132135
break
133136
}
134137
}
@@ -167,15 +170,17 @@ func (s *Screens) EncodeGIF(maxDuration int, filters ...ImageFilter) ([]byte, er
167170
draw.Draw(imPaletted, imRGBA.Bounds(), imRGBA, image.Point{0, 0}, draw.Src)
168171

169172
frameDelay := int(s.delay)
170-
if frameDelay > remainingDuration {
171-
frameDelay = remainingDuration
173+
if maxDuration > 0 {
174+
if frameDelay > remainingDuration {
175+
frameDelay = remainingDuration
176+
}
177+
remainingDuration -= frameDelay
172178
}
173-
remainingDuration -= frameDelay
174179

175180
g.Image = append(g.Image, imPaletted)
176181
g.Delay = append(g.Delay, frameDelay/10) // in 100ths of a second
177182

178-
if remainingDuration <= 0 {
183+
if maxDuration > 0 && remainingDuration <= 0 {
179184
break
180185
}
181186
}

encode/encode_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,4 +349,15 @@ def main():
349349
for _, d := range gifDelays(gifData) {
350350
assert.Equal(t, 500, d)
351351
}
352+
353+
// inf ms -> all 100 frames, 500 ms each.
354+
gifData, err = ScreensFromRoots(roots).EncodeGIF(0)
355+
assert.NoError(t, err)
356+
webpData, err = ScreensFromRoots(roots).EncodeWebP(0)
357+
assert.NoError(t, err)
358+
assert.Equal(t, gifDelays(gifData), webpDelays(webpData))
359+
for _, d := range gifDelays(gifData) {
360+
assert.Equal(t, 500, d)
361+
}
362+
352363
}

0 commit comments

Comments
 (0)