Skip to content

Commit 083e446

Browse files
author
Ruben Bridgewater
committed
Fix parser regression. Out of memory resulted in an endless loop
1 parent 26e5764 commit 083e446

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

lib/parser/javascript.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ ReplyParser.prototype._packetEndOffset = function () {
220220

221221
while (this._buffer[offset] !== 0x0d && this._buffer[offset + 1] !== 0x0a) {
222222
offset++;
223+
224+
/* istanbul ignore if: activate the js parser out of memory test to test this */
225+
if (offset >= this._buffer.length) {
226+
throw new IncompleteReadBuffer("didn't see LF after NL reading multi bulk count (" + offset + " => " + this._buffer.length + ", " + this._offset + ")");
227+
}
223228
}
224229

225230
offset++;

test/parser/javascript.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
var assert = require('assert');
44
var Parser = require("../../lib/parser/javascript").Parser;
5+
var config = require("../lib/config");
6+
var redis = config.redis;
57

68
describe('javascript parser', function () {
79
it('handles multi-bulk reply', function (done) {
@@ -24,4 +26,36 @@ describe('javascript parser', function () {
2426
assert.equal(reply_count, 3, "check reply should have been called three times");
2527
return done();
2628
});
29+
30+
// Activate this if you want to fry your cpu / memory
31+
describe.skip("test out of memory", function () {
32+
var args = config.configureClient('javascript', '127.0.0.1');
33+
var clients = new Array(300).join(" ").split(" ");
34+
var client;
35+
beforeEach(function (done) {
36+
client = redis.createClient.apply(redis.createClient, args);
37+
client.once("connect", function () {
38+
client.flushdb(done);
39+
});
40+
});
41+
42+
it('reach limit and wait for further data', function (done) {
43+
setTimeout(done, 5000);
44+
clients.forEach(function(entry, a) {
45+
var max = 0;
46+
var client = redis.createClient.apply(redis.createClient, args);
47+
client.on('ready', function() {
48+
while (++max < 50) {
49+
var item = [];
50+
for (var i = 0; i < 100; ++i) {
51+
item.push('aaa' + (Math.random() * 1000000 | 0));
52+
}
53+
client.del('foo' + a);
54+
client.lpush('foo' + a, item);
55+
client.lrange('foo' + a, 0, 99);
56+
}
57+
});
58+
});
59+
});
60+
});
2761
});

0 commit comments

Comments
 (0)