From 53adb4bca86b75c4a77b60d9081e2e5ea4501ffa Mon Sep 17 00:00:00 2001 From: Frank Tang Date: Wed, 26 Feb 2025 18:53:57 -0800 Subject: [PATCH] ICU-23060 Fix TestLargeAppend Correct the test based on the kMaxCapacity setToBogus on error --- icu4c/source/common/unistr.cpp | 2 ++ icu4c/source/test/intltest/ustrtest.cpp | 19 +++++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/icu4c/source/common/unistr.cpp b/icu4c/source/common/unistr.cpp index 7b48addbe85..4e29bad1d3b 100644 --- a/icu4c/source/common/unistr.cpp +++ b/icu4c/source/common/unistr.cpp @@ -1946,9 +1946,11 @@ UnicodeString::cloneArrayIfNeeded(int32_t newCapacity, } else if(newCapacity <= US_STACKBUF_SIZE && growCapacity > US_STACKBUF_SIZE) { growCapacity = US_STACKBUF_SIZE; } else if(newCapacity > growCapacity) { + setToBogus(); return false; // bad inputs } if(growCapacity > kMaxCapacity) { + setToBogus(); return false; } diff --git a/icu4c/source/test/intltest/ustrtest.cpp b/icu4c/source/test/intltest/ustrtest.cpp index 31cb6519051..f0466982cff 100644 --- a/icu4c/source/test/intltest/ustrtest.cpp +++ b/icu4c/source/test/intltest/ustrtest.cpp @@ -2392,15 +2392,17 @@ void UnicodeStringTest::TestLargeAppend() { } dest.remove(); total = 0; + // Copy kMaxCapacity from common/unistr.cpp + const int32_t kMaxCapacity = 0x7ffffff5; for (int32_t i = 0; i < 16; i++) { dest.append(str); total += len; - if (total + len <= INT32_MAX) { + if (total + len <= kMaxCapacity) { assertFalse("dest is not bogus", dest.isBogus()); - } else if (total <= INT32_MAX) { + } else if (total <= kMaxCapacity) { // Check that a string of exactly the maximum size works UnicodeString str2; - int32_t remain = static_cast(INT32_MAX - total); + int32_t remain = static_cast(kMaxCapacity - total); char16_t *buf2 = str2.getBuffer(remain); if (buf2 == nullptr) { // if somehow memory allocation fail, return the test @@ -2410,14 +2412,19 @@ void UnicodeStringTest::TestLargeAppend() { str2.releaseBuffer(remain); dest.append(str2); total += remain; - assertEquals("When a string of exactly the maximum size works", static_cast(INT32_MAX), total); - assertEquals("When a string of exactly the maximum size works", INT32_MAX, dest.length()); + assertEquals("When a string of exactly the maximum size works", static_cast(kMaxCapacity), total); + assertEquals("When a string of exactly the maximum size works", kMaxCapacity, dest.length()); assertFalse("dest is not bogus", dest.isBogus()); - // Check that a string size+1 goes bogus + // Check that a string size+1 does not go bogus (one more byte reserved for NUL) str2.truncate(1); dest.append(str2); total++; + assertFalse("dest should be not bogus", dest.isBogus()); + // Check that a string size+2 goes bogus (beyond the byte reserved + // for NUL) + dest.append(str2); + total++; assertTrue("dest should be bogus", dest.isBogus()); } else { assertTrue("dest should be bogus", dest.isBogus());