From 0379b80f1591de150146ab33469a7563ff50ce3b Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Mon, 25 May 2015 12:50:15 +0100 Subject: [PATCH] Bug 1167423 - patch 1 - Handle return values of FallibleTArray functions in Console API, r=smaug --- dom/base/Console.cpp | 117 ++++++++++++++++++++++++++++--------------- dom/base/Console.h | 4 +- 2 files changed, 79 insertions(+), 42 deletions(-) diff --git a/dom/base/Console.cpp b/dom/base/Console.cpp index 7d186af3aaf1..186245700d21 100644 --- a/dom/base/Console.cpp +++ b/dom/base/Console.cpp @@ -174,7 +174,9 @@ public: mMethodString = aString; for (uint32_t i = 0; i < aArguments.Length(); ++i) { - mArguments.AppendElement(aArguments[i]); + if (!mArguments.AppendElement(aArguments[i])) { + return; + } } } @@ -666,7 +668,9 @@ private: return; } - arguments.AppendElement(value); + if (!arguments.AppendElement(value)) { + return; + } } mConsole->ProfileMethod(aCx, mAction, arguments); @@ -826,8 +830,8 @@ Console::Time(JSContext* aCx, const JS::Handle aTime) Sequence data; SequenceRooter rooter(aCx, &data); - if (!aTime.isUndefined()) { - data.AppendElement(aTime); + if (!aTime.isUndefined() && !data.AppendElement(aTime)) { + return; } Method(aCx, MethodTime, NS_LITERAL_STRING("time"), data); @@ -839,8 +843,8 @@ Console::TimeEnd(JSContext* aCx, const JS::Handle aTime) Sequence data; SequenceRooter rooter(aCx, &data); - if (!aTime.isUndefined()) { - data.AppendElement(aTime); + if (!aTime.isUndefined() && !data.AppendElement(aTime)) { + return; } Method(aCx, MethodTimeEnd, NS_LITERAL_STRING("timeEnd"), data); @@ -852,8 +856,8 @@ Console::TimeStamp(JSContext* aCx, const JS::Handle aData) Sequence data; SequenceRooter rooter(aCx, &data); - if (aData.isString()) { - data.AppendElement(aData); + if (aData.isString() && !data.AppendElement(aData)) { + return; } Method(aCx, MethodTimeStamp, NS_LITERAL_STRING("timeStamp"), data); @@ -892,7 +896,9 @@ Console::ProfileMethod(JSContext* aCx, const nsAString& aAction, Sequence& sequence = event.mArguments.Value(); for (uint32_t i = 0; i < aData.Length(); ++i) { - sequence.AppendElement(aData[i]); + if (!sequence.AppendElement(aData[i])) { + return; + } } JS::Rooted eventValue(aCx); @@ -1293,13 +1299,18 @@ Console::ProcessCallData(ConsoleCallData* aData) case MethodAssert: event.mArguments.Construct(); event.mStyles.Construct(); - ProcessArguments(cx, aData->mArguments, event.mArguments.Value(), - event.mStyles.Value()); + if (!ProcessArguments(cx, aData->mArguments, event.mArguments.Value(), + event.mStyles.Value())) { + return; + } + break; default: event.mArguments.Construct(); - ArgumentsToValueList(aData->mArguments, event.mArguments.Value()); + if (!ArgumentsToValueList(aData->mArguments, event.mArguments.Value())) { + return; + } } if (aData->mMethodName == MethodGroup || @@ -1418,48 +1429,52 @@ Console::ProcessCallData(ConsoleCallData* aData) namespace { // Helper method for ProcessArguments. Flushes output, if non-empty, to aSequence. -void -FlushOutput(JSContext* aCx, Sequence& aSequence, nsString &output) +bool +FlushOutput(JSContext* aCx, Sequence& aSequence, nsString &aOutput) { - if (!output.IsEmpty()) { + if (!aOutput.IsEmpty()) { JS::Rooted str(aCx, JS_NewUCStringCopyN(aCx, - output.get(), - output.Length())); + aOutput.get(), + aOutput.Length())); if (!str) { - return; + return false; } - aSequence.AppendElement(JS::StringValue(str)); - output.Truncate(); + if (!aSequence.AppendElement(JS::StringValue(str))) { + return false; + } + + aOutput.Truncate(); } + + return true; } } // anonymous namespace -void +bool Console::ProcessArguments(JSContext* aCx, const nsTArray>& aData, Sequence& aSequence, Sequence& aStyles) { if (aData.IsEmpty()) { - return; + return true; } if (aData.Length() == 1 || !aData[0].isString()) { - ArgumentsToValueList(aData, aSequence); - return; + return ArgumentsToValueList(aData, aSequence); } JS::Rooted format(aCx, aData[0]); JS::Rooted jsString(aCx, JS::ToString(aCx, format)); if (!jsString) { - return; + return false; } nsAutoJSString string; if (!string.init(aCx, jsString)) { - return; + return false; } nsString::const_iterator start, end; @@ -1547,35 +1562,47 @@ Console::ProcessArguments(JSContext* aCx, case 'o': case 'O': { - FlushOutput(aCx, aSequence, output); + if (!FlushOutput(aCx, aSequence, output)) { + return false; + } JS::Rooted v(aCx); if (index < aData.Length()) { v = aData[index++]; } - aSequence.AppendElement(v); + if (!aSequence.AppendElement(v)) { + return false; + } + break; } case 'c': { - FlushOutput(aCx, aSequence, output); + if (!FlushOutput(aCx, aSequence, output)) { + return false; + } if (index < aData.Length()) { JS::Rooted v(aCx, aData[index++]); JS::Rooted jsString(aCx, JS::ToString(aCx, v)); if (!jsString) { - return; + return false; } int32_t diff = aSequence.Length() - aStyles.Length(); if (diff > 0) { for (int32_t i = 0; i < diff; i++) { - aStyles.AppendElement(JS::NullValue()); + if (!aStyles.AppendElement(JS::NullValue())) { + return false; + } } } - aStyles.AppendElement(JS::StringValue(jsString)); + + if (!aStyles.AppendElement(JS::StringValue(jsString))) { + return false; + } } break; } @@ -1585,12 +1612,12 @@ Console::ProcessArguments(JSContext* aCx, JS::Rooted value(aCx, aData[index++]); JS::Rooted jsString(aCx, JS::ToString(aCx, value)); if (!jsString) { - return; + return false; } nsAutoJSString v; if (!v.init(aCx, jsString)) { - return; + return false; } output.Append(v); @@ -1604,7 +1631,7 @@ Console::ProcessArguments(JSContext* aCx, int32_t v; if (!JS::ToInt32(aCx, value, &v)) { - return; + return false; } nsCString format; @@ -1619,7 +1646,7 @@ Console::ProcessArguments(JSContext* aCx, double v; if (!JS::ToNumber(aCx, value, &v)) { - return; + return false; } nsCString format; @@ -1634,7 +1661,9 @@ Console::ProcessArguments(JSContext* aCx, } } - FlushOutput(aCx, aSequence, output); + if (!FlushOutput(aCx, aSequence, output)) { + return false; + } // Discard trailing style element if there is no output to apply it to. if (aStyles.Length() > aSequence.Length()) { @@ -1643,8 +1672,12 @@ Console::ProcessArguments(JSContext* aCx, // The rest of the array, if unused by the format string. for (; index < aData.Length(); ++index) { - aSequence.AppendElement(aData[index]); + if (!aSequence.AppendElement(aData[index])) { + return false; + } } + + return true; } void @@ -1770,13 +1803,17 @@ Console::StopTimer(JSContext* aCx, const JS::Value& aName, return value; } -void +bool Console::ArgumentsToValueList(const nsTArray>& aData, Sequence& aSequence) { for (uint32_t i = 0; i < aData.Length(); ++i) { - aSequence.AppendElement(aData[i]); + if (!aSequence.AppendElement(aData[i])) { + return false; + } } + + return true; } JS::Value diff --git a/dom/base/Console.h b/dom/base/Console.h index 4fff166d3d2d..8c9584863962 100644 --- a/dom/base/Console.h +++ b/dom/base/Console.h @@ -158,7 +158,7 @@ private: // finds based the format string. The index of the styles matches the indexes // of elements that need the custom styling from aSequence. For elements with // no custom styling the array is padded with null elements. - void + bool ProcessArguments(JSContext* aCx, const nsTArray>& aData, Sequence& aSequence, Sequence& aStyles); @@ -182,7 +182,7 @@ private: DOMHighResTimeStamp aTimestamp); // The method populates a Sequence from an array of JS::Value. - void + bool ArgumentsToValueList(const nsTArray>& aData, Sequence& aSequence);