Truncate long TITLE strings to avoid crashing GTK/WM/X. b=167315 r+sr=bzbarsky

This commit is contained in:
mats.palmgren%bredband.net 2005-08-18 08:10:28 +00:00
parent fcc8138442
commit e91966348c
3 changed files with 41 additions and 7 deletions

View File

@ -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<nsIUnicodeEncoder> 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, '?');

View File

@ -211,6 +211,8 @@ static nsWindow *gPluginFocusWindow = NULL;
nsCOMPtr <nsIRollupListener> 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;
}

View File

@ -25,6 +25,7 @@
* B.J. Rossiter <bj@igelaus.com.au>
* Tony Tsui <tony@igelaus.com.au>
* Roland Mainz <roland.mainz@informatik.med.uni-giessen.de>
* Mats Palmgren <mats.palmgren@bredband.net>
*
* 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);