ICU-10889 test & fix u_charFromName(U_EXTENDED_CHAR_NAME) underflow crash

X-SVN-Rev: 36293
This commit is contained in:
Markus Scherer 2014-08-29 21:31:07 +00:00
parent 6860fda7cd
commit b422082fab
2 changed files with 36 additions and 4 deletions

View File

@ -1554,15 +1554,16 @@ u_charFromName(UCharNameChoice nameChoice,
*pErrorCode = U_ILLEGAL_CHAR_FOUND;
return error;
}
// i==strlen(name)==strlen(lower)==strlen(upper)
/* try extended names first */
if (lower[0] == '<') {
if (nameChoice == U_EXTENDED_CHAR_NAME) {
if (lower[--i] == '>') {
for (--i; lower[i] && lower[i] != '-'; --i) {
}
// Parse a string like "<category-HHHH>" where HHHH is a hex code point.
if (lower[--i] == '>' && i >= 3 && lower[--i] != '-') {
while (i >= 3 && lower[--i] != '-') {}
if (lower[i] == '-') { /* We've got a category. */
if (i >= 2 && lower[i] == '-') {
uint32_t cIdx;
lower[i] = 0;

View File

@ -49,6 +49,7 @@ static void TestCodeUnit(void);
static void TestCodePoint(void);
static void TestCharLength(void);
static void TestCharNames(void);
static void TestUCharFromNameUnderflow(void);
static void TestMirroring(void);
static void TestUScriptRunAPI(void);
static void TestAdditionalProperties(void);
@ -183,6 +184,7 @@ void addUnicodeTest(TestNode** root)
addTest(root, &TestControlPrint, "tsutil/cucdtst/TestControlPrint");
addTest(root, &TestIdentifier, "tsutil/cucdtst/TestIdentifier");
addTest(root, &TestCharNames, "tsutil/cucdtst/TestCharNames");
addTest(root, &TestUCharFromNameUnderflow, "tsutil/cucdtst/TestUCharFromNameUnderflow");
addTest(root, &TestMirroring, "tsutil/cucdtst/TestMirroring");
addTest(root, &TestUScriptCodeAPI, "tsutil/cucdtst/TestUScriptCodeAPI");
addTest(root, &TestHasScript, "tsutil/cucdtst/TestHasScript");
@ -1937,6 +1939,35 @@ TestCharNames() {
/* ### TODO: test error cases and other interesting things */
}
static void
TestUCharFromNameUnderflow() {
// Ticket #10889: Underflow crash when there is no dash.
UErrorCode errorCode=U_ZERO_ERROR;
UChar32 c=u_charFromName(U_EXTENDED_CHAR_NAME, "<NO BREAK SPACE>", &errorCode);
if(U_SUCCESS(errorCode)) {
log_err("u_charFromName(<NO BREAK SPACE>) = U+%04x but should fail - %s\n", c, u_errorName(errorCode));
}
// Test related edge cases.
errorCode=U_ZERO_ERROR;
c=u_charFromName(U_EXTENDED_CHAR_NAME, "<-00a0>", &errorCode);
if(U_SUCCESS(errorCode)) {
log_err("u_charFromName(<-00a0>) = U+%04x but should fail - %s\n", c, u_errorName(errorCode));
}
errorCode=U_ZERO_ERROR;
c=u_charFromName(U_EXTENDED_CHAR_NAME, "<control->", &errorCode);
if(U_SUCCESS(errorCode)) {
log_err("u_charFromName(<control->) = U+%04x but should fail - %s\n", c, u_errorName(errorCode));
}
errorCode=U_ZERO_ERROR;
c=u_charFromName(U_EXTENDED_CHAR_NAME, "<control-111111>", &errorCode);
if(U_SUCCESS(errorCode)) {
log_err("u_charFromName(<control-111111>) = U+%04x but should fail - %s\n", c, u_errorName(errorCode));
}
}
/* test u_isMirrored() and u_charMirror() ----------------------------------- */
static void