Skip to content

Commit 7be7128

Browse files
author
Ruben Bridgewater
committed
Arguments passed as arrays should not be mutated. Fixes #866
1 parent f29193a commit 7be7128

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

changelog.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
Changelog
22
=========
33

4+
## v2.0.1 - Sep 24, 2015
5+
6+
Bugfixes:
7+
8+
- Fix argument mutation while using the array notation in combination with keys / callbacks ([#866]). (@BridgeAR)
9+
410
## v2.0.0 - Sep 21, 2015
511

612
This is the biggest release that node_redis had since it was released in 2010. A long list of outstanding bugs has been fixed, so we are very happy to present you redis 2.0 and we highly recommend updating as soon as possible.

index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ commands.forEach(function (fullCommand) {
885885
return this.send_command(command, key, arg);
886886
}
887887
if (Array.isArray(arg)) {
888-
arg.unshift(key);
888+
arg = [key].concat(arg);
889889
return this.send_command(command, arg, callback);
890890
}
891891
return this.send_command(command, to_array(arguments));
@@ -895,12 +895,12 @@ commands.forEach(function (fullCommand) {
895895
Multi.prototype[command] = function (key, arg, callback) {
896896
if (Array.isArray(key)) {
897897
if (arg) {
898-
key.push(arg);
898+
key = key.concat([arg]);
899899
}
900900
this.queue.push([command].concat(key));
901901
} else if (Array.isArray(arg)) {
902902
if (callback) {
903-
arg.push(callback);
903+
arg = arg.concat([callback]);
904904
}
905905
this.queue.push([command, key].concat(arg));
906906
} else {
@@ -980,12 +980,12 @@ Multi.prototype.hmset = Multi.prototype.HMSET = function (key, args, callback) {
980980
var tmp_args, field;
981981
if (Array.isArray(key)) {
982982
if (args) {
983-
key.push(args);
983+
key = key.concat([args]);
984984
}
985985
tmp_args = ['hmset'].concat(key);
986986
} else if (Array.isArray(args)) {
987987
if (callback) {
988-
args.push(callback);
988+
args = args.concat([callback]);
989989
}
990990
tmp_args = ['hmset', key].concat(args);
991991
} else if (typeof args === "object") {
@@ -1029,7 +1029,7 @@ Multi.prototype.exec = Multi.prototype.EXEC = function (callback) {
10291029
this.wants_buffers = new Array(this.queue.length);
10301030
// drain queue, callback will catch "QUEUED" or error
10311031
for (var index = 0; index < this.queue.length; index++) {
1032-
var args = this.queue[index].slice();
1032+
var args = this.queue[index].slice(0);
10331033
var command = args.shift();
10341034
var cb;
10351035
if (typeof args[args.length - 1] === "function") {

test/commands/hmget.spec.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ describe("The 'hmget' method", function () {
3030
});
3131
});
3232

33-
it('allows keys to be specified by passing an array', function (done) {
34-
client.HMGET(hash, ["0123456789", "some manner of key"], function (err, reply) {
33+
it('allows keys to be specified by passing an array without manipulating the array', function (done) {
34+
var data = ["0123456789", "some manner of key"];
35+
client.HMGET(hash, data, function (err, reply) {
36+
assert.strictEqual(data.length, 2);
3537
assert.strictEqual("abcdefghij", reply[0].toString());
3638
assert.strictEqual("a type of value", reply[1].toString());
3739
return done(err);

test/commands/multi.spec.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -148,26 +148,32 @@ describe("The 'multi' method", function () {
148148

149149
it('allows multiple operations to be performed using constructor with all kinds of syntax', function (done) {
150150
var now = Date.now();
151+
var arr = ["multihmset", "multibar", "multibaz"];
152+
var arr2 = ['some manner of key', 'otherTypes'];
153+
var arr3 = [5768, "multibarx", "multifoox"];
151154
client.multi([
152155
["mset", [578, "multibar"], helper.isString('OK')],
153156
[["mset", "multifoo2", "multibar2", "multifoo3", "multibar3"], helper.isString('OK')],
154-
["hmset", ["multihmset", "multibar", "multibaz"]],
157+
["hmset", arr],
155158
[["hmset", "multihmset2", "multibar2", "multifoo3", "multibar3", "test", helper.isString('OK')]],
156159
["hmset", ["multihmset", "multibar", "multifoo", helper.isString('OK')]],
157-
["hmset", [5768, "multibarx", "multifoox"], helper.isString('OK')],
160+
["hmset", arr3, helper.isString('OK')],
158161
['hmset', now, {123456789: "abcdefghij", "some manner of key": "a type of value", "otherTypes": 555}],
159162
['hmset', 'key2', {"0123456789": "abcdefghij", "some manner of key": "a type of value", "otherTypes": 999}, helper.isString('OK')],
160163
["hmset", "multihmset", ["multibar", "multibaz"]],
161164
["hmset", "multihmset", ["multibar", "multibaz"], helper.isString('OK')],
162165
])
163166
.hmget(now, 123456789, 'otherTypes')
164-
.hmget('key2', ['some manner of key', 'otherTypes'])
167+
.hmget('key2', arr2, function noop() {})
165168
.hmget(['multihmset2', 'some manner of key', 'multibar3'])
166169
.mget('multifoo2', ['multifoo3', 'multifoo'], function(err, res) {
167170
assert(res[0], 'multifoo3');
168171
assert(res[1], 'multifoo');
169172
})
170173
.exec(function (err, replies) {
174+
assert.equal(arr.length, 3);
175+
assert.equal(arr2.length, 2);
176+
assert.equal(arr3.length, 3);
171177
assert.strictEqual(null, err);
172178
assert.equal(replies[10][1], '555');
173179
assert.equal(replies[11][0], 'a type of value');

0 commit comments

Comments
 (0)