From f6465f98b9dd6a9b762b0d0ad007908aca0cfb60 Mon Sep 17 00:00:00 2001 From: Nathan Froyd Date: Fri, 5 May 2017 11:33:36 -0400 Subject: [PATCH] Bug 1362194 - part 1 - add a fallible CopyASCIItoUTF16 function; r=mccr8 We already have all the machinery to expose a function like this, we just need to write it. --- xpcom/string/nsReadableUtils.cpp | 13 ++++++++++++- xpcom/string/nsReadableUtils.h | 2 ++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/xpcom/string/nsReadableUtils.cpp b/xpcom/string/nsReadableUtils.cpp index 3c6af1cd8f31..13c04fb924de 100644 --- a/xpcom/string/nsReadableUtils.cpp +++ b/xpcom/string/nsReadableUtils.cpp @@ -87,9 +87,20 @@ LossyCopyUTF16toASCII(const nsAString& aSource, nsACString& aDest) void CopyASCIItoUTF16(const nsACString& aSource, nsAString& aDest) +{ + if (!CopyASCIItoUTF16(aSource, aDest, mozilla::fallible)) { + // Note that this may wildly underestimate the allocation that failed, as + // we report the length of aSource as UTF-16 instead of UTF-8. + aDest.AllocFailed(aDest.Length() + aSource.Length()); + } +} + +bool +CopyASCIItoUTF16(const nsACString& aSource, nsAString& aDest, + const mozilla::fallible_t& aFallible) { aDest.Truncate(); - AppendASCIItoUTF16(aSource, aDest); + return AppendASCIItoUTF16(aSource, aDest, aFallible); } void diff --git a/xpcom/string/nsReadableUtils.h b/xpcom/string/nsReadableUtils.h index 24824d927898..fab5efb04126 100644 --- a/xpcom/string/nsReadableUtils.h +++ b/xpcom/string/nsReadableUtils.h @@ -35,6 +35,8 @@ Distance(const nsReadingIterator& aStart, void LossyCopyUTF16toASCII(const nsAString& aSource, nsACString& aDest); void CopyASCIItoUTF16(const nsACString& aSource, nsAString& aDest); +MOZ_MUST_USE bool CopyASCIItoUTF16(const nsACString& aSource, nsAString& aDest, + const mozilla::fallible_t&); void LossyCopyUTF16toASCII(const char16ptr_t aSource, nsACString& aDest); void CopyASCIItoUTF16(const char* aSource, nsAString& aDest);