From 26aa50fdbfdc47e4e0025a2fa9c085aa81ba15a4 Mon Sep 17 00:00:00 2001 From: Martin Kustermann Date: Thu, 8 Feb 2024 14:05:44 +0000 Subject: [PATCH] [dart2wasm] Fix off-by-one error in List.insert() Closes https://github.com/dart-lang/sdk/issues/54845 TEST=lib/collection/regress_54845_test Change-Id: I1af7f59a57c70ef5ec724b089555ebbcbd88a484 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/351120 Commit-Queue: Martin Kustermann Reviewed-by: Slava Egorov --- sdk/lib/_internal/wasm/lib/growable_list.dart | 2 +- tests/lib/collection/regress_54845_test.dart | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 tests/lib/collection/regress_54845_test.dart 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); +}