Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zlib: report premature ends earlier #26363

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 10 additions & 0 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,16 @@ function processCallback() {
return;
}

if (availInAfter > 0) {
// If we have more input that should be written, but we also have output
// space available, that means that the compression library was not
// interested in receiving more data, and in particular that the input
// stream has ended early.
// This applies to streams where we don't check data past the end of
// what was consumed; that is, everything except Gunzip/Unzip.
self.push(null);
}

// finished with the chunk.
this.buffer = null;
this.cb();
Expand Down
33 changes: 33 additions & 0 deletions test/parallel/test-zlib-premature-end.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
const common = require('../common');
const zlib = require('zlib');
const assert = require('assert');

const input = '0123456789'.repeat(4);

for (const [ compress, decompressor ] of [
[ zlib.deflateRawSync, zlib.createInflateRaw ],
[ zlib.deflateSync, zlib.createInflate ],
[ zlib.brotliCompressSync, zlib.createBrotliDecompress ]
]) {
const compressed = compress(input);
const trailingData = Buffer.from('not valid compressed data');

for (const variant of [
(stream) => { stream.end(compressed); },
(stream) => { stream.write(compressed); stream.write(trailingData); },
(stream) => { stream.write(compressed); stream.end(trailingData); },
(stream) => { stream.write(Buffer.concat([compressed, trailingData])); },
(stream) => { stream.end(Buffer.concat([compressed, trailingData])); }
]) {
let output = '';
const stream = decompressor();
stream.setEncoding('utf8');
stream.on('data', (chunk) => output += chunk);
stream.on('end', common.mustCall(() => {
assert.strictEqual(output, input);
assert.strictEqual(stream.bytesWritten, compressed.length);
}));
variant(stream);
}
}