Reject non-positive duration in saveGif to avoid empty-frames crash#8740
Reject non-positive duration in saveGif to avoid empty-frames crash#8740Chessing234 wants to merge 1 commit into
Conversation
When saveGif is called with duration <= 0, the main recording loop never runs and the frames array stays empty. _generateGlobalPalette then reads frames[0].length, producing a cryptic TypeError. Validate duration > 0 up front with a clear error, matching the existing type checks. Fixes processing#8710
perminder-17
left a comment
There was a problem hiding this comment.
Hi, thanks for your work on this.
Adding an explicit duration check feels fine, but I'm not sure it's strictly necessary. The API already documents duration clearly as recording time in seconds, so the intent is fairly self-evident. If a user accidentally passes 0 or a negative value, I'd lean toward a silent no-op rather than throwing, that feels more in line with how p5.js generally handles unexpected input. That said, I'm equally happy to leave it as-is if preferred.
Also, one small hesitation is also around consistency: saveGif already has 7 validation throws, which is a bit heavier than most of the codebase tends to be. Adding an 8th would push it a little further in that direction.
CC: @davepagurek @ksen0
Bug
saveGif('test', 0)(or any non-positive duration) crashes withTypeError: Cannot read properties of undefined (reading 'length')instead of a friendly error. Reported in #8710.Root cause
In
src/image/loading_displaying.js,saveGifvalidates thatdurationis a number but not its range. Whenduration <= 0:nFrames = duration * _frameRateis<= 0while (frameIterator < totalNumberOfFrames)loop never executesframesstays[]_generateGlobalPalette(frames)then readsframes[0].lengthon line 597 and throws.Fix
Add a 3-line range check directly after the existing
typeof durationcheck, in the same style as the surrounding validators:Why this is correct
duration > 0; existing tests intest/unit/image/downloading.jscontinue to pass unchanged.Fixes #8710