Set correct output params. Bug 634257, r=emk, a=bsmedberg
authorSimon Montagu <smontagu@smontagu.org>
Sun, 20 Feb 2011 11:04:28 +0200
changeset 62880 aa28638dc457230f400f90a452eba32977a815e4
parent 62879 e77f4eda0bad4081f5387acc1bc321b81ca2f4f4
child 62881 28bf1def92065e9a6412a14b02eb08cfa9859656
push idunknown
push userunknown
push dateunknown
reviewersemk, bsmedberg
bugs634257
milestone2.0b12pre
Set correct output params. Bug 634257, r=emk, a=bsmedberg
intl/uconv/ucvlatin/nsUCS2BEToUnicode.cpp
--- a/intl/uconv/ucvlatin/nsUCS2BEToUnicode.cpp
+++ b/intl/uconv/ucvlatin/nsUCS2BEToUnicode.cpp
@@ -58,26 +58,23 @@ UTF16ConvertToUnicode(PRUint8& aState, P
 {
   const char* src = aSrc;
   const char* srcEnd = aSrc + *aSrcLength;
   PRUnichar* dest = aDest;
   PRUnichar* destEnd = aDest + *aDestLength;
 
   switch(aState) {
     case STATE_FOUND_BOM:
-      if (*aSrcLength < 2)
-        return NS_ERROR_ILLEGAL_INPUT;
+      NS_ASSERTION(*aSrcLength > 1, "buffer too short");
       src+=2;
       aState = STATE_NORMAL;
       break;
 
     case STATE_FIRST_CALL: // first time called
-      if (*aSrcLength < 2)
-        return NS_ERROR_ILLEGAL_INPUT;
-
+      NS_ASSERTION(*aSrcLength > 1, "buffer too short");
       // Eliminate BOM (0xFEFF). Note that different endian case is taken care
       // of in |Convert| of LE and BE converters. Here, we only have to
       // deal with the same endian case. That is, 0xFFFE (byte-swapped BOM) is
       // illegal.
       if(0xFEFF == *((PRUnichar*)src)) {
         src+=2;
       } else if(0xFFFE == *((PRUnichar*)src)) {
         *aSrcLength=0;
@@ -217,24 +214,28 @@ nsUTF16ToUnicodeBase::GetMaxLength(const
   return NS_OK;
 }
 
 
 NS_IMETHODIMP
 nsUTF16BEToUnicode::Convert(const char * aSrc, PRInt32 * aSrcLength,
                             PRUnichar * aDest, PRInt32 * aDestLength)
 {
+    if(STATE_FIRST_CALL == mState && *aSrcLength < 2)
+    {
+      *aSrcLength=0;
+      *aDestLength=0;
+      return NS_ERROR_ILLEGAL_INPUT;
+    }
 #ifdef IS_LITTLE_ENDIAN
     // Remove the BOM if we're little-endian. The 'same endian' case with the
     // leading BOM will be taken care of by |UTF16ConvertToUnicode|.
     if(STATE_FIRST_CALL == mState) // Called for the first time.
     {
       mState = STATE_NORMAL;
-      if (*aSrcLength < 2)
-        return NS_ERROR_ILLEGAL_INPUT;
       if(0xFFFE == *((PRUnichar*)aSrc)) {
         // eliminate BOM (on LE machines, BE BOM is 0xFFFE)
         mState = STATE_FOUND_BOM;
       } else if(0xFEFF == *((PRUnichar*)aSrc)) {
         *aSrcLength=0;
         *aDestLength=0;
         return NS_ERROR_ILLEGAL_INPUT;
       }
@@ -252,24 +253,28 @@ nsUTF16BEToUnicode::Convert(const char *
                                       );
   return rv;
 }
 
 NS_IMETHODIMP
 nsUTF16LEToUnicode::Convert(const char * aSrc, PRInt32 * aSrcLength,
                             PRUnichar * aDest, PRInt32 * aDestLength)
 {
+    if(STATE_FIRST_CALL == mState && *aSrcLength < 2)
+    {
+      *aSrcLength=0;
+      *aDestLength=0;
+      return NS_ERROR_ILLEGAL_INPUT;
+    }
 #ifdef IS_BIG_ENDIAN
     // Remove the BOM if we're big-endian. The 'same endian' case with the
     // leading BOM will be taken care of by |UTF16ConvertToUnicode|.
     if(STATE_FIRST_CALL == mState) // first time called
     {
       mState = STATE_NORMAL;
-      if (*aSrcLength < 2)
-        return NS_ERROR_ILLEGAL_INPUT;
       if(0xFFFE == *((PRUnichar*)aSrc)) {
         // eliminate BOM (on BE machines, LE BOM is 0xFFFE)
         mState = STATE_FOUND_BOM;
       } else if(0xFEFF == *((PRUnichar*)aSrc)) {
         *aSrcLength=0;
         *aDestLength=0;
         return NS_ERROR_ILLEGAL_INPUT;
       }
@@ -295,22 +300,25 @@ nsUTF16ToUnicode::Reset()
   mFoundBOM = PR_FALSE;
   return nsUTF16ToUnicodeBase::Reset();
 }
 
 NS_IMETHODIMP
 nsUTF16ToUnicode::Convert(const char * aSrc, PRInt32 * aSrcLength,
                           PRUnichar * aDest, PRInt32 * aDestLength)
 {
+    if(STATE_FIRST_CALL == mState && *aSrcLength < 2)
+    {
+      *aSrcLength=0;
+      *aDestLength=0;
+      return NS_ERROR_ILLEGAL_INPUT;
+    }
     if(STATE_FIRST_CALL == mState) // first time called
     {
       mState = STATE_NORMAL;
-      if (*aSrcLength < 2)
-        return NS_ERROR_ILLEGAL_INPUT;
-
       // check if BOM (0xFEFF) is at the beginning, remove it if found, and
       // set mEndian accordingly.
       if(0xFF == PRUint8(aSrc[0]) && 0xFE == PRUint8(aSrc[1])) {
         mState = STATE_FOUND_BOM;
         mEndian = kLittleEndian;
         mFoundBOM = PR_TRUE;
       }
       else if(0xFE == PRUint8(aSrc[0]) && 0xFF == PRUint8(aSrc[1])) {