From 118efa19c68be597b01c42aaa5ba921e2c77dde2 Mon Sep 17 00:00:00 2001 From: Andy Heninger Date: Thu, 21 Apr 2011 00:07:50 +0000 Subject: [PATCH] ICU-8486 UText not checking for Bogus UnicodeStrings, causing crash in regular expressions. X-SVN-Rev: 29840 --- icu4c/source/common/utext.cpp | 22 +++++++++------------- icu4c/source/test/intltest/regextst.cpp | 20 ++++++++++++++++++++ icu4c/source/test/intltest/regextst.h | 3 ++- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/icu4c/source/common/utext.cpp b/icu4c/source/common/utext.cpp index e0cc5b76c69..10ebc3296b3 100644 --- a/icu4c/source/common/utext.cpp +++ b/icu4c/source/common/utext.cpp @@ -2620,20 +2620,9 @@ U_CDECL_END U_CAPI UText * U_EXPORT2 utext_openUnicodeString(UText *ut, UnicodeString *s, UErrorCode *status) { - // TODO: use openConstUnicodeString, then add in the differences. - // - ut = utext_setup(ut, 0, status); + ut = utext_openConstUnicodeString(ut, s, status); if (U_SUCCESS(*status)) { - ut->pFuncs = &unistrFuncs; - ut->context = s; - ut->providerProperties = I32_FLAG(UTEXT_PROVIDER_STABLE_CHUNKS)| - I32_FLAG(UTEXT_PROVIDER_WRITABLE); - - ut->chunkContents = s->getBuffer(); - ut->chunkLength = s->length(); - ut->chunkNativeStart = 0; - ut->chunkNativeLimit = ut->chunkLength; - ut->nativeIndexingLimit = ut->chunkLength; + ut->providerProperties |= I32_FLAG(UTEXT_PROVIDER_WRITABLE); } return ut; } @@ -2642,6 +2631,13 @@ utext_openUnicodeString(UText *ut, UnicodeString *s, UErrorCode *status) { U_CAPI UText * U_EXPORT2 utext_openConstUnicodeString(UText *ut, const UnicodeString *s, UErrorCode *status) { + if (U_SUCCESS(*status) && s->isBogus()) { + // The UnicodeString is bogus, but we still need to detach the UText + // from whatever it was hooked to before, if anything. + utext_openUChars(ut, NULL, 0, status); + *status = U_ILLEGAL_ARGUMENT_ERROR; + return ut; + } ut = utext_setup(ut, 0, status); // note: use the standard (writable) function table for UnicodeString. // The flag settings disable writing, so having the functions in diff --git a/icu4c/source/test/intltest/regextst.cpp b/icu4c/source/test/intltest/regextst.cpp index 34fda5c93aa..20c8e235888 100644 --- a/icu4c/source/test/intltest/regextst.cpp +++ b/icu4c/source/test/intltest/regextst.cpp @@ -108,6 +108,9 @@ void RegexTest::runIndexedTest( int32_t index, UBool exec, const char* &name, ch case 17: name = "Bug 7740"; if (exec) Bug7740(); break; + case 18: name = "Bug 8479"; + if (exec) Bug8479(); + break; default: name = ""; break; //needed to end loop @@ -5046,7 +5049,24 @@ void RegexTest::Bug7740() { delete m; } +// Bug 8479: was crashing whith a Bogus UnicodeString as input. +void RegexTest::Bug8479() { + UErrorCode status = U_ZERO_ERROR; + + RegexMatcher* const pMatcher = new RegexMatcher("\\Aboo\\z", UREGEX_DOTALL|UREGEX_CASE_INSENSITIVE, status); + REGEX_CHECK_STATUS; + if (U_SUCCESS(status)) + { + UnicodeString str; + str.setToBogus(); + pMatcher->reset(str); + status = U_ZERO_ERROR; + pMatcher->matches(status); + REGEX_ASSERT(status == U_ILLEGAL_ARGUMENT_ERROR); + delete pMatcher; + } +} #endif /* !UCONFIG_NO_REGULAR_EXPRESSIONS */ diff --git a/icu4c/source/test/intltest/regextst.h b/icu4c/source/test/intltest/regextst.h index cbae0e4a152..10ebb269e6c 100644 --- a/icu4c/source/test/intltest/regextst.h +++ b/icu4c/source/test/intltest/regextst.h @@ -1,6 +1,6 @@ /******************************************************************** * COPYRIGHT: - * Copyright (c) 2002-2010, International Business Machines Corporation and + * Copyright (c) 2002-2011, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ @@ -43,6 +43,7 @@ public: virtual void PreAllocatedUTextCAPI(); virtual void Bug7651(); virtual void Bug7740(); + virtual void Bug8479(); // The following functions are internal to the regexp tests. virtual void assertUText(const char *expected, UText *actual, const char *file, int line);