diff --git a/sdk/lib/_internal/wasm/lib/growable_list.dart b/sdk/lib/_internal/wasm/lib/growable_list.dart index 513c1ef47e5..f824469b0d2 100644 --- a/sdk/lib/_internal/wasm/lib/growable_list.dart +++ b/sdk/lib/_internal/wasm/lib/growable_list.dart @@ -97,7 +97,7 @@ class _GrowableList extends _ModifiableList { data = WasmArray(_nextCapacity(_capacity)); if (index != 0) { // Copy elements before the insertion point. - data.copy(0, _data, 0, index - 1); + data.copy(0, _data, 0, index); } } else { data = _data; diff --git a/tests/lib/collection/regress_54845_test.dart b/tests/lib/collection/regress_54845_test.dart new file mode 100644 index 00000000000..6354b5cbb77 --- /dev/null +++ b/tests/lib/collection/regress_54845_test.dart @@ -0,0 +1,13 @@ +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:expect/expect.dart'; + +main() { + final l = [10, 20, 30]; + for (int i = l.length; i-- > 0;) { + if (i > 0) l.insert(i, -1); + } + Expect.deepEquals([10, -1, 20, -1, 30], l); +}