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

This commit is contained in:
James Kitchener 2014-02-03 22:57:56 +10:30
Родитель d4cdf301e3
Коммит ee57f16151
5 изменённых файлов: 19 добавлений и 7 удалений

Просмотреть файл

@ -209,7 +209,10 @@ EncodeInputStream(nsIInputStream *aInputStream,
if (state.charsOnStack) if (state.charsOnStack)
Encode(state.c, state.charsOnStack, state.buffer); 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; return NS_OK;
} }

Просмотреть файл

@ -153,6 +153,13 @@ int32_t nsUnescapeCount(char * str)
char* const pc1 = c1; char* const pc1 = c1;
char* const pc2 = c2; char* const pc2 = c2;
if (!*src) {
// A null string was passed in. Nothing to escape.
// Returns early as the string might not actually be mutable with
// length 0.
return 0;
}
while (*src) while (*src)
{ {
c1[0] = *(src+1); c1[0] = *(src+1);

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

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