From 2f638609081d0bf0c54b0f905f0797e3ef121195 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrico=20Tr=C3=B6ger?= Date: Sun, 25 Apr 2010 17:42:01 +0000 Subject: [PATCH] Replace g_vsnprintf() by g_strdup_vprintf() to avoid truncated strings in case of reaching the buffer size limit (part of #2979697). git-svn-id: https://geany.svn.sourceforge.net/svnroot/geany/trunk@4855 ea778897-0a13-0410-b9d1-a72fbfd435f5 --- ChangeLog | 7 +++++++ src/dialogs.c | 36 +++++++++++++++++++++++------------- src/msgwindow.c | 15 +++++++++------ src/ui_utils.c | 6 ++++-- 4 files changed, 43 insertions(+), 21 deletions(-) diff --git a/ChangeLog b/ChangeLog index b04ec1a3..2c5bb22b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2010-04-25 Enrico Tröger + + * src/build.c, src/dialogs.c, src/msgwindow.c, src/ui_utils.c: + Replace g_vsnprintf() by g_strdup_vprintf() to avoid truncated + strings in case of reaching the buffer size limit (part of #2979697). + + 2010-04-22 Nick Treleaven * src/plugindata.h, src/pluginutils.c, src/pluginutils.h, THANKS: diff --git a/src/dialogs.c b/src/dialogs.c index f1cab57f..4e0316bf 100644 --- a/src/dialogs.c +++ b/src/dialogs.c @@ -607,12 +607,12 @@ void dialogs_show_msgbox(GtkMessageType type, const gchar *text, ...) #ifndef G_OS_WIN32 GtkWidget *dialog; #endif - gchar string[512]; + gchar *string; va_list args; GtkWindow *parent = (main_status.main_window_realized) ? GTK_WINDOW(main_widgets.window) : NULL; va_start(args, text); - g_vsnprintf(string, 511, text, args); + string = g_strdup_vprintf(text, args); va_end(args); #ifdef G_OS_WIN32 @@ -622,6 +622,7 @@ void dialogs_show_msgbox(GtkMessageType type, const gchar *text, ...) type, GTK_BUTTONS_OK, "%s", string); show_msgbox_dialog(dialog, type, parent); #endif + g_free(string); } @@ -1465,18 +1466,21 @@ static gint show_prompt(GtkWidget *parent, **/ gboolean dialogs_show_question(const gchar *text, ...) { - gchar string[512]; + gchar *string; va_list args; GtkWidget *parent = (main_status.main_window_realized) ? main_widgets.window : NULL; + gint result; va_start(args, text); - g_vsnprintf(string, 511, text, args); + string = g_strdup_vprintf(text, args); va_end(args); - return show_prompt(parent, + result = show_prompt(parent, NULL, GTK_RESPONSE_NONE, GTK_STOCK_NO, GTK_RESPONSE_NO, GTK_STOCK_YES, GTK_RESPONSE_YES, - string, NULL) == GTK_RESPONSE_YES; + string, NULL); + g_free(string); + return (result == GTK_RESPONSE_YES); } @@ -1486,17 +1490,20 @@ gboolean dialogs_show_question(const gchar *text, ...) gboolean dialogs_show_question_full(GtkWidget *parent, const gchar *yes_btn, const gchar *no_btn, const gchar *extra_text, const gchar *main_text, ...) { - gchar string[512]; + gint result; + gchar *string; va_list args; va_start(args, main_text); - g_vsnprintf(string, 511, main_text, args); + string = g_strdup_vprintf(main_text, args); va_end(args); - return show_prompt(parent, + result = show_prompt(parent, NULL, GTK_RESPONSE_NONE, no_btn, GTK_RESPONSE_NO, yes_btn, GTK_RESPONSE_YES, - string, extra_text) == GTK_RESPONSE_YES; + string, extra_text); + g_free(string); + return (result == GTK_RESPONSE_YES); } @@ -1510,14 +1517,17 @@ gint dialogs_show_prompt(GtkWidget *parent, const gchar *btn_3, GtkResponseType response_3, const gchar *extra_text, const gchar *main_text, ...) { - gchar string[512]; + gchar *string; va_list args; + gint result; va_start(args, main_text); - g_vsnprintf(string, 511, main_text, args); + string = g_strdup_vprintf(main_text, args); va_end(args); - return show_prompt(parent, btn_1, response_1, btn_2, response_2, btn_3, response_3, + result = show_prompt(parent, btn_1, response_1, btn_2, response_2, btn_3, response_3, string, extra_text); + g_free(string); + return result; } diff --git a/src/msgwindow.c b/src/msgwindow.c index 72ea4933..4587da66 100644 --- a/src/msgwindow.c +++ b/src/msgwindow.c @@ -252,13 +252,14 @@ static const GdkColor *get_color(gint msg_color) **/ void msgwin_compiler_add(gint msg_color, const gchar *format, ...) { - gchar string[512]; + gchar *string; va_list args; va_start(args, format); - g_vsnprintf(string, 512, format, args); + string = g_strdup_vprintf(format, args); va_end(args); msgwin_compiler_add_string(msg_color, string); + g_free(string); } @@ -314,14 +315,15 @@ void msgwin_show_hide(gboolean show) **/ void msgwin_msg_add(gint msg_color, gint line, GeanyDocument *doc, const gchar *format, ...) { - gchar string[512]; + gchar *string; va_list args; va_start(args, format); - g_vsnprintf(string, 512, format, args); + string = g_strdup_vprintf(format, args); va_end(args); msgwin_msg_add_string(msg_color, line, doc, string); + g_free(string); } @@ -361,18 +363,19 @@ void msgwin_msg_add_string(gint msg_color, gint line, GeanyDocument *doc, const void msgwin_status_add(const gchar *format, ...) { GtkTreeIter iter; - gchar string[512]; + gchar *string; gchar *statusmsg, *time_str; va_list args; va_start(args, format); - g_vsnprintf(string, 512, format, args); + string = g_strdup_vprintf(format, args); va_end(args); /* add a timestamp to status messages */ time_str = utils_get_current_time_string(); statusmsg = g_strconcat(time_str, ": ", string, NULL); g_free(time_str); + g_free(string); /* add message to Status window */ gtk_list_store_append(msgwindow.store_status, &iter); diff --git a/src/ui_utils.c b/src/ui_utils.c index 3c9e31c8..c65b7d25 100644 --- a/src/ui_utils.c +++ b/src/ui_utils.c @@ -140,11 +140,11 @@ static void set_statusbar(const gchar *text, gboolean allow_override) * @param format A @c printf -style string. */ void ui_set_statusbar(gboolean log, const gchar *format, ...) { - gchar string[512]; + gchar *string; va_list args; va_start(args, format); - g_vsnprintf(string, 512, format, args); + string = g_strdup_vprintf(format, args); va_end(args); if (! prefs.suppress_status_messages) @@ -152,6 +152,8 @@ void ui_set_statusbar(gboolean log, const gchar *format, ...) if (log || prefs.suppress_status_messages) msgwin_status_add("%s", string); + + g_free(string); }