diff --git a/icu4c/source/common/utext.cpp b/icu4c/source/common/utext.cpp index fede868027..719c50ef91 100644 --- a/icu4c/source/common/utext.cpp +++ b/icu4c/source/common/utext.cpp @@ -1,7 +1,7 @@ /* ******************************************************************************* * -* Copyright (C) 2005-2006, International Business Machines +* Copyright (C) 2005-2007, International Business Machines * Corporation and others. All Rights Reserved. * ******************************************************************************* @@ -578,8 +578,6 @@ utext_setup(UText *ut, int32_t extraSpace, UErrorCode *status) { if (spaceRequired>0) { ut->extraSize = extraSpace; ut->pExtra = &((ExtendedUText *)ut)->extension; - uprv_memset(ut->pExtra, 0, extraSpace); // Purify whines about copying untouched extra [buffer] - // space when cloning, so init it now. } } } else { @@ -611,7 +609,6 @@ utext_setup(UText *ut, int32_t extraSpace, UErrorCode *status) { } else { ut->extraSize = extraSpace; ut->flags |= UTEXT_EXTRA_HEAP_ALLOCATED; - uprv_memset(ut->pExtra, 0, extraSpace); } } } @@ -638,6 +635,9 @@ utext_setup(UText *ut, int32_t extraSpace, UErrorCode *status) { ut->privB = 0; ut->privC = 0; ut->privP = NULL; + if (ut->pExtra!=NULL && ut->extraSize>0) + uprv_memset(ut->pExtra, 0, ut->extraSize); + } return ut; } @@ -799,6 +799,7 @@ shallowTextClone(UText * dest, const UText * src, UErrorCode * status) { adjustPointer(dest, &dest->p, src); adjustPointer(dest, &dest->q, src); adjustPointer(dest, &dest->r, src); + adjustPointer(dest, (const void **)&dest->chunkContents, src); return dest; } diff --git a/icu4c/source/test/intltest/utxttest.cpp b/icu4c/source/test/intltest/utxttest.cpp index 7d0c58f642..fdcf2177dc 100644 --- a/icu4c/source/test/intltest/utxttest.cpp +++ b/icu4c/source/test/intltest/utxttest.cpp @@ -1,6 +1,6 @@ /******************************************************************** * COPYRIGHT: - * Copyright (c) 2005-2006, International Business Machines Corporation and + * Copyright (c) 2005-2007, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ /************************************************************************ @@ -54,6 +54,8 @@ UTextTest::runIndexedTest(int32_t index, UBool exec, if (exec) ErrorTest(); break; case 2: name = "FreezeTest"; if (exec) FreezeTest(); break; + case 3: name = "Ticket5560"; + if (exec) Ticket5560(); break; default: name = ""; break; } } @@ -1350,3 +1352,40 @@ openFragmentedUnicodeString(UText *ut, UnicodeString *s, UErrorCode *status) { return ut; } +// Regression test for Ticket 5560 +// Clone fails to update chunkContentPointer in the cloned copy. +// This is only an issue for UText types that work in a local buffer, +// (UTF-8 wrapper, for example) +// +// The test: +// 1. Create an inital UText +// 2. Deep clone it. Contents should match original. +// 3. Reset original to something different. +// 4. Check that clone contents did not change. +// +void UTextTest::Ticket5560() { + char *s1 = "ABCDEF"; + char *s2 = "123456"; + UErrorCode status = U_ZERO_ERROR; + + UText ut1 = UTEXT_INITIALIZER; + UText ut2 = UTEXT_INITIALIZER; + + utext_openUTF8(&ut1, s1, -1, &status); + UChar c = utext_next32(&ut1); + TEST_ASSERT(c == 0x41); // c == 'A' + + utext_clone(&ut2, &ut1, TRUE, FALSE, &status); + TEST_SUCCESS(status); + c = utext_next32(&ut2); + TEST_ASSERT(c == 0x42); // c == 'B' + c = utext_next32(&ut1); + TEST_ASSERT(c == 0x42); // c == 'B' + + utext_openUTF8(&ut1, s2, -1, &status); + c = utext_next32(&ut1); + TEST_ASSERT(c == 0x31); // c == '1' + c = utext_next32(&ut2); + TEST_ASSERT(c == 0x43); // c == 'C' + +} diff --git a/icu4c/source/test/intltest/utxttest.h b/icu4c/source/test/intltest/utxttest.h index bfa3aa9325..ca1e2f605e 100644 --- a/icu4c/source/test/intltest/utxttest.h +++ b/icu4c/source/test/intltest/utxttest.h @@ -1,6 +1,6 @@ /******************************************************************** * COPYRIGHT: - * Copyright (c) 2005-2006, International Business Machines Corporation and + * Copyright (c) 2005-2007, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ /************************************************************************ @@ -31,6 +31,7 @@ public: void TextTest(); void ErrorTest(); void FreezeTest(); + void Ticket5560(); private: struct m { // Map between native indices & code points.