Skip to content

Commit 2872608

Browse files
committed
🦺 add strict list limit checks for empty root input in decode tests
1 parent efbac46 commit 2872608

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

lib/src/extensions/decode.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,10 +405,13 @@ extension _$Decode on QS {
405405
DecodeOptions options,
406406
bool valuesParsed,
407407
) {
408+
final bool isListGrowthPath =
409+
chain.isNotEmpty && chain.last == '[]' && options.parseLists;
410+
408411
// Determine the current list length if we are appending into `[]`.
409412
late final int currentListLength;
410413

411-
if (chain.length >= 2 && chain.last == '[]') {
414+
if (isListGrowthPath && chain.length >= 2) {
412415
final String prev = chain[chain.length - 2];
413416
final bool bracketed = prev.startsWith('[') && prev.endsWith(']');
414417
final int? parentIndex =
@@ -433,7 +436,7 @@ extension _$Decode on QS {
433436
val,
434437
options,
435438
currentListLength,
436-
chain.length >= 2 && chain.last == '[]' && options.parseLists,
439+
isListGrowthPath,
437440
);
438441

439442
for (int i = chain.length - 1; i >= 0; --i) {

test/unit/decode_test.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,6 +2113,38 @@ void main() {
21132113
});
21142114

21152115
group('list limit tests', () {
2116+
test('map input root [] enforces strict list limit checks', () {
2117+
expect(
2118+
() => QS.decode(
2119+
{'[]': 'a'},
2120+
const DecodeOptions(listLimit: 0, throwOnLimitExceeded: true),
2121+
),
2122+
throwsA(
2123+
isA<RangeError>().having(
2124+
(e) => e.message,
2125+
'message',
2126+
contains('List limit exceeded'),
2127+
),
2128+
),
2129+
);
2130+
});
2131+
2132+
test('map input root [] enforces negative listLimit strict mode', () {
2133+
expect(
2134+
() => QS.decode(
2135+
{'[]': 'a'},
2136+
const DecodeOptions(listLimit: -1, throwOnLimitExceeded: true),
2137+
),
2138+
throwsA(
2139+
isA<RangeError>().having(
2140+
(e) => e.message,
2141+
'message',
2142+
contains('List parsing is disabled'),
2143+
),
2144+
),
2145+
);
2146+
});
2147+
21162148
test('strict list limit applies when duplicate scalar grows into a list',
21172149
() {
21182150
expect(

0 commit comments

Comments
 (0)