From cc12bbe5991ab6f335e429887f6ad5a2ad61da22 Mon Sep 17 00:00:00 2001 From: Kyue <164024549+Gooh456@users.noreply.github.com> Date: Fri, 24 Jul 2026 17:33:45 +0100 Subject: [PATCH] fix(image): reject non-positive duration in saveGif() saveGif('name', 0) or a negative duration captures zero frames, then _generateGlobalPalette does `new Uint8Array(frames.length * frames[0].length)` with frames empty, so frames[0] is undefined and it throws "Cannot read properties of undefined (reading 'length')". Every other bad param in saveGif already throws up front, so guard duration the same way and throw a RangeError before recording starts. Fixes #8710 Signed-off-by: Kyue <164024549+Gooh456@users.noreply.github.com> --- src/image/loading_displaying.js | 5 +++++ test/unit/image/downloading.js | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/image/loading_displaying.js b/src/image/loading_displaying.js index 3d23cc4c0b..fd3ba0aa30 100644 --- a/src/image/loading_displaying.js +++ b/src/image/loading_displaying.js @@ -265,6 +265,11 @@ function loadingDisplaying(p5, fn){ if (typeof duration !== 'number') { throw TypeError('Duration parameter must be a number'); } + // a non-positive duration captures zero frames, which later crashes + // in palette generation (frames[0] is undefined). reject early. + if (duration <= 0) { + throw RangeError('Duration parameter must be greater than 0'); + } // extract variables for more comfortable use const delay = (options && options.delay) || 0; // in seconds diff --git a/test/unit/image/downloading.js b/test/unit/image/downloading.js index 3d37d979e3..edc025d1a9 100644 --- a/test/unit/image/downloading.js +++ b/test/unit/image/downloading.js @@ -177,6 +177,11 @@ suite('Downloading', () => { assert.typeOf(mockP5Prototype.saveGif, 'function'); }); + test('should reject a non-positive duration', async () => { + await expect(mockP5Prototype.saveGif('myGif', 0)).rejects.toThrow(); + await expect(mockP5Prototype.saveGif('myGif', -3)).rejects.toThrow(); + }); + // TODO: this implementation need refactoring test.todo('should not throw an error', async () => { await mockP5Prototype.saveGif('myGif', 3);