ICU-8421 uregex error checking fixes.

X-SVN-Rev: 29716
This commit is contained in:
Andy Heninger 2011-03-31 18:33:28 +00:00
parent ac61a7b5bc
commit 5481ac737a
2 changed files with 73 additions and 46 deletions

View file

@ -81,7 +81,7 @@ U_NAMESPACE_USE
// validateRE Do boilerplate style checks on API function parameters.
// Return TRUE if they look OK.
//----------------------------------------------------------------------------------------
static UBool validateRE(const RegularExpression *re, UErrorCode *status, UBool requiresText = TRUE) {
static UBool validateRE(const RegularExpression *re, UBool requiresText, UErrorCode *status) {
if (U_FAILURE(*status)) {
return FALSE;
}
@ -267,7 +267,7 @@ U_CAPI void U_EXPORT2
uregex_close(URegularExpression *re2) {
RegularExpression *re = (RegularExpression*)re2;
UErrorCode status = U_ZERO_ERROR;
if (validateRE(re, &status, FALSE) == FALSE) {
if (validateRE(re, FALSE, &status) == FALSE) {
return;
}
delete re;
@ -282,7 +282,7 @@ uregex_close(URegularExpression *re2) {
U_CAPI URegularExpression * U_EXPORT2
uregex_clone(const URegularExpression *source2, UErrorCode *status) {
RegularExpression *source = (RegularExpression*)source2;
if (validateRE(source, status, FALSE) == FALSE) {
if (validateRE(source, FALSE, status) == FALSE) {
return NULL;
}
@ -322,7 +322,7 @@ uregex_pattern(const URegularExpression *regexp2,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status, FALSE) == FALSE) {
if (validateRE(regexp, FALSE, status) == FALSE) {
return NULL;
}
if (patLength != NULL) {
@ -353,7 +353,7 @@ uregex_patternUText(const URegularExpression *regexp2,
U_CAPI int32_t U_EXPORT2
uregex_flags(const URegularExpression *regexp2, UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status, FALSE) == FALSE) {
if (validateRE(regexp, FALSE, status) == FALSE) {
return 0;
}
int32_t flags = regexp->fPat->flags();
@ -372,7 +372,7 @@ uregex_setText(URegularExpression *regexp2,
int32_t textLength,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status, FALSE) == FALSE) {
if (validateRE(regexp, FALSE, status) == FALSE) {
return;
}
if (text == NULL || textLength < -1) {
@ -405,7 +405,7 @@ uregex_setUText(URegularExpression *regexp2,
UText *text,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status, FALSE) == FALSE) {
if (validateRE(regexp, FALSE, status) == FALSE) {
return;
}
if (text == NULL) {
@ -435,7 +435,7 @@ uregex_getText(URegularExpression *regexp2,
int32_t *textLength,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status, FALSE) == FALSE) {
if (validateRE(regexp, FALSE, status) == FALSE) {
return NULL;
}
@ -475,7 +475,7 @@ uregex_getUText(URegularExpression *regexp2,
UText *dest,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status, FALSE) == FALSE) {
if (validateRE(regexp, FALSE, status) == FALSE) {
return dest;
}
return regexp->fMatcher->getInput(dest, *status);
@ -492,7 +492,7 @@ uregex_refreshUText(URegularExpression *regexp2,
UText *text,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status, FALSE) == FALSE) {
if (validateRE(regexp, FALSE, status) == FALSE) {
return;
}
regexp->fMatcher->refreshInputText(text, *status);
@ -517,7 +517,7 @@ uregex_matches64(URegularExpression *regexp2,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
UBool result = FALSE;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, TRUE, status) == FALSE) {
return result;
}
if (startIndex == -1) {
@ -547,7 +547,7 @@ uregex_lookingAt64(URegularExpression *regexp2,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
UBool result = FALSE;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, TRUE, status) == FALSE) {
return result;
}
if (startIndex == -1) {
@ -578,7 +578,7 @@ uregex_find64(URegularExpression *regexp2,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
UBool result = FALSE;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, TRUE, status) == FALSE) {
return result;
}
if (startIndex == -1) {
@ -600,7 +600,7 @@ U_CAPI UBool U_EXPORT2
uregex_findNext(URegularExpression *regexp2,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, TRUE, status) == FALSE) {
return FALSE;
}
UBool result = regexp->fMatcher->find();
@ -616,7 +616,7 @@ U_CAPI int32_t U_EXPORT2
uregex_groupCount(URegularExpression *regexp2,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status, FALSE) == FALSE) {
if (validateRE(regexp, FALSE, status) == FALSE) {
return 0;
}
int32_t result = regexp->fMatcher->groupCount();
@ -636,7 +636,7 @@ uregex_group(URegularExpression *regexp2,
int32_t destCapacity,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, TRUE, status) == FALSE) {
return 0;
}
if (destCapacity < 0 || (destCapacity > 0 && dest == NULL)) {
@ -699,7 +699,7 @@ uregex_groupUText(URegularExpression *regexp2,
int64_t *groupLength,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, TRUE, status) == FALSE) {
UErrorCode emptyTextStatus = U_ZERO_ERROR;
return (dest ? dest : utext_openUChars(NULL, NULL, 0, &emptyTextStatus));
}
@ -718,7 +718,7 @@ uregex_groupUTextDeep(URegularExpression *regexp2,
UText *dest,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, TRUE, status) == FALSE) {
UErrorCode emptyTextStatus = U_ZERO_ERROR;
return (dest ? dest : utext_openUChars(NULL, NULL, 0, &emptyTextStatus));
}
@ -767,7 +767,7 @@ uregex_start64(URegularExpression *regexp2,
int32_t groupNum,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, TRUE, status) == FALSE) {
return 0;
}
int32_t result = regexp->fMatcher->start(groupNum, *status);
@ -791,7 +791,7 @@ uregex_end64(URegularExpression *regexp2,
int32_t groupNum,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, TRUE, status) == FALSE) {
return 0;
}
int32_t result = regexp->fMatcher->end(groupNum, *status);
@ -815,7 +815,7 @@ uregex_reset64(URegularExpression *regexp2,
int64_t index,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, TRUE, status) == FALSE) {
return;
}
regexp->fMatcher->reset(index, *status);
@ -841,7 +841,7 @@ uregex_setRegion64(URegularExpression *regexp2,
int64_t regionLimit,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, TRUE, status) == FALSE) {
return;
}
regexp->fMatcher->region(regionStart, regionLimit, *status);
@ -860,7 +860,7 @@ uregex_setRegionAndStart(URegularExpression *regexp2,
int64_t startIndex,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, TRUE, status) == FALSE) {
return;
}
regexp->fMatcher->region(regionStart, regionLimit, startIndex, *status);
@ -881,7 +881,7 @@ U_CAPI int64_t U_EXPORT2
uregex_regionStart64(const URegularExpression *regexp2,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, TRUE, status) == FALSE) {
return 0;
}
return regexp->fMatcher->regionStart();
@ -903,7 +903,7 @@ U_CAPI int64_t U_EXPORT2
uregex_regionEnd64(const URegularExpression *regexp2,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, TRUE, status) == FALSE) {
return 0;
}
return regexp->fMatcher->regionEnd();
@ -919,7 +919,7 @@ U_CAPI UBool U_EXPORT2
uregex_hasTransparentBounds(const URegularExpression *regexp2,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, FALSE, status) == FALSE) {
return FALSE;
}
return regexp->fMatcher->hasTransparentBounds();
@ -936,7 +936,7 @@ uregex_useTransparentBounds(URegularExpression *regexp2,
UBool b,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, FALSE, status) == FALSE) {
return;
}
regexp->fMatcher->useTransparentBounds(b);
@ -952,7 +952,7 @@ U_CAPI UBool U_EXPORT2
uregex_hasAnchoringBounds(const URegularExpression *regexp2,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, FALSE, status) == FALSE) {
return FALSE;
}
return regexp->fMatcher->hasAnchoringBounds();
@ -969,7 +969,7 @@ uregex_useAnchoringBounds(URegularExpression *regexp2,
UBool b,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, FALSE, status) == FALSE) {
return;
}
regexp->fMatcher->useAnchoringBounds(b);
@ -985,7 +985,7 @@ U_CAPI UBool U_EXPORT2
uregex_hitEnd(const URegularExpression *regexp2,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, TRUE, status) == FALSE) {
return FALSE;
}
return regexp->fMatcher->hitEnd();
@ -1001,7 +1001,7 @@ U_CAPI UBool U_EXPORT2
uregex_requireEnd(const URegularExpression *regexp2,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, TRUE, status) == FALSE) {
return FALSE;
}
return regexp->fMatcher->requireEnd();
@ -1018,7 +1018,7 @@ uregex_setTimeLimit(URegularExpression *regexp2,
int32_t limit,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status)) {
if (validateRE(regexp, FALSE, status)) {
regexp->fMatcher->setTimeLimit(limit, *status);
}
}
@ -1035,7 +1035,7 @@ uregex_getTimeLimit(const URegularExpression *regexp2,
UErrorCode *status) {
int32_t retVal = 0;
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status)) {
if (validateRE(regexp, FALSE, status)) {
retVal = regexp->fMatcher->getTimeLimit();
}
return retVal;
@ -1053,7 +1053,7 @@ uregex_setStackLimit(URegularExpression *regexp2,
int32_t limit,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status)) {
if (validateRE(regexp, FALSE, status)) {
regexp->fMatcher->setStackLimit(limit, *status);
}
}
@ -1070,7 +1070,7 @@ uregex_getStackLimit(const URegularExpression *regexp2,
UErrorCode *status) {
int32_t retVal = 0;
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status)) {
if (validateRE(regexp, FALSE, status)) {
retVal = regexp->fMatcher->getStackLimit();
}
return retVal;
@ -1088,7 +1088,7 @@ uregex_setMatchCallback(URegularExpression *regexp2,
const void *context,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status)) {
if (validateRE(regexp, FALSE, status)) {
regexp->fMatcher->setMatchCallback(callback, context, *status);
}
}
@ -1105,7 +1105,7 @@ uregex_getMatchCallback(const URegularExpression *regexp2,
const void **context,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status)) {
if (validateRE(regexp, FALSE, status)) {
regexp->fMatcher->getMatchCallback(*callback, *context, *status);
}
}
@ -1122,7 +1122,7 @@ uregex_setFindProgressCallback(URegularExpression *regexp2,
const void *context,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status)) {
if (validateRE(regexp, FALSE, status)) {
regexp->fMatcher->setFindProgressCallback(callback, context, *status);
}
}
@ -1139,7 +1139,7 @@ uregex_getFindProgressCallback(const URegularExpression *regexp2,
const void **context,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status)) {
if (validateRE(regexp, FALSE, status)) {
regexp->fMatcher->getFindProgressCallback(*callback, *context, *status);
}
}
@ -1158,7 +1158,7 @@ uregex_replaceAll(URegularExpression *regexp2,
int32_t destCapacity,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, TRUE, status) == FALSE) {
return 0;
}
if (replacementText == NULL || replacementLength < -1 ||
@ -1206,7 +1206,7 @@ uregex_replaceAllUText(URegularExpression *regexp2,
UText *dest,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, TRUE, status) == FALSE) {
return 0;
}
if (replacementText == NULL) {
@ -1232,7 +1232,7 @@ uregex_replaceFirst(URegularExpression *regexp2,
int32_t destCapacity,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, TRUE, status) == FALSE) {
return 0;
}
if (replacementText == NULL || replacementLength < -1 ||
@ -1267,7 +1267,7 @@ uregex_replaceFirstUText(URegularExpression *regexp2,
UText *dest,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, TRUE, status) == FALSE) {
return 0;
}
if (replacementText == NULL) {
@ -1356,7 +1356,7 @@ int32_t RegexCImpl::appendReplacement(RegularExpression *regexp,
//
// Validate all paramters
//
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, TRUE, status) == FALSE) {
return 0;
}
if (replacementText == NULL || replacementLength < -1 ||
@ -1594,7 +1594,7 @@ int32_t RegexCImpl::appendTail(RegularExpression *regexp,
*status = U_ZERO_ERROR;
}
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, TRUE, status) == FALSE) {
return 0;
}
@ -1895,7 +1895,7 @@ uregex_split(URegularExpression *regexp2,
int32_t destFieldsCapacity,
UErrorCode *status) {
RegularExpression *regexp = (RegularExpression*)regexp2;
if (validateRE(regexp, status) == FALSE) {
if (validateRE(regexp, TRUE, status) == FALSE) {
return 0;
}
if ((destBuf == NULL && destCapacity > 0) ||

View file

@ -120,6 +120,7 @@ static void TestRegexCAPI(void);
static void TestBug4315(void);
static void TestUTextAPI(void);
static void TestRefreshInput(void);
static void TestBug8421(void);
void addURegexTest(TestNode** root);
@ -129,6 +130,7 @@ void addURegexTest(TestNode** root)
addTest(root, &TestBug4315, "regex/TestBug4315");
addTest(root, &TestUTextAPI, "regex/TestUTextAPI");
addTest(root, &TestRefreshInput, "regex/TestRefreshInput");
addTest(root, &TestBug8421, "regex/TestBug8421");
}
/*
@ -2199,4 +2201,29 @@ static void TestRefreshInput(void) {
}
static void TestBug8421(void) {
/* Bug 8421: setTimeLimit on a regular expresssion before setting text to be matched
* was failing.
*/
URegularExpression *re;
UErrorCode status = U_ZERO_ERROR;
int32_t limit = -1;
re = uregex_openC("abc", 0, 0, &status);
TEST_ASSERT_SUCCESS(status);
limit = uregex_getTimeLimit(re, &status);
TEST_ASSERT_SUCCESS(status);
TEST_ASSERT(limit == 0);
uregex_setTimeLimit(re, 100, &status);
TEST_ASSERT_SUCCESS(status);
limit = uregex_getTimeLimit(re, &status);
TEST_ASSERT_SUCCESS(status);
TEST_ASSERT(limit == 100);
uregex_close(re);
}
#endif /* !UCONFIG_NO_REGULAR_EXPRESSIONS */