Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

simple fix for #2364 (zlib inflate/deflate/gunzip/gzip crashes when called on non-string input) #2385

Closed
wants to merge 4 commits into from

4 participants

@nlacasse

The bug symptom is that calling any of the zlib convenience methods with a non-string argument results in a crash.

The zlibBuffer function is calling removeListener without passing in the event listener itself.

This patch saves the event listeners in variables, and passes them to removeListener.

@bnoordhuis

@isaacs: You know the zlib code best. Can you review this patch when you're back?

@nlacasse

Any word on if/when this patch will be reviewed? Thanks!

@isaacs
Owner

@nlacasse I don't get what this is doing. Can you include a test that fails without the patch, and passes with it?

Also, please prefer named function declarations like function onError() { rather than function expressions assigned to a variable like var onError = function() {.

Nicolas LaCasse added some commits
@nlacasse

@isaacs I changed the function declarations as you asked, and I added a test.

The issue is that zlib is not calling "removeListener" correctly. So on an error (like that caused by invalid input), removeListener gets called improperly and throws an error. The expected behavior is for a useful error to be passed to the callback.

@piscisaureus

@nlacasse Can you sign the CLA? http://nodejs.org/cla.html

@piscisaureus

I made some minor edits and squashed everything. When the cla is signed we can land https://gist.github.com/1647651

@nlacasse

@piscisaureus CLA is signed. Thanks!

@isaacs
Owner

Landed on 40c9348. Thanks!

@isaacs isaacs closed this
@piscisaureus

The related commit incorrectly references #2365. Adding backreference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 20, 2011
  1. fix #2384 zlib inflate/deflate/gunzip/gzip crashes when called on non…

    Nicolas LaCasse authored
    …-string input
Commits on Jan 20, 2012
  1. use named function declarations

    Nicolas LaCasse authored
  2. add test for #2365

    Nicolas LaCasse authored
  3. remove console.log debug statement from that test

    Nicolas LaCasse authored
This page is out of date. Refresh to see the latest.
Showing with 49 additions and 8 deletions.
  1. +12 −8 lib/zlib.js
  2. +37 −0 test/simple/test-zlib-invalid-input.js
View
20 lib/zlib.js
@@ -122,18 +122,18 @@ function zlibBuffer(engine, buffer, callback) {
var buffers = [];
var nread = 0;
- engine.on('error', function(err) {
- engine.removeListener('end');
- engine.removeListener('error');
+ function onError (err) {
+ engine.removeListener('end', onEnd);
+ engine.removeListener('error', onError);
callback(err);
- });
+ };
- engine.on('data', function(chunk) {
+ function onData (chunk) {
buffers.push(chunk);
nread += chunk.length;
- });
+ };
- engine.on('end', function() {
+ function onEnd() {
var buffer;
switch(buffers.length) {
case 0:
@@ -153,7 +153,11 @@ function zlibBuffer(engine, buffer, callback) {
break;
}
callback(null, buffer);
- });
+ };
+
+ engine.on('error', onError);
+ engine.on('data', onData);
+ engine.on('end', onEnd);
engine.write(buffer);
engine.end();
View
37 test/simple/test-zlib-invalid-input.js
@@ -0,0 +1,37 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+// test uncompressing invalid input
+
+var assert = require('assert');
+var zlib = require('zlib');
+
+var nonStringInputs = [1, true, {a: 1}, ['a']];
+
+nonStringInputs.forEach(function(input) {
+ // zlib.gunzip should not throw an error when called with bad input.
+ assert.doesNotThrow(function () {
+ zlib.gunzip(input, function (err, buffer) {
+ // zlib.gunzip should pass the error to the callback.
+ assert.ok(err);
+ });
+ });
+});
Something went wrong with that request. Please try again.