Skip to content

Commit

Permalink
Merge pull request redis#139 from felixge/master
Browse files Browse the repository at this point in the history
Fix: Hiredis parser traps pubsub event exceptions
  • Loading branch information
mranney committed Nov 14, 2011
2 parents abd32ce + fbc1642 commit 16c79f0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
23 changes: 14 additions & 9 deletions lib/parser/hiredis.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,20 @@ HiredisReplyParser.prototype.reset = function () {
HiredisReplyParser.prototype.execute = function (data) {
var reply;
this.reader.feed(data);
try {
while ((reply = this.reader.get()) !== undefined) {
if (reply && reply.constructor === Error) {
this.emit("reply error", reply);
} else {
this.emit("reply", reply);
}
while (true) {
try {
reply = this.reader.get();
} catch (err) {
this.emit("error", err);
break;
}

if (reply === undefined) break;

if (reply && reply.constructor === Error) {
this.emit("reply error", reply);
} else {
this.emit("reply", reply);
}
} catch (err) {
this.emit("error", err);
}
};
38 changes: 38 additions & 0 deletions tests/hiredis_parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var Parser = require('../lib/parser/hiredis').Parser;
var assert = require('assert');

/*
This test makes sure that exceptions thrown inside of "reply" event handlers
are not trapped and mistakenly emitted as parse errors.
*/
(function testExecuteDoesNotCatchReplyCallbackExceptions() {
var parser = new Parser();
var replies = [{}];

parser.reader = {
feed: function() {},
get: function() {
return replies.shift();
}
};

var emittedError = false;
var caughtException = false;

parser
.on('error', function() {
emittedError = true;
})
.on('reply', function() {
throw new Error('bad');
});

try {
parser.execute();
} catch (err) {
caughtException = true;
}

assert.equal(caughtException, true);
assert.equal(emittedError, false);
})();

0 comments on commit 16c79f0

Please sign in to comment.