16a380
diff -ru icu/source/common/unistr.cpp icu.new/source/common/unistr.cpp
16a380
--- icu/source/common/unistr.cpp	2019-04-12 00:26:16.000000000 +0200
16a380
+++ icu.new/source/common/unistr.cpp	2020-03-03 15:39:37.069874709 +0100
16a380
@@ -1544,7 +1544,11 @@
16a380
   }
16a380
 
16a380
   int32_t oldLength = length();
16a380
-  int32_t newLength = oldLength + srcLength;
16a380
+  int32_t newLength;
16a380
+  if (uprv_add32_overflow(oldLength, srcLength, &newLength)) {
16a380
+    setToBogus();
16a380
+    return *this;
16a380
+  }
16a380
   // optimize append() onto a large-enough, owned string
16a380
   if((newLength <= getCapacity() && isBufferWritable()) ||
16a380
       cloneArrayIfNeeded(newLength, getGrowCapacity(newLength))) {
16a380
diff -ru icu/source/test/intltest/ustrtest.cpp icu.new/source/test/intltest/ustrtest.cpp
16a380
--- icu/source/test/intltest/ustrtest.cpp	2019-04-12 00:26:16.000000000 +0200
16a380
+++ icu.new/source/test/intltest/ustrtest.cpp	2020-03-03 15:44:59.059239188 +0100
16a380
@@ -64,6 +64,7 @@
16a380
     TESTCASE_AUTO(TestUInt16Pointers);
16a380
     TESTCASE_AUTO(TestWCharPointers);
16a380
     TESTCASE_AUTO(TestNullPointers);
16a380
+    TESTCASE_AUTO(TestLargeAppend);
16a380
     TESTCASE_AUTO_END;
16a380
 }
16a380
 
16a380
@@ -2248,3 +2249,64 @@
16a380
     UnicodeString(u"def").extract(nullptr, 0, errorCode);
16a380
     assertEquals("buffer overflow extracting to nullptr", U_BUFFER_OVERFLOW_ERROR, errorCode);
16a380
 }
16a380
+
16a380
+void UnicodeStringTest::TestLargeAppend() {
16a380
+    if(quick) return;
16a380
+
16a380
+    IcuTestErrorCode status(*this, "TestLargeAppend");
16a380
+    // Make a large UnicodeString
16a380
+    int32_t len = 0xAFFFFFF;
16a380
+    UnicodeString str;
16a380
+    char16_t *buf = str.getBuffer(len);
16a380
+    // A fast way to set buffer to valid Unicode.
16a380
+    // 4E4E is a valid unicode character
16a380
+    uprv_memset(buf, 0x4e, len * 2);
16a380
+    str.releaseBuffer(len);
16a380
+    UnicodeString dest;
16a380
+    // Append it 16 times
16a380
+    // 0xAFFFFFF times 16 is 0xA4FFFFF1,
16a380
+    // which is greater than INT32_MAX, which is 0x7FFFFFFF.
16a380
+    int64_t total = 0;
16a380
+    for (int32_t i = 0; i < 16; i++) {
16a380
+        dest.append(str);
16a380
+        total += len;
16a380
+        if (total <= INT32_MAX) {
16a380
+            assertFalse("dest is not bogus", dest.isBogus());
16a380
+        } else {
16a380
+            assertTrue("dest should be bogus", dest.isBogus());
16a380
+        }
16a380
+    }
16a380
+    dest.remove();
16a380
+    total = 0;
16a380
+    for (int32_t i = 0; i < 16; i++) {
16a380
+        dest.append(str);
16a380
+        total += len;
16a380
+        if (total + len <= INT32_MAX) {
16a380
+            assertFalse("dest is not bogus", dest.isBogus());
16a380
+        } else if (total <= INT32_MAX) {
16a380
+            // Check that a string of exactly the maximum size works
16a380
+            UnicodeString str2;
16a380
+            int32_t remain = INT32_MAX - total;
16a380
+            char16_t *buf2 = str2.getBuffer(remain);
16a380
+            if (buf2 == nullptr) {
16a380
+                // if somehow memory allocation fail, return the test
16a380
+                return;
16a380
+            }
16a380
+            uprv_memset(buf2, 0x4e, remain * 2);
16a380
+            str2.releaseBuffer(remain);
16a380
+            dest.append(str2);
16a380
+            total += remain;
16a380
+            assertEquals("When a string of exactly the maximum size works", (int64_t)INT32_MAX, total);
16a380
+            assertEquals("When a string of exactly the maximum size works", INT32_MAX, dest.length());
16a380
+            assertFalse("dest is not bogus", dest.isBogus());
16a380
+
16a380
+            // Check that a string size+1 goes bogus
16a380
+            str2.truncate(1);
16a380
+            dest.append(str2);
16a380
+            total++;
16a380
+            assertTrue("dest should be bogus", dest.isBogus());
16a380
+        } else {
16a380
+            assertTrue("dest should be bogus", dest.isBogus());
16a380
+        }
16a380
+    }
16a380
+}
16a380
diff -ru icu/source/test/intltest/ustrtest.h icu.new/source/test/intltest/ustrtest.h
16a380
--- icu/source/test/intltest/ustrtest.h	2019-04-12 00:26:16.000000000 +0200
16a380
+++ icu.new/source/test/intltest/ustrtest.h	2020-03-03 15:45:36.147935611 +0100
16a380
@@ -96,6 +96,7 @@
16a380
     void TestUInt16Pointers();
16a380
     void TestWCharPointers();
16a380
     void TestNullPointers();
16a380
+    void TestLargeAppend();
16a380
 };
16a380
 
16a380
 #endif