Bug 950762 - Add fallible AppendUTF16toUTF8 and make DOMParser::ParseFromString fallible. r=jst, r=bsmedberg

This commit is contained in:
Alessio Placitelli 2014-01-07 10:18:30 -05:00
parent 6c81e90760
commit 9da9342c58
3 changed files with 23 additions and 4 deletions

View File

@ -106,17 +106,21 @@ DOMParser::ParseFromString(const nsAString& str,
return rv;
}
NS_ConvertUTF16toUTF8 data(str);
nsAutoCString utf8str;
// Convert from UTF16 to UTF8 using fallible allocations
if (!AppendUTF16toUTF8(str, utf8str, mozilla::fallible_t())) {
return NS_ERROR_OUT_OF_MEMORY;
}
// The new stream holds a reference to the buffer
nsCOMPtr<nsIInputStream> stream;
rv = NS_NewByteInputStream(getter_AddRefs(stream),
data.get(), data.Length(),
utf8str.get(), utf8str.Length(),
NS_ASSIGNMENT_DEPEND);
if (NS_FAILED(rv))
return rv;
return ParseFromStream(stream, "UTF-8", data.Length(), contentType, aResult);
return ParseFromStream(stream, "UTF-8", utf8str.Length(), contentType, aResult);
}
already_AddRefed<nsIDocument>

View File

@ -46,6 +46,8 @@ void LossyAppendUTF16toASCII( const char16_t* aSource, nsACString& aDest );
void AppendASCIItoUTF16( const char* aSource, nsAString& aDest );
void AppendUTF16toUTF8( const nsAString& aSource, nsACString& aDest );
bool AppendUTF16toUTF8( const nsAString& aSource, nsACString& aDest,
const mozilla::fallible_t& ) NS_WARN_UNUSED_RESULT;
void AppendUTF8toUTF16( const nsACString& aSource, nsAString& aDest );
bool AppendUTF8toUTF16( const nsACString& aSource, nsAString& aDest,
const mozilla::fallible_t& ) NS_WARN_UNUSED_RESULT;

View File

@ -126,6 +126,15 @@ AppendASCIItoUTF16( const char* aSource, nsAString& aDest )
void
AppendUTF16toUTF8( const nsAString& aSource, nsACString& aDest )
{
if (!AppendUTF16toUTF8(aSource, aDest, mozilla::fallible_t())) {
NS_ABORT_OOM(aDest.Length() + aSource.Length());
}
}
bool
AppendUTF16toUTF8( const nsAString& aSource, nsACString& aDest,
const mozilla::fallible_t& )
{
nsAString::const_iterator source_start, source_end;
CalculateUTF8Size calculator;
@ -139,7 +148,9 @@ AppendUTF16toUTF8( const nsAString& aSource, nsACString& aDest )
uint32_t old_dest_length = aDest.Length();
// Grow the buffer if we need to.
aDest.SetLength(old_dest_length + count);
if (!aDest.SetLength(old_dest_length + count, mozilla::fallible_t())) {
return false;
}
// All ready? Time to convert
@ -151,6 +162,8 @@ AppendUTF16toUTF8( const nsAString& aSource, nsACString& aDest )
"Unexpected disparity between CalculateUTF8Size and "
"ConvertUTF16toUTF8");
}
return true;
}
void