From 9910e03b3b25350a9c546e15d0e1a648e4e2347a Mon Sep 17 00:00:00 2001 From: "asqueella%gmail.com" Date: Sun, 11 Feb 2007 00:14:15 +0000 Subject: [PATCH] Bug 310037 - alert() text is cut off by null byte (fix by stripping the null bytes in nsGlobalWindow::Alert, Prompt, and Confirm) p=Ryan Jones r=sicking sr=bzbarsky --- dom/src/base/nsGlobalWindow.cpp | 50 +++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/dom/src/base/nsGlobalWindow.cpp b/dom/src/base/nsGlobalWindow.cpp index 87a3ce8a763b..4b1b647b2a61 100644 --- a/dom/src/base/nsGlobalWindow.cpp +++ b/dom/src/base/nsGlobalWindow.cpp @@ -28,6 +28,7 @@ * Vidur Apparao * Johnny Stenback * Mark Hammond + * Ryan Jones * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), @@ -321,6 +322,29 @@ IsAboutBlank(nsIURI* aURI) return str.EqualsLiteral("about:blank"); } +static void +StripNullChars(const nsAString& aInStr, + nsAString& aOutStr) +{ + // In common cases where we don't have nulls in the + // string we can simple simply bypass the checking code. + PRInt32 firstNullPos = aInStr.FindChar('\0'); + if (firstNullPos == kNotFound) { + aOutStr.Assign(aInStr); + return; + } + + nsAString::const_iterator start, end; + aInStr.BeginReading(start); + aInStr.EndReading(end); + + while (start != end) { + if (*start != '\0') + aOutStr.Append(*start); + ++start; + } +} + /** * An indirect observer object that means we don't have to implement nsIObserver * on nsGlobalWindow, where any script could see it. @@ -3504,7 +3528,12 @@ nsGlobalWindow::Alert(const nsAString& aString) nsAutoString title; MakeScriptDialogTitle(title); - return prompter->Alert(title.get(), PromiseFlatString(*str).get()); + // Remove non-terminating null characters from the + // string. See bug #310037. + nsAutoString final; + StripNullChars(*str, final); + + return prompter->Alert(title.get(), final.get()); } NS_IMETHODIMP @@ -3529,8 +3558,13 @@ nsGlobalWindow::Confirm(const nsAString& aString, PRBool* aReturn) nsAutoString title; MakeScriptDialogTitle(title); - return prompter->Confirm(title.get(), - PromiseFlatString(aString).get(), aReturn); + // Remove non-terminating null characters from the + // string. See bug #310037. + nsAutoString final; + StripNullChars(aString, final); + + return prompter->Confirm(title.get(), final.get(), + aReturn); } NS_IMETHODIMP @@ -3565,9 +3599,15 @@ nsGlobalWindow::Prompt(const nsAString& aMessage, const nsAString& aInitial, nsAutoString title; MakeScriptDialogTitle(title); + + // Remove non-terminating null characters from the + // string. See bug #310037. + nsAutoString fixedMessage, fixedInitial; + StripNullChars(aMessage, fixedMessage); + StripNullChars(aInitial, fixedInitial); - rv = prompter->Prompt(title.get(), PromiseFlatString(aMessage).get(), nsnull, - aSavePassword, PromiseFlatString(aInitial).get(), + rv = prompter->Prompt(title.get(), fixedMessage.get(), nsnull, + aSavePassword, fixedInitial.get(), getter_Copies(uniResult), &b); NS_ENSURE_SUCCESS(rv, rv);