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: prevent uncaught exception in zlibBuffer #1811

Closed
wants to merge 3 commits 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
20 changes: 18 additions & 2 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const Transform = require('_stream_transform');
const binding = process.binding('zlib');
const util = require('util');
const assert = require('assert').ok;
const kMaxLength = process.binding('smalloc').kMaxLength;
const kRangeErrorMessage = 'Cannot create final Buffer. ' +
'It would be larger than 0x' + kMaxLength.toString(16) + ' bytes.';

// zlib doesn't provide these, so kludge them in following the same
// const naming scheme zlib uses.
Expand Down Expand Up @@ -209,10 +212,18 @@ function zlibBuffer(engine, buffer, callback) {
}

function onEnd() {
var buf = Buffer.concat(buffers, nread);
var buf;
var err = null;

if (nread >= kMaxLength) {
err = new RangeError(kRangeErrorMessage);
} else {
buf = Buffer.concat(buffers, nread);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you do something more like:

err = null;

try {
  buf = Buffer.concat(buffers, nread);
} catch (e) {
  err = e;
}

buffers = [];
engine.close();
callback(err, buf);

Copy link
Member Author

Choose a reason for hiding this comment

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

done


buffers = [];
callback(null, buf);
engine.close();
callback(err, buf);
}
}

Expand Down Expand Up @@ -525,6 +536,11 @@ Zlib.prototype._processChunk = function(chunk, flushFlag, cb) {
throw error;
}

if (nread >= kMaxLength) {
this.close();
throw new RangeError(kRangeErrorMessage);
}

var buf = Buffer.concat(buffers, nread);
this.close();

Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-regress-GH-io-1811.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const assert = require('assert');

// Change kMaxLength for zlib to trigger the error
// without having to allocate 1GB of buffers
const smalloc = process.binding('smalloc');
smalloc.kMaxLength = 128;
const zlib = require('zlib');
smalloc.kMaxLength = 0x3fffffff;

const encoded = new Buffer('H4sIAAAAAAAAA0tMHFgAAIw2K/GAAAAA', 'base64');

// Async
zlib.gunzip(encoded, function(err) {
assert.ok(err instanceof RangeError);
});

// Sync
assert.throws(function() {
zlib.gunzipSync(encoded);
}, RangeError);