From fff2c0d2c09c9d3451a085abc41c314b009a5a9e Mon Sep 17 00:00:00 2001 From: "mats.palmgren%bredband.net" Date: Thu, 18 Aug 2005 08:11:23 +0000 Subject: [PATCH] Truncate long TITLE strings to avoid crashing GTK/WM/X. b=167315 r+sr=bzbarsky a=asa git-svn-id: svn://10.0.0.236/branches/MOZILLA_1_8_BRANCH@178012 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/widget/src/gtk/nsWindow.cpp | 26 +++++++++++++++++++++----- mozilla/widget/src/gtk2/nsWindow.cpp | 15 +++++++++++++-- mozilla/widget/src/xlib/nsWindow.cpp | 7 +++++++ 3 files changed, 41 insertions(+), 7 deletions(-) diff --git a/mozilla/widget/src/gtk/nsWindow.cpp b/mozilla/widget/src/gtk/nsWindow.cpp index 7968d62ea9b..297b3278db1 100644 --- a/mozilla/widget/src/gtk/nsWindow.cpp +++ b/mozilla/widget/src/gtk/nsWindow.cpp @@ -175,6 +175,8 @@ PLDHashTable* nsWindow::sIconCache; PRBool gJustGotDeactivate = PR_FALSE; PRBool gJustGotActivate = PR_FALSE; +#define NS_WINDOW_TITLE_MAX_LENGTH 4095 + #ifdef USE_XIM struct nsXICLookupEntry : public PLDHashEntryHdr { @@ -2319,19 +2321,29 @@ NS_IMETHODIMP nsWindow::SetTitle(const nsAString& aTitle) PRInt32 platformLen; // Set UTF8_STRING title for NET_WM-supporting window managers - NS_ConvertUTF16toUTF8 utf8_title(aTitle); +#define UTF8_FOLLOWBYTE(ch) (((ch) & 0xC0) == 0x80) + NS_ConvertUTF16toUTF8 titleUTF8(aTitle); + if (titleUTF8.Length() > NS_WINDOW_TITLE_MAX_LENGTH) { + // Truncate overlong titles (bug 167315). Make sure we chop after a + // complete sequence by making sure the next char isn't a follow-byte. + PRUint32 len = NS_WINDOW_TITLE_MAX_LENGTH; + while(UTF8_FOLLOWBYTE(titleUTF8[len])) + --len; + titleUTF8.Truncate(len); + } + XChangeProperty(GDK_DISPLAY(), GDK_WINDOW_XWINDOW(mShell->window), XInternAtom(GDK_DISPLAY(), "_NET_WM_NAME", False), XInternAtom(GDK_DISPLAY(), "UTF8_STRING", False), - 8, PropModeReplace, (unsigned char *) utf8_title.get(), - utf8_title.Length()); + 8, PropModeReplace, (unsigned char *) titleUTF8.get(), + titleUTF8.Length()); // Set UTF8_STRING title for _NET_WM_ICON_NAME as well XChangeProperty(GDK_DISPLAY(), GDK_WINDOW_XWINDOW(mShell->window), XInternAtom(GDK_DISPLAY(), "_NET_WM_ICON_NAME", False), XInternAtom(GDK_DISPLAY(), "UTF8_STRING", False), - 8, PropModeReplace, (unsigned char *) utf8_title.get(), - utf8_title.Length()); + 8, PropModeReplace, (unsigned char *) titleUTF8.get(), + titleUTF8.Length()); nsCOMPtr encoder; // get the charset @@ -2359,6 +2371,10 @@ NS_IMETHODIMP nsWindow::SetTitle(const nsAString& aTitle) PRInt32 len = (PRInt32)aTitle.Length(); encoder->GetMaxLength(title, len, &platformLen); if (platformLen) { + // Truncate overlong titles (bug 167315). + if (platformLen > NS_WINDOW_TITLE_MAX_LENGTH) { + platformLen = NS_WINDOW_TITLE_MAX_LENGTH; + } platformText = NS_REINTERPRET_CAST(char*, nsMemory::Alloc(platformLen + sizeof(char))); if (platformText) { rv = encoder->SetOutputErrorBehavior(nsIUnicodeEncoder::kOnError_Replace, nsnull, '?'); diff --git a/mozilla/widget/src/gtk2/nsWindow.cpp b/mozilla/widget/src/gtk2/nsWindow.cpp index fb42f100d9d..e12a042f132 100644 --- a/mozilla/widget/src/gtk2/nsWindow.cpp +++ b/mozilla/widget/src/gtk2/nsWindow.cpp @@ -211,6 +211,8 @@ static nsWindow *gPluginFocusWindow = NULL; nsCOMPtr gRollupListener; nsWeakPtr gRollupWindow; +#define NS_WINDOW_TITLE_MAX_LENGTH 4095 + #ifdef USE_XIM static nsWindow *gIMEFocusWindow = NULL; @@ -1078,8 +1080,17 @@ nsWindow::SetTitle(const nsAString& aTitle) return NS_OK; // convert the string into utf8 and set the title. - NS_ConvertUCS2toUTF8 utf8title(aTitle); - gtk_window_set_title(GTK_WINDOW(mShell), (const char *)utf8title.get()); +#define UTF8_FOLLOWBYTE(ch) (((ch) & 0xC0) == 0x80) + NS_ConvertUTF16toUTF8 titleUTF8(aTitle); + if (titleUTF8.Length() > NS_WINDOW_TITLE_MAX_LENGTH) { + // Truncate overlong titles (bug 167315). Make sure we chop after a + // complete sequence by making sure the next char isn't a follow-byte. + PRUint32 len = NS_WINDOW_TITLE_MAX_LENGTH; + while(UTF8_FOLLOWBYTE(titleUTF8[len])) + --len; + titleUTF8.Truncate(len); + } + gtk_window_set_title(GTK_WINDOW(mShell), (const char *)titleUTF8.get()); return NS_OK; } diff --git a/mozilla/widget/src/xlib/nsWindow.cpp b/mozilla/widget/src/xlib/nsWindow.cpp index deac8bc301e..1034a242289 100644 --- a/mozilla/widget/src/xlib/nsWindow.cpp +++ b/mozilla/widget/src/xlib/nsWindow.cpp @@ -25,6 +25,7 @@ * B.J. Rossiter * Tony Tsui * Roland Mainz + * Mats Palmgren * * Alternatively, the contents of this file may be used under the terms of * either the GNU General Public License Version 2 or later (the "GPL"), or @@ -50,6 +51,8 @@ #include "nsIPlatformCharset.h" #include "nsIServiceManager.h" +#define NS_WINDOW_TITLE_MAX_LENGTH 4095 + // Variables for grabbing PRBool nsWindow::sIsGrabbing = PR_FALSE; nsWindow *nsWindow::sGrabWindow = nsnull; @@ -751,6 +754,10 @@ NS_IMETHODIMP nsWindow::SetTitle(const nsAString& aTitle) PRInt32 len = (PRInt32)aTitle.Length(); encoder->GetMaxLength(title, len, &platformLen); if (platformLen) { + // Truncate overlong titles (bug 167315). + if (platformLen > NS_WINDOW_TITLE_MAX_LENGTH) { + platformLen = NS_WINDOW_TITLE_MAX_LENGTH; + } platformText = NS_REINTERPRET_CAST(char*, nsMemory::Alloc(platformLen + sizeof(char))); if (platformText) { rv = encoder->Convert(title, &len, platformText, &platformLen);