From 03072ae01d2e2c44b8668623e1511fb7eae53390 Mon Sep 17 00:00:00 2001 From: "jaggernaut%netscape.com" Date: Tue, 14 May 2002 08:27:57 +0000 Subject: [PATCH] Bug 132583: deal more cleanly with passing non-UTF8 strings to NS_ConvertUTF8toUCS2. r=scc, sr=darin git-svn-id: svn://10.0.0.236/trunk@121461 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/string/obsolete/nsString2.cpp | 12 +++++------- mozilla/string/obsolete/nsString2.h | 13 ++++++++++--- mozilla/xpcom/string/obsolete/nsString2.cpp | 12 +++++------- mozilla/xpcom/string/obsolete/nsString2.h | 13 ++++++++++--- 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/mozilla/string/obsolete/nsString2.cpp b/mozilla/string/obsolete/nsString2.cpp index dc0ebc3b747..7570173d081 100644 --- a/mozilla/string/obsolete/nsString2.cpp +++ b/mozilla/string/obsolete/nsString2.cpp @@ -1330,22 +1330,20 @@ NS_ConvertUTF8toUCS2::Init( const nsACString& aCString ) PRUint32 count = calculator.Length(); if (count) { - // Grow the buffer if we need to. - SetCapacity(count); - // |SetCapacity| normally doesn't guarantee the use we are putting it to here (see its interface comment in nsAString.h), - // we can only use it since our local implementation, |nsString::SetCapacity|, is known to do what we want + SetLength(count); // All ready? Time to convert ConvertUTF8toUCS2 converter(mUStr); copy_string(aCString.BeginReading(start), aCString.EndReading(end), converter); mLength = converter.Length(); - if (GetCapacity()) - mUStr[mLength] = '\0'; // null terminate + if (mLength != count) { + NS_ERROR("Input wasn't UTF8 or incorrect length was calculated"); + Truncate(); + } } - NS_ASSERTION(count == mLength, "calculator calculated incorrect length"); } /** diff --git a/mozilla/string/obsolete/nsString2.h b/mozilla/string/obsolete/nsString2.h index ea6e0691862..b171645e56a 100644 --- a/mozilla/string/obsolete/nsString2.h +++ b/mozilla/string/obsolete/nsString2.h @@ -583,12 +583,16 @@ class ConvertUTF8toUCS2 typedef nsACString::char_type value_type; typedef nsAString::char_type buffer_type; - ConvertUTF8toUCS2( buffer_type* aBuffer ) : mStart(aBuffer), mBuffer(aBuffer) {} + ConvertUTF8toUCS2( buffer_type* aBuffer ) + : mStart(aBuffer), mBuffer(aBuffer), mErrorEncountered(PR_FALSE) {} size_t Length() const { return mBuffer - mStart; } PRUint32 write( const value_type* start, PRUint32 N ) { + if ( mErrorEncountered ) + return N; + // algorithm assumes utf8 units won't // be spread across fragments const value_type* p = start; @@ -640,7 +644,8 @@ class ConvertUTF8toUCS2 else { NS_ERROR("Not a UTF-8 string. This code should only be used for converting from known UTF-8 strings."); - break; + mErrorEncountered = PR_TRUE; + return N; } while ( state-- ) @@ -655,7 +660,8 @@ class ConvertUTF8toUCS2 else { NS_ERROR("not a UTF8 string"); - return p - start; + mErrorEncountered = PR_TRUE; + return N; } } @@ -694,6 +700,7 @@ class ConvertUTF8toUCS2 private: buffer_type* mStart; buffer_type* mBuffer; + PRBool mErrorEncountered; }; #endif /* !defined(nsString2_h__) */ diff --git a/mozilla/xpcom/string/obsolete/nsString2.cpp b/mozilla/xpcom/string/obsolete/nsString2.cpp index dc0ebc3b747..7570173d081 100644 --- a/mozilla/xpcom/string/obsolete/nsString2.cpp +++ b/mozilla/xpcom/string/obsolete/nsString2.cpp @@ -1330,22 +1330,20 @@ NS_ConvertUTF8toUCS2::Init( const nsACString& aCString ) PRUint32 count = calculator.Length(); if (count) { - // Grow the buffer if we need to. - SetCapacity(count); - // |SetCapacity| normally doesn't guarantee the use we are putting it to here (see its interface comment in nsAString.h), - // we can only use it since our local implementation, |nsString::SetCapacity|, is known to do what we want + SetLength(count); // All ready? Time to convert ConvertUTF8toUCS2 converter(mUStr); copy_string(aCString.BeginReading(start), aCString.EndReading(end), converter); mLength = converter.Length(); - if (GetCapacity()) - mUStr[mLength] = '\0'; // null terminate + if (mLength != count) { + NS_ERROR("Input wasn't UTF8 or incorrect length was calculated"); + Truncate(); + } } - NS_ASSERTION(count == mLength, "calculator calculated incorrect length"); } /** diff --git a/mozilla/xpcom/string/obsolete/nsString2.h b/mozilla/xpcom/string/obsolete/nsString2.h index ea6e0691862..b171645e56a 100644 --- a/mozilla/xpcom/string/obsolete/nsString2.h +++ b/mozilla/xpcom/string/obsolete/nsString2.h @@ -583,12 +583,16 @@ class ConvertUTF8toUCS2 typedef nsACString::char_type value_type; typedef nsAString::char_type buffer_type; - ConvertUTF8toUCS2( buffer_type* aBuffer ) : mStart(aBuffer), mBuffer(aBuffer) {} + ConvertUTF8toUCS2( buffer_type* aBuffer ) + : mStart(aBuffer), mBuffer(aBuffer), mErrorEncountered(PR_FALSE) {} size_t Length() const { return mBuffer - mStart; } PRUint32 write( const value_type* start, PRUint32 N ) { + if ( mErrorEncountered ) + return N; + // algorithm assumes utf8 units won't // be spread across fragments const value_type* p = start; @@ -640,7 +644,8 @@ class ConvertUTF8toUCS2 else { NS_ERROR("Not a UTF-8 string. This code should only be used for converting from known UTF-8 strings."); - break; + mErrorEncountered = PR_TRUE; + return N; } while ( state-- ) @@ -655,7 +660,8 @@ class ConvertUTF8toUCS2 else { NS_ERROR("not a UTF8 string"); - return p - start; + mErrorEncountered = PR_TRUE; + return N; } } @@ -694,6 +700,7 @@ class ConvertUTF8toUCS2 private: buffer_type* mStart; buffer_type* mBuffer; + PRBool mErrorEncountered; }; #endif /* !defined(nsString2_h__) */