Bug 328755 - Assigning a zero-length string should not alloc a buffer. r=bsmedberg

This commit is contained in:
James Kitchener 2013-11-04 14:22:37 -05:00
parent 7a847783e0
commit c75c6853ab
5 changed files with 21 additions and 8 deletions

View File

@ -208,7 +208,10 @@ EncodeInputStream(nsIInputStream *aInputStream,
if (state.charsOnStack)
Encode(state.c, state.charsOnStack, state.buffer);
*aDest.EndWriting() = '\0';
if (aDest.Length())
// May belong to an nsCString with an unallocated buffer, so only null
// terminate if there is a need to.
*aDest.EndWriting() = '\0';
return NS_OK;
}

View File

@ -181,7 +181,15 @@ int32_t nsUnescapeCount(char * str)
}
}
*dst = 0;
/* The string may belong to a nsCString with an unallocated buffer.
In such a situation the buffer points to a const char, so attempting
to write to it will crash. This can be avoided by only null-terminating
when needed. The above while loop is safe as it won't iterate when this
occurs.
*/
if (*dst) {
*dst = 0;
}
return (int)(dst - str);
} /* NET_UnEscapeCnt */

View File

@ -100,7 +100,7 @@ struct nsCharTraits<PRUnichar>
typedef uint16_t unsigned_char_type;
typedef char incompatible_char_type;
static char_type *sEmptyBuffer;
static char_type * const sEmptyBuffer;
static
void
@ -326,7 +326,7 @@ struct nsCharTraits<char>
typedef unsigned char unsigned_char_type;
typedef PRUnichar incompatible_char_type;
static char_type *sEmptyBuffer;
static char_type *const sEmptyBuffer;
static
void

View File

@ -29,10 +29,12 @@ using mozilla::Atomic;
// ---------------------------------------------------------------------------
static PRUnichar gNullChar = 0;
static const PRUnichar gNullChar = 0;
char* nsCharTraits<char> ::sEmptyBuffer = (char*) &gNullChar;
PRUnichar* nsCharTraits<PRUnichar>::sEmptyBuffer = &gNullChar;
char* const nsCharTraits<char> ::sEmptyBuffer =
(char*) const_cast<PRUnichar*>(&gNullChar);
PRUnichar* const nsCharTraits<PRUnichar>::sEmptyBuffer =
const_cast<PRUnichar*>(&gNullChar);
// ---------------------------------------------------------------------------

View File

@ -302,7 +302,7 @@ nsTSubstring_CharT::Assign( const char_type* data, size_type length )
bool
nsTSubstring_CharT::Assign( const char_type* data, size_type length, const fallible_t& )
{
if (!data)
if (!data || length == 0)
{
Truncate();
return true;