mirror of
https://github.com/dart-lang/sdk
synced 2024-10-14 10:48:25 +00:00
[vm/corelib/nnbd] Fix null check in _GrowableList.length=
We should verify that element type is nullable every time length is increased, not only when capacity is increased. Fixes https://github.com/dart-lang/sdk/issues/42502 Change-Id: Id91c702a99028634da8c2d41ae0ceac521af2cf5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/152900 Reviewed-by: Régis Crelier <regis@google.com> Reviewed-by: Lasse R.H. Nielsen <lrn@google.com> Commit-Queue: Alexander Markov <alexmarkov@google.com>
This commit is contained in:
parent
97ba0acc72
commit
e6cbfd71f1
|
@ -120,15 +120,16 @@ class _GrowableList<T> extends ListBase<T> {
|
|||
int get length native "GrowableList_getLength";
|
||||
|
||||
void set length(int new_length) {
|
||||
int old_capacity = _capacity;
|
||||
int new_capacity = new_length;
|
||||
if (new_capacity > old_capacity) {
|
||||
if (new_length > length) {
|
||||
// Verify that element type is nullable.
|
||||
null as T;
|
||||
_grow(new_capacity);
|
||||
if (new_length > _capacity) {
|
||||
_grow(new_length);
|
||||
}
|
||||
_setLength(new_length);
|
||||
return;
|
||||
}
|
||||
final int new_capacity = new_length;
|
||||
// We are shrinking. Pick the method which has fewer writes.
|
||||
// In the shrink-to-fit path, we write |new_capacity + new_length| words
|
||||
// (null init + copy).
|
||||
|
|
27
tests/corelib/regress_42502_test.dart
Normal file
27
tests/corelib/regress_42502_test.dart
Normal file
|
@ -0,0 +1,27 @@
|
|||
// 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/42502.
|
||||
// Requirements=nnbd-strong
|
||||
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
void main() {
|
||||
List<int> x = [];
|
||||
Expect.throws(() {
|
||||
x.length = x.length + 1;
|
||||
});
|
||||
Expect.equals(0, x.length);
|
||||
x.add(222);
|
||||
Expect.equals(1, x.length);
|
||||
Expect.throws(() {
|
||||
x.length = 2;
|
||||
});
|
||||
Expect.equals(1, x.length);
|
||||
Expect.throws(() {
|
||||
x.length = x.length + 1;
|
||||
});
|
||||
Expect.equals(1, x.length);
|
||||
Expect.equals(222, x[0]);
|
||||
}
|
Loading…
Reference in a new issue