From 2962faed63f4d4520ff34045859420b96b67d5ae Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Fri, 25 Oct 2013 15:44:52 +0100 Subject: fix gtk title setting to cope with null titles coverity 1109897 --- gtk/scaffolding.c | 40 ++++++++++++++++++++++++++++++---------- gtk/tabs.c | 3 +-- gtk/tabs.h | 9 +++++++++ 3 files changed, 40 insertions(+), 12 deletions(-) (limited to 'gtk') diff --git a/gtk/scaffolding.c b/gtk/scaffolding.c index 80b6dea4c..3d86975cc 100644 --- a/gtk/scaffolding.c +++ b/gtk/scaffolding.c @@ -2162,25 +2162,45 @@ nsgtk_scaffolding *nsgtk_new_scaffolding(struct gui_window *toplevel) return g; } +/** set the title in the window + * + * @param gw The gui window to set title on + * @param title The title to set (may be NULL) + */ void gui_window_set_title(struct gui_window *gw, const char *title) { - static char suffix[] = " - NetSurf"; - char nt[strlen(title) + strlen(suffix) + 1]; struct gtk_scaffolding *gs = nsgtk_get_scaffold(gw); + int title_len; + char *newtitle; - nsgtk_tab_set_title(gw, title); - - if (gs->top_level == gw) { - if (title == NULL || title[0] == '\0') { + if ((title == NULL) || (title[0] == '\0')) { + if (gs->top_level != gw) { gtk_window_set_title(gs->window, "NetSurf"); - } else { - strcpy(nt, title); - strcat(nt, suffix); - gtk_window_set_title(gs->window, nt); } + return; + } + + nsgtk_tab_set_title(gw, title); + + if (gs->top_level != gw) { + /* not top level window so do not set window title */ + return; + } + + title_len = strlen(title) + SLEN(" - NetSurf") + 1; + newtitle = malloc(title_len); + if (newtitle == NULL) { + return; } + + snprintf(newtitle, title_len, "%s - NetSurf", title); + + gtk_window_set_title(gs->window, newtitle); + + free(newtitle); } + void gui_window_set_url(struct gui_window *_g, const char *url) { struct gtk_scaffolding *g = nsgtk_get_scaffold(_g); diff --git a/gtk/tabs.c b/gtk/tabs.c index c5ef6fe9c..62a864ed1 100644 --- a/gtk/tabs.c +++ b/gtk/tabs.c @@ -334,8 +334,8 @@ void nsgtk_tab_set_title(struct gui_window *g, const char *title) { GtkWidget *label; GtkWidget *tab; - tab = nsgtk_window_get_tab(g); + tab = nsgtk_window_get_tab(g); if (tab == NULL) { return; } @@ -343,7 +343,6 @@ void nsgtk_tab_set_title(struct gui_window *g, const char *title) label = g_object_get_data(G_OBJECT(tab), "label"); gtk_label_set_text(GTK_LABEL(label), title); gtk_widget_set_tooltip_text(tab, title); - } /* exported interface documented in gtk/tabs.h */ diff --git a/gtk/tabs.h b/gtk/tabs.h index 959799edd..caa683e40 100644 --- a/gtk/tabs.h +++ b/gtk/tabs.h @@ -23,6 +23,15 @@ struct gui_window; void nsgtk_tab_init(struct gtk_scaffolding *gs); void nsgtk_tab_add(struct gui_window *window, GtkWidget *tab_contents, bool background); + +/** set the tab title + * + * The tab title will be set to the parameter + * + * @note currently only called from gui_window_set_title() + * @param g the gui window to set tab title for. + * @param title The title text which may not be NULL. + */ void nsgtk_tab_set_title(struct gui_window *g, const char *title); void nsgtk_tab_options_changed(GtkNotebook *notebook); nserror nsgtk_tab_close_current(GtkNotebook *notebook); -- cgit v1.2.3