Skip to content

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

Open
Chessing234 wants to merge 1 commit into
processing: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 into
processing: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
Copy link
Copy Markdown
Collaborator

@perminder-17 perminder-17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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)

2 participants