dart-lang / core

This repository is home to core Dart packages.
https://pub.dev/publishers/dart.dev
BSD 3-Clause "New" or "Revised" License
20 stars 8 forks source link

ListExtensions.slice: infinite recursion when calling slice on a ListSlice<T> declared as List<T> #661

Open ToufiPF opened 1 year ago

ToufiPF commented 1 year ago

Consider this snippet:

void recursivePrint(List<int> list) {
  if (list.isEmpty) {
    return;
  }
  print("${list.first} ");
  recursivePrint(list.slice(1));
}
recursivePrint([0, 1, 2, 3]);

Expected output

Printing the list: "0 1 2 3".

Actual behavior

Calling ListExtensions.slice when the List is actually a ListSlice enters an infinite recursion, and triggers a StackOverflow. Stacktrace:

[...]
package:collection/src/list_extensions.dart 244:40  ListExtensions.slice
package:collection/src/list_extensions.dart 244:40  ListExtensions.slice
package:collection/src/list_extensions.dart 244:40  ListExtensions.slice
package:collection/src/list_extensions.dart 244:40  ListExtensions.slice
package:collection/src/list_extensions.dart 244:40  ListExtensions.slice
package:collection/src/list_extensions.dart 244:40  ListExtensions.slice
package:collection/src/list_extensions.dart 244:40  ListExtensions.slice
package:collection/src/list_extensions.dart 244:40  ListExtensions.slice
test\bug_slice.dart 11:17                           recursivePrint
test\bug_slice.dart 11:3                            recursivePrint
test\bug_slice.dart 16:5                            main.<fn>

Auxilliary info

The following snippet will not throw, but has incorrect behavior: it prints "0 2 2 2" instead of "0 1 2 3"

void recPrint2(ListSlice<int> list) {
  if (list.isEmpty) {
    return;
  }
  print("${list.first} ");
  recPrint2(list.slice(1));
}
recPrint2([0, 1, 2, 3].slice(0));

Version

Collection version: 1.17.2

Flutter 3.12.0-1.1.pre • channel beta • https://github.com/flutter/flutter.git
Framework • revision e2e0aa7037 (3 weeks ago) • 2023-06-20 19:48:59 -0700
Engine • revision 45712dd2f1
Tools • Dart 3.1.0 (build 3.1.0-163.1.beta) • DevTools 2.24.0
gnprice commented 1 year ago

This appears to be a duplicate of dart-lang/core#656.

That issue was fixed, but it looks like there hasn't been a release since the fix. So this report can be taken as a reminder that it would be good to make a release.