[vm/corelib] Fix ConcurrentModificationError for empty addAll.

Since we only add elements if the iterator is non-empty, we
shouldn't get a ConcurrentModificationError if both the iterator
and receiver are the same empty EfficientLengthIterator.

Fixes https://github.com/dart-lang/sdk/issues/42011

TEST=New regression test added that fails without the sdk change.

Fixed: 42011
Change-Id: Ib5259c8f043cc8277d2fea407d1f64a24430c4d3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/173901
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Tess Strickland <sstrickl@google.com>
This commit is contained in:
Tess Strickland 2020-11-25 12:01:44 +00:00 committed by commit-bot@chromium.org
parent b64550eaff
commit 367761987b
3 changed files with 23 additions and 0 deletions

View file

@ -211,6 +211,9 @@ class _GrowableList<T> extends ListBase<T> {
var cap = _capacity;
// Pregrow if we know iterable.length.
var iterLen = iterable.length;
if (iterLen == 0) {
return;
}
var newLen = len + iterLen;
if (newLen > cap) {
do {

View file

@ -0,0 +1,10 @@
// Copyright (c) 2020, 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.
// Regression test for https://github.com/dart-lang/sdk/issues/42011
main() {
var a = [];
// No elements are added, so should not get a ConcurrentModificationError.
a.addAll(a);
}

View file

@ -0,0 +1,10 @@
// Copyright (c) 2020, 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.
// Regression test for https://github.com/dart-lang/sdk/issues/42011
main() {
var a = [];
// No elements are added, so should not get a ConcurrentModificationError.
a.addAll(a);
}