From adc79bc389a83348d18ffe2c0bb7c8014c06c6fc Mon Sep 17 00:00:00 2001 From: William Hesse Date: Fri, 27 Oct 2023 12:36:29 +0000 Subject: [PATCH] Avoid crashes in string concatenation 32-bit integer overflow test The test standalone/string_overflow_test.dart is causing crashes and out-of-memory thrashing on some configurations. It was originally a regression test that concatenating a string with 2^31 characters did not overflow a 32-bit signed integer. It was changed to exponentially grow a string until an out-of-memory runtime exception is thrown in Dart. On some VM configurations, this crashes the machine instead. Restoring it to only check concatenating a string of 2^31 bytes. Bug: https://github.com/dart-lang/sdk/issues/46225 Change-Id: Ib50625fe14be2613c907a7bc30cd57766693f6aa Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/332423 Reviewed-by: Martin Kustermann --- tests/standalone/string_overflow_test.dart | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tests/standalone/string_overflow_test.dart b/tests/standalone/string_overflow_test.dart index d63a5b11888..a51fb25261b 100644 --- a/tests/standalone/string_overflow_test.dart +++ b/tests/standalone/string_overflow_test.dart @@ -3,22 +3,22 @@ // BSD-style license that can be found in the LICENSE file. // Test to ensure that the VM does not have an integer overflow issue -// when concatenating strings. +// when concatenating strings and hitting length 2^31. // See https://github.com/dart-lang/sdk/issues/11214 import "package:expect/expect.dart"; main() { + const length28bits = 1 << 28; String a = "a"; - - var caughtOutOfMemoryException = false; - try { - while (true) { - a = "$a$a$a$a$a$a$a$a"; - } - } on OutOfMemoryError { - caughtOutOfMemoryException = true; + while (a.length < length28bits) { + a = a + a; + } + Expect.equals(a.length, length28bits); + try { + final concat = "$a$a$a$a$a$a$a$a"; + Expect.equals(concat.length, 8 * length28bits); + } on OutOfMemoryError { + // Allow test to run out of memory instead. } - Expect.isTrue(caughtOutOfMemoryException); - Expect.isTrue(a.startsWith('aaaaa') && a.length > 1024); }