Skip to content

Reject non-positive duration in saveGif to avoid empty-frames crash#8740

Open
Chessing234 wants to merge 1 commit intoprocessing:mainfrom
Chessing234:fix/savegif-nonpositive-duration
Open

Reject non-positive duration in saveGif to avoid empty-frames crash#8740
Chessing234 wants to merge 1 commit intoprocessing:mainfrom
Chessing234:fix/savegif-nonpositive-duration

Conversation

@Chessing234
Copy link
Copy Markdown

Bug

saveGif('test', 0) (or any non-positive duration) crashes with TypeError: Cannot read properties of undefined (reading 'length') instead of a friendly error. Reported in #8710.

Root cause

In src/image/loading_displaying.js, saveGif validates that duration is a number but not its range. When duration <= 0:

  • nFrames = duration * _frameRate is <= 0
  • the while (frameIterator < totalNumberOfFrames) loop never executes
  • frames stays []
  • _generateGlobalPalette(frames) then reads frames[0].length on line 597 and throws.

Fix

Add a 3-line range check directly after the existing typeof duration check, in the same style as the surrounding validators:

if (duration <= 0) {
  throw new Error('Duration parameter must be a positive number');
}

Why this is correct

  • A non-positive duration can never produce a valid GIF, so rejecting it up front is semantically correct and matches the issue reporter's suggested behavior ("saveGif() should reject invalid duration early").
  • Preserves the existing validator style (type + range checks at the top of the function).
  • No behavioral change for any duration > 0; existing tests in test/unit/image/downloading.js continue to pass unchanged.

Fixes #8710

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

saveGif() crashes when duration <= 0 (empty frame list path)

1 participant