Bug 1097452 - Use fallible allocation when setting the value of an <input> or <textarea> element; r=jst,froydnj

This patch handles most of the call sites for these allocations except
for a few where I added TODO comments with some information.  Handling
those places may require reworking lots of code, so I prefer to not do
that here.
This commit is contained in:
Ehsan Akhgari
2015-01-07 20:14:01 -05:00
parent 9d1d0dafe9
commit 0f02dbc8c0
10 changed files with 179 additions and 45 deletions

View File

@@ -454,17 +454,41 @@ nsTString_CharT::ReplaceChar( const char* aSet, char_type aNewChar )
void ReleaseData(void* aData, uint32_t aFlags);
void
nsTString_CharT::ReplaceSubstring( const char_type* aTarget, const char_type* aNewValue )
nsTString_CharT::ReplaceSubstring(const char_type* aTarget,
const char_type* aNewValue)
{
ReplaceSubstring(nsTDependentString_CharT(aTarget),
nsTDependentString_CharT(aNewValue));
}
bool
nsTString_CharT::ReplaceSubstring(const char_type* aTarget,
const char_type* aNewValue,
const fallible_t& fallible)
{
return ReplaceSubstring(nsTDependentString_CharT(aTarget),
nsTDependentString_CharT(aNewValue),
fallible);
}
void
nsTString_CharT::ReplaceSubstring( const self_type& aTarget, const self_type& aNewValue )
nsTString_CharT::ReplaceSubstring(const self_type& aTarget,
const self_type& aNewValue)
{
if (!ReplaceSubstring(aTarget, aNewValue, fallible_t())) {
// Note that this may wildly underestimate the allocation that failed, as
// we could have been replacing multiple copies of aTarget.
AllocFailed(mLength + (aNewValue.Length() - aTarget.Length()));
}
}
bool
nsTString_CharT::ReplaceSubstring(const self_type& aTarget,
const self_type& aNewValue,
const fallible_t&)
{
if (aTarget.Length() == 0)
return;
return true;
// Remember all of the non-matching parts.
nsAutoTArray<Segment, 16> nonMatching;
@@ -495,7 +519,7 @@ nsTString_CharT::ReplaceSubstring( const self_type& aTarget, const self_type& aN
if (nonMatching.Length() == 1) {
MOZ_ASSERT(nonMatching[0].mBegin == 0 && nonMatching[0].mLength == mLength,
"We should have the correct non-matching segment.");
return;
return true;
}
// Make sure that we can mutate our buffer.
@@ -505,7 +529,7 @@ nsTString_CharT::ReplaceSubstring( const self_type& aTarget, const self_type& aN
char_type* oldData;
uint32_t oldFlags;
if (!MutatePrep(XPCOM_MAX(mLength, newLength), &oldData, &oldFlags))
return;
return false;
if (oldData) {
// Copy all of the old data to the new buffer.
char_traits::copy(mData, oldData, mLength);
@@ -549,6 +573,8 @@ nsTString_CharT::ReplaceSubstring( const self_type& aTarget, const self_type& aN
// Adjust the length and make sure the string is null terminated.
mLength = newLength;
mData[mLength] = char_type(0);
return true;
}
/**