Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down Expand Up @@ -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) {
Expand Down
37 changes: 37 additions & 0 deletions test/parallel/test-zlib-sync-array-push.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'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');

function gunzipWithInstrumentedPush(opts) {
const originalPush = Array.prototype.push;
let chunkPushes = 0;
let result;
Array.prototype.push = function(...args) {
if (args.some((arg) => arg instanceof Uint8Array))
chunkPushes++;
return Reflect.apply(originalPush, this, args);
};
try {
result = zlib.gunzipSync(compressed, opts);
} finally {
Array.prototype.push = originalPush;
}
return { result, chunkPushes };
}

// 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 { result, chunkPushes } = gunzipWithInstrumentedPush(opts);
assert.deepStrictEqual(result, expected);
assert.strictEqual(chunkPushes, 0);
}
Loading