From 71d2d8d00499c9b614e1ef9601bef0dd1497fffc Mon Sep 17 00:00:00 2001 From: Alessio Placitelli Date: Tue, 7 Jan 2014 10:18:30 -0500 Subject: [PATCH] Bug 950762 - Add fallible AppendUTF16toUTF8 and make DOMParser::ParseFromString fallible. r=jst, r=bsmedberg --- content/base/src/DOMParser.cpp | 10 +++++++--- xpcom/string/public/nsReadableUtils.h | 2 ++ xpcom/string/src/nsReadableUtils.cpp | 15 ++++++++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/content/base/src/DOMParser.cpp b/content/base/src/DOMParser.cpp index 0dfb96f0e75b..65762036c45d 100644 --- a/content/base/src/DOMParser.cpp +++ b/content/base/src/DOMParser.cpp @@ -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 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 diff --git a/xpcom/string/public/nsReadableUtils.h b/xpcom/string/public/nsReadableUtils.h index a44514524ff4..fd9b02895f37 100644 --- a/xpcom/string/public/nsReadableUtils.h +++ b/xpcom/string/public/nsReadableUtils.h @@ -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; diff --git a/xpcom/string/src/nsReadableUtils.cpp b/xpcom/string/src/nsReadableUtils.cpp index afceee48a3f1..35cf83ab9b24 100644 --- a/xpcom/string/src/nsReadableUtils.cpp +++ b/xpcom/string/src/nsReadableUtils.cpp @@ -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