Skip to content

Commit 07623b8

Browse files
committed
ICU-23165 Fixed crash in the ISO 2022-CN converter. There was an erroneous version check in one spot that could
cause certain input strings to dereference a null pointer because a particular conversion table wasn't allocated. Added an appropriate unit test.
1 parent 31f02d4 commit 07623b8

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

icu4c/source/common/ucnv2022.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ _ISO2022Open(UConverter *cnv, UConverterLoadArgs *pArgs, UErrorCode *errorCode){
597597
/* open the required converters and cache them */
598598
myConverterData->myConverterArray[GB2312_1] =
599599
ucnv_loadSharedData("ibm-5478", &stackPieces, &stackArgs, errorCode);
600-
if(version==1) {
600+
if(version>=1) {
601601
myConverterData->myConverterArray[ISO_IR_165] =
602602
ucnv_loadSharedData("iso-ir-165", &stackPieces, &stackArgs, errorCode);
603603
}

icu4c/source/test/cintltst/ncnvtst.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ static void TestFlushInternalBuffer(void); /*for improved code coverage in ucnv
7979
static void TestResetBehaviour(void);
8080
static void TestTruncated(void);
8181
static void TestUnicodeSet(void);
82+
static void TestISO2022Crash(void);
8283

8384
static void TestWithBufferSize(int32_t osize, int32_t isize);
8485

@@ -137,6 +138,7 @@ void addExtraTests(TestNode** root)
137138
addTest(root, &TestRegressionUTF32, "tsconv/ncnvtst/TestRegressionUTF32");
138139
addTest(root, &TestTruncated, "tsconv/ncnvtst/TestTruncated");
139140
addTest(root, &TestUnicodeSet, "tsconv/ncnvtst/TestUnicodeSet");
141+
addTest(root, &TestISO2022Crash, "tsconv/ncnvtst/TestISO2022Crash");
140142
}
141143

142144
/*test surrogate behaviour*/
@@ -2061,3 +2063,32 @@ TestUnicodeSet(void) {
20612063

20622064
uset_close(set);
20632065
}
2066+
2067+
// Test for https://unicode-org.atlassian.net/browse/ICU-23165
2068+
static void TestISO2022Crash(void) {
2069+
static const char offendingText[] = {
2070+
0x6d, 0x1b, 0x24, 0x29, 0x45, 0x65, 0x6c, 0x3a,
2071+
0x6c, 0x2e, 0x27, 0x41, 0x41, 0x0e, 0x41, 0x6c,
2072+
};
2073+
UErrorCode err = U_ZERO_ERROR;
2074+
UConverter * cnv = ucnv_open("ISO_2022,locale=zh,version=2", &err);
2075+
if (U_FAILURE(err)) {
2076+
log_data_err("Unable to open ISO-2022-CN converter: %s\n", u_errorName(err));
2077+
return;
2078+
}
2079+
ucnv_setToUCallBack(cnv, UCNV_TO_U_CALLBACK_ESCAPE, NULL, NULL, NULL, &err);
2080+
if (U_FAILURE(err)) {
2081+
log_data_err("Unable to setToUCallBack for ISO-2022-CN converter: %s\n", u_errorName(err));
2082+
ucnv_close(cnv);
2083+
return;
2084+
}
2085+
{
2086+
UChar toUChars[100];
2087+
UChar * toUCharsPtr = toUChars;
2088+
const UChar * toUCharsLimit = toUCharsPtr + 100;
2089+
const char * inCharsPtr = offendingText;
2090+
const char * inCharsLimit = offendingText + sizeof(offendingText);
2091+
ucnv_toUnicode(cnv, &toUCharsPtr, toUCharsLimit, &inCharsPtr, inCharsLimit, NULL, true, &err);
2092+
}
2093+
ucnv_close(cnv);
2094+
}

0 commit comments

Comments
 (0)