From f12c2b2ac8e1aa6a919b660a30b70a053fbefea6 Mon Sep 17 00:00:00 2001 From: Roberto Bianchi Date: Tue, 24 Nov 2026 13:33:53 +0100 Subject: [PATCH] zlib: avoid mutable push in sync output collection processChunkSync() and processSyncWithDefaults() collected output chunks with buffers.push(), which looks up Array.prototype.push at call time. Replacing that method from user code changed how synchronous zlib calls such as gunzipSync() collected their output. Use indexed assignments instead. Signed-off-by: Roberto Bianchi Signed-off-by: Roberto Bianchi --- lib/zlib.js | 4 ++-- test/parallel/test-zlib-sync-array-push.js | 28 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-zlib-sync-array-push.js diff --git a/lib/zlib.js b/lib/zlib.js index a2d2c158e5df..2230913d534a 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -325,7 +325,7 @@ function processSyncWithDefaults(handle, writeState, chunk, flushFlag, if (nread > kMaxLength) throw new ERR_BUFFER_TOO_LARGE(kMaxLength); if (out !== scratch) { - buffers.push(out.subarray(0, have)); + buffers[buffers.length] = out.subarray(0, have); } else if (availOutAfter === 0) { buffers = [out]; } @@ -599,7 +599,7 @@ function processChunkSync(self, chunk, flushFlag) { if (have > 0) { const out = buffer.slice(offset, offset + have); offset += have; - buffers.push(out); + buffers[buffers.length] = out; nread += out.byteLength; if (nread > self._maxOutputLength) { diff --git a/test/parallel/test-zlib-sync-array-push.js b/test/parallel/test-zlib-sync-array-push.js new file mode 100644 index 000000000000..38ae7d7a015d --- /dev/null +++ b/test/parallel/test-zlib-sync-array-push.js @@ -0,0 +1,28 @@ +'use strict'; + +// Synchronous zlib output collection must not depend on a user-modified +// Array.prototype.push. + +require('../common'); +const assert = require('assert'); +const fixtures = require('../common/fixtures'); +const zlib = require('zlib'); + +const expected = fixtures.readSync('person.jpg'); +const compressed = fixtures.readSync('person.jpg.gz'); + +// Without options, the default chunk size still yields several chunks. +// A small chunkSize goes through the options path and yields many chunks. +for (const opts of [undefined, { chunkSize: 1024 }]) { + const originalPush = Array.prototype.push; + let result; + Array.prototype.push = function() { + throw new Error('Array.prototype.push should not be called'); + }; + try { + result = zlib.gunzipSync(compressed, opts); + } finally { + Array.prototype.push = originalPush; + } + assert.deepStrictEqual(result, expected); +}