]> granicus.if.org Git - icu/commitdiff
ICU-20832 use uint32_t instead of uint16_t to avoid overflows for very long strings
authorAdam Sitnik <adam.sitnik@microsoft.com>
Wed, 25 Sep 2019 06:06:10 +0000 (08:06 +0200)
committerJeff Genovy <29107334+jefgen@users.noreply.github.com>
Wed, 25 Sep 2019 21:20:56 +0000 (14:20 -0700)
icu4c/source/i18n/usearch.cpp
icu4c/source/test/cintltst/usrchtst.c

index ea2cce1206c927fb96415d6a0a06b370b67a7fe0..8866de703375237366d4bda3a1784c2e5be260cc 100644 (file)
@@ -317,7 +317,7 @@ inline uint16_t initializePatternCETable(UStringSearch *strsrch,
         uprv_free(pattern->ces);
     }
 
-    uint16_t  offset      = 0;
+    uint32_t  offset      = 0;
     uint16_t  result      = 0;
     int32_t   ce;
 
@@ -388,7 +388,7 @@ inline uint16_t initializePatternPCETable(UStringSearch *strsrch,
         uprv_free(pattern->pces);
     }
 
-    uint16_t  offset = 0;
+    uint32_t  offset = 0;
     uint16_t  result = 0;
     int64_t   pce;
 
index f91aa081ae4e6dcf4e4e59a83d01d9679ab1edd3..7a683a063d0f87900f6d378d5bcc437c48b686ef 100644 (file)
@@ -2894,6 +2894,43 @@ exit:
    return;
 }
 
+static void TestUInt16Overflow(void) {
+    const int32_t uint16_overflow = UINT16_MAX + 1;
+    UChar* pattern = (UChar*)uprv_malloc(uint16_overflow * sizeof(UChar));
+    if (pattern == NULL)
+    {
+        log_err("Err: uprv_malloc returned NULL\n");
+        return;
+    }
+    u_memset(pattern, 'A', uint16_overflow);
+    UChar text[] = { 'B' };
+
+    UErrorCode errorCode = U_ZERO_ERROR;
+    UStringSearch* usearch = usearch_open(pattern, uint16_overflow, text, 1, "en-US", NULL, &errorCode);
+
+    if (U_SUCCESS(errorCode))
+    {
+        int32_t match = usearch_first(usearch, &errorCode);
+
+        if (U_SUCCESS(errorCode))
+        {
+            if (match != USEARCH_DONE)
+            {
+                log_err("Err: match was not expected, got %d\n", match);
+            }
+        }
+        else
+        {
+            log_err("usearch_first error %s\n", u_errorName(errorCode));
+        }
+        usearch_close(usearch);
+    }
+    else
+    {
+        log_err("usearch_open error %s\n", u_errorName(errorCode));
+    }
+    uprv_free(pattern);
+}
 
 static void TestPCEBuffer_100df(void) {
   UChar search[] =
@@ -3070,6 +3107,7 @@ void addSearchTest(TestNode** root)
     addTest(root, &TestPCEBuffer_2surr, "tscoll/usrchtst/TestPCEBuffer/2_dfff");
     addTest(root, &TestMatchFollowedByIgnorables, "tscoll/usrchtst/TestMatchFollowedByIgnorables");
     addTest(root, &TestIndicPrefixMatch, "tscoll/usrchtst/TestIndicPrefixMatch");
+    addTest(root, &TestUInt16Overflow, "tscoll/usrchtst/TestUInt16Overflow");
 }
 
 #endif /* #if !UCONFIG_NO_COLLATION */