Skip to content
This repository
Browse code

Fix #2365 zlib crashing on invalid input

Fix zlib crashes on error due to improper use of removeListener
in the error handler
  • Loading branch information...
commit 40c93486e8b08f7f37dfd8be95ff03990ab858bd 1 parent f25ec07
Nicolas LaCasse authored isaacs committed

Showing 2 changed files with 50 additions and 8 deletions. Show diff stats Hide diff stats

  1. +12 8 lib/zlib.js
  2. +38 0 test/simple/test-zlib-invalid-input.js
20 lib/zlib.js
@@ -122,18 +122,18 @@ function zlibBuffer(engine, buffer, callback) {
122 122 var buffers = [];
123 123 var nread = 0;
124 124
125   - engine.on('error', function(err) {
126   - engine.removeListener('end');
127   - engine.removeListener('error');
  125 + function onError(err) {
  126 + engine.removeListener('end', onEnd);
  127 + engine.removeListener('error', onError);
128 128 callback(err);
129   - });
  129 + }
130 130
131   - engine.on('data', function(chunk) {
  131 + function onData(chunk) {
132 132 buffers.push(chunk);
133 133 nread += chunk.length;
134   - });
  134 + }
135 135
136   - engine.on('end', function() {
  136 + function onEnd() {
137 137 var buffer;
138 138 switch(buffers.length) {
139 139 case 0:
@@ -153,7 +153,11 @@ function zlibBuffer(engine, buffer, callback) {
153 153 break;
154 154 }
155 155 callback(null, buffer);
156   - });
  156 + }
  157 +
  158 + engine.on('error', onError);
  159 + engine.on('data', onData);
  160 + engine.on('end', onEnd);
157 161
158 162 engine.write(buffer);
159 163 engine.end();
38 test/simple/test-zlib-invalid-input.js
... ... @@ -0,0 +1,38 @@
  1 +// Copyright Joyent, Inc. and other Node contributors.
  2 +//
  3 +// Permission is hereby granted, free of charge, to any person obtaining a
  4 +// copy of this software and associated documentation files (the
  5 +// "Software"), to deal in the Software without restriction, including
  6 +// without limitation the rights to use, copy, modify, merge, publish,
  7 +// distribute, sublicense, and/or sell copies of the Software, and to permit
  8 +// persons to whom the Software is furnished to do so, subject to the
  9 +// following conditions:
  10 +//
  11 +// The above copyright notice and this permission notice shall be included
  12 +// in all copies or substantial portions of the Software.
  13 +//
  14 +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15 +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16 +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17 +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18 +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19 +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20 +// USE OR OTHER DEALINGS IN THE SOFTWARE.
  21 +
  22 +// test uncompressing invalid input
  23 +
  24 +var common = require('../common.js'),
  25 + assert = require('assert'),
  26 + zlib = require('zlib');
  27 +
  28 +var nonStringInputs = [1, true, {a: 1}, ['a']];
  29 +
  30 +nonStringInputs.forEach(function(input) {
  31 + // zlib.gunzip should not throw an error when called with bad input.
  32 + assert.doesNotThrow(function () {
  33 + zlib.gunzip(input, function (err, buffer) {
  34 + // zlib.gunzip should pass the error to the callback.
  35 + assert.ok(err);
  36 + });
  37 + });
  38 +});

0 comments on commit 40c9348

Please sign in to comment.
Something went wrong with that request. Please try again.