Fix bug 383426 (OOM crash). r=peterv, sr=roc.

This commit is contained in:
jruderman@hmc.edu 2007-07-02 23:03:11 -07:00
parent 0dbeb4eb58
commit cdf7d8d9e6

View File

@ -109,12 +109,34 @@ CopyUTF8toUTF16( const char* aSource, nsAString& aDest )
AppendUTF8toUTF16(aSource, aDest);
}
// Like GetMutableData, but returns false if it can't
// allocate enough memory (e.g. due to OOM) rather than
// returning zero (which could have other meanings) and
// throws away the out-param pointer.
PRBool
SetLengthForWriting(nsAString& aDest, PRUint32 aDesiredLength)
{
PRUnichar* dummy;
PRUint32 len = aDest.GetMutableData(&dummy, aDesiredLength);
return (len >= aDesiredLength);
}
PRBool
SetLengthForWritingC(nsACString& aDest, PRUint32 aDesiredLength)
{
char* dummy;
PRUint32 len = aDest.GetMutableData(&dummy, aDesiredLength);
return (len >= aDesiredLength);
}
NS_COM
void
LossyAppendUTF16toASCII( const nsAString& aSource, nsACString& aDest )
{
PRUint32 old_dest_length = aDest.Length();
aDest.SetLength(old_dest_length + aSource.Length());
if (!SetLengthForWritingC(aDest, old_dest_length + aSource.Length()))
return;
nsAString::const_iterator fromBegin, fromEnd;
@ -134,7 +156,8 @@ void
AppendASCIItoUTF16( const nsACString& aSource, nsAString& aDest )
{
PRUint32 old_dest_length = aDest.Length();
aDest.SetLength(old_dest_length + aSource.Length());
if (!SetLengthForWriting(aDest, old_dest_length + aSource.Length()))
return;
nsACString::const_iterator fromBegin, fromEnd;
@ -183,7 +206,8 @@ AppendUTF16toUTF8( const nsAString& aSource, nsACString& aDest )
PRUint32 old_dest_length = aDest.Length();
// Grow the buffer if we need to.
aDest.SetLength(old_dest_length + count);
if(!SetLengthForWritingC(aDest, old_dest_length + count))
return;
nsACString::iterator dest;
aDest.BeginWriting(dest);
@ -238,7 +262,8 @@ AppendUTF8toUTF16( const nsACString& aSource, nsAString& aDest )
PRUint32 old_dest_length = aDest.Length();
// Grow the buffer if we need to.
aDest.SetLength(old_dest_length + count);
if(!SetLengthForWriting(aDest, old_dest_length + count))
return;
nsAString::iterator dest;
aDest.BeginWriting(dest);
@ -438,7 +463,9 @@ CopyUnicodeTo( const nsAString::const_iterator& aSrcStart,
nsAString& aDest )
{
nsAString::iterator writer;
aDest.SetLength(Distance(aSrcStart, aSrcEnd));
if (!SetLengthForWriting(aDest, Distance(aSrcStart, aSrcEnd)))
return;
aDest.BeginWriting(writer);
nsAString::const_iterator fromBegin(aSrcStart);
@ -453,7 +480,9 @@ AppendUnicodeTo( const nsAString::const_iterator& aSrcStart,
{
nsAString::iterator writer;
PRUint32 oldLength = aDest.Length();
aDest.SetLength(oldLength + Distance(aSrcStart, aSrcEnd));
if(!SetLengthForWriting(aDest, oldLength + Distance(aSrcStart, aSrcEnd)))
return;
aDest.BeginWriting(writer).advance(oldLength);
nsAString::const_iterator fromBegin(aSrcStart);
@ -702,7 +731,9 @@ ToUpperCase( const nsACString& aSource, nsACString& aDest )
{
nsACString::const_iterator fromBegin, fromEnd;
nsACString::iterator toBegin;
aDest.SetLength(aSource.Length());
if (!SetLengthForWritingC(aDest, aSource.Length()))
return;
CopyToUpperCase converter(aDest.BeginWriting(toBegin));
copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd), converter);
}
@ -792,7 +823,9 @@ ToLowerCase( const nsACString& aSource, nsACString& aDest )
{
nsACString::const_iterator fromBegin, fromEnd;
nsACString::iterator toBegin;
aDest.SetLength(aSource.Length());
if (!SetLengthForWritingC(aDest, aSource.Length()))
return;
CopyToLowerCase converter(aDest.BeginWriting(toBegin));
copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd), converter);
}