]> granicus.if.org Git - icu/commitdiff
ICU-21823 Adding changes to fix charset detection incase of no match
authorshaobero <shaobero@adobe.com>
Thu, 4 Nov 2021 17:57:12 +0000 (17:57 +0000)
committerMarkus Scherer <markus.icu@gmail.com>
Tue, 15 Mar 2022 21:47:40 +0000 (21:47 +0000)
icu4c/source/i18n/csdetect.cpp
icu4c/source/test/cintltst/ucsdetst.c
icu4c/source/test/intltest/csdetest.cpp
icu4c/source/test/intltest/csdetest.h

index 84f0776542d896869fcaa83e545b04b2f54a4383..d866eb66286811399e6fbad58f979e2dcd073eb5 100644 (file)
@@ -270,6 +270,11 @@ const CharsetMatch * const *CharsetDetector::detectAll(int32_t &maxMatchesFound,
 
     maxMatchesFound = resultCount;
 
+    if (maxMatchesFound == 0) {
+        status = U_INVALID_CHAR_FOUND;
+        return NULL;
+    }
+
     return resultArray;
 }
 
index f65f8d79aa4fa63eb686f8cb371c78b7b8a0770a..b8d3b5f20b81209eafea2f729c2a800d9dfb07c3 100644 (file)
@@ -402,6 +402,7 @@ static void TestBufferOverflow(void) {
     }
 
     for (idx = 0; idx < UPRV_LENGTHOF(testStrings); idx++) {
+        status = U_ZERO_ERROR;
         ucsdet_setText(csd, testStrings[idx], -1, &status);
         match = ucsdet_detect(csd, &status);
 
index 66f9340b83a2342eb0ca0c91c20baec3653767dd..95f19d43d1adc41fbd954a1f1e6d480ff331c82c 100644 (file)
@@ -109,6 +109,10 @@ void CharsetDetectionTest::runIndexedTest( int32_t index, UBool exec, const char
             if (exec) Ticket6954Test();
             break;
 
+       case 10: name = "Ticket21823Test";
+            if (exec) Ticket21823Test();
+            break;
+
         default: name = "";
             break; //needed to end loop
     }
@@ -839,3 +843,22 @@ void CharsetDetectionTest::Ticket6954Test() {
     TEST_ASSERT(strcmp(name1, "windows-1252")==0);
 #endif
 }
+
+
+// Ticket 21823 - Issue with Charset Detector for ill-formed input strings. 
+//                Its fix involves returning a failure based error code 
+//                (U_INVALID_CHAR_FOUND) incase no charsets appear to match the input data.
+void CharsetDetectionTest::Ticket21823Test() {
+    UErrorCode status = U_ZERO_ERROR;
+    std::string str = "\x80";
+    UCharsetDetector* csd = ucsdet_open(&status);
+
+    ucsdet_setText(csd, str.data(), str.length(), &status);
+    const UCharsetMatch* match = ucsdet_detect(csd, &status);
+
+    if (match == NULL) {
+        TEST_ASSERT(U_FAILURE(status));
+    }
+
+    ucsdet_close(csd);
+}
index 72d1ca92aea1dcefe1c6bb5ccd48c3922122d702..8893b88e5916613ab8954f6868ae7214dc71b91d 100644 (file)
@@ -33,6 +33,7 @@ public:
     virtual void IBM420Test();
     virtual void Ticket6394Test();
     virtual void Ticket6954Test();
+    virtual void Ticket21823Test();
 
 private:
     void checkEncoding(const UnicodeString &testString,