Skip to content

Commit 6addf8c

Browse files
committed
[Fix] parse: mark overflow objects for indexed notation exceeding arrayLimit
Fixes #546
1 parent cfc108f commit 6addf8c

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

lib/parse.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ var parseObject = function (chain, val, options, valuesParsed) {
199199
obj[index] = leaf;
200200
} else if (isValidArrayIndex && options.throwOnLimitExceeded) {
201201
throw new RangeError('Array limit exceeded. Only ' + options.arrayLimit + ' element' + (options.arrayLimit === 1 ? '' : 's') + ' allowed in an array.');
202+
} else if (isValidArrayIndex) {
203+
obj[index] = leaf;
204+
utils.markOverflow(obj, index);
202205
} else if (decodedRoot !== '__proto__') {
203206
obj[decodedRoot] = leaf;
204207
}

lib/utils.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@ var merge = function merge(target, source, options) {
147147
} else {
148148
acc[key] = value;
149149
}
150+
151+
if (isOverflow(source) && !isOverflow(acc)) {
152+
markOverflow(acc, getMaxIndex(source));
153+
}
154+
if (isOverflow(acc)) {
155+
var keyNum = parseInt(key, 10);
156+
if (String(keyNum) === key && keyNum >= 0 && keyNum > getMaxIndex(acc)) {
157+
setMaxIndex(acc, keyNum);
158+
}
159+
}
160+
150161
return acc;
151162
}, mergeTarget);
152163
};
@@ -323,6 +334,7 @@ module.exports = {
323334
isBuffer: isBuffer,
324335
isOverflow: isOverflow,
325336
isRegExp: isRegExp,
337+
markOverflow: markOverflow,
326338
maybeMap: maybeMap,
327339
merge: merge
328340
};

test/parse.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,5 +1484,29 @@ test('mixed array and object notation', function (t) {
14841484
st.end();
14851485
});
14861486

1487+
t.test('mixed notation produces consistent results when arrayLimit is exceeded', function (st) {
1488+
var expected = { a: { 0: 'b', 1: 'c', 2: 'd' } };
1489+
1490+
st.deepEqual(
1491+
qs.parse('a[]=b&a[1]=c&a=d', { arrayLimit: -1 }),
1492+
expected,
1493+
'arrayLimit -1'
1494+
);
1495+
1496+
st.deepEqual(
1497+
qs.parse('a[]=b&a[1]=c&a=d', { arrayLimit: 0 }),
1498+
expected,
1499+
'arrayLimit 0'
1500+
);
1501+
1502+
st.deepEqual(
1503+
qs.parse('a[]=b&a[1]=c&a=d', { arrayLimit: 1 }),
1504+
expected,
1505+
'arrayLimit 1'
1506+
);
1507+
1508+
st.end();
1509+
});
1510+
14871511
t.end();
14881512
});

0 commit comments

Comments
 (0)