[gnome-terminal/wip/rishi/window-title-truncate] window: Limit the title's length to avoid integer overflows in Pango



commit b7bb9bebb6a94258ad801754dfeef252adb0402d
Author: Debarshi Ray <debarshir gnome org>
Date:   Wed Mar 4 17:06:21 2020 +0100

    window: Limit the title's length to avoid integer overflows in Pango
    
    GtkLabels with really long texts of the order of 2^21 characters can
    cause integer overflows in Pango when measuring the width of the
    PangoLayout for that text. Tracking down the root cause of the overflow
    in the font stack and fixing it is difficult, so it's prudent to work
    around it by limiting the length of the window title to a sane, yet
    large enough, number.
    
    Pango does integer measurements in Pango units where 1 pixel is 2^10
    Pango units, and font glyphs are roughly 16, or 2^4, pixels wide.
    Therefore, roughly 2^(31-10-4), or 131072, characters is the threshold
    after which Pango might start to overflow.
    
    It's been assumed that a 1000 character limit for the window title is
    fine even with a small font and a 4K display. Hence a number that's
    double that guess (ie., 2048) should be a good starting point.
    
    https://gitlab.gnome.org/GNOME/gnome-terminal/issues/207

 src/terminal-window.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
---
diff --git a/src/terminal-window.c b/src/terminal-window.c
index 7a8953c5..5ab23102 100644
--- a/src/terminal-window.c
+++ b/src/terminal-window.c
@@ -2407,13 +2407,17 @@ sync_screen_title (TerminalScreen *screen,
 {
   TerminalWindowPrivate *priv = window->priv;
   const char *title;
+  gs_free char *title_truncated = NULL;
 
   if (screen != priv->active_screen)
     return;
 
   title = terminal_screen_get_title (screen);
-  gtk_window_set_title (GTK_WINDOW (window),
-                        title && title[0] ? title : _("Terminal"));
+  if (title == NULL || title[0] == '\0')
+    title = _("Terminal");
+
+  title_truncated = g_utf8_substring (title, 0, 2048);
+  gtk_window_set_title (GTK_WINDOW (window), title_truncated);
 }
 
 static void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]