Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
check size in zlib
  • Loading branch information
targos committed Jun 4, 2015
commit ce770de20f3ebbc74274fb405981935b8653a3c4
16 changes: 13 additions & 3 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 @@ -211,11 +214,13 @@ function zlibBuffer(engine, buffer, callback) {
function onEnd() {
var buf;
var err = null;
try {

if (nread >= kMaxLength) {
err = new RangeError(kRangeErrorMessage);
} else {
buf = Buffer.concat(buffers, nread);
} catch (e) {
err = e;
}
Copy link
Copy Markdown
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
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

done


buffers = [];
engine.close();
callback(err, buf);
Expand Down Expand Up @@ -531,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