From f29306cc9aae8365ab52418c589700c0f448fbd4 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Fri, 25 Oct 2013 20:58:19 +0100 Subject: [PATCH] improve gtk source save to use gtk overwrite protection feature coverity 1109873 --- amiga/clipboard.c | 2 +- amiga/utf8.c | 2 +- atari/encoding.c | 2 +- gtk/dialogs/source.c | 135 ++++++++++++++++++------------------------- riscos/ucstables.c | 4 +- utils/utf8.c | 74 +++++++++++++----------- utils/utf8.h | 2 +- 7 files changed, 100 insertions(+), 121 deletions(-) diff --git a/amiga/clipboard.c b/amiga/clipboard.c index e02344c96..d37fb1aec 100644 --- a/amiga/clipboard.c +++ b/amiga/clipboard.c @@ -159,7 +159,7 @@ char *ami_clipboard_cat_collection(struct CollectionItem *ci, LONG codeset, size utf8_from_enc(ci_curr->ci_Data, (const char *)ObtainCharsetInfo(DFCS_NUMBER, codeset, DFCS_MIMENAME), - ci_curr->ci_Size, (char **)&ci_next->ci_Data); + ci_curr->ci_Size, (char **)&ci_next->ci_Data, NULL); ci_next->ci_Size = strlen(ci_next->ci_Data); len += ci_next->ci_Size; break; diff --git a/amiga/utf8.c b/amiga/utf8.c index 76e7b8690..a620ac181 100755 --- a/amiga/utf8.c +++ b/amiga/utf8.c @@ -70,7 +70,7 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len, encname = (const char *) ObtainCharsetInfo(DFCS_NUMBER, charset, DFCS_MIMENAME); #endif - return utf8_from_enc(string,encname,len,result); + return utf8_from_enc(string,encname,len,result,NULL); } utf8_convert_ret utf8_to_local_encoding(const char *string, size_t len, diff --git a/atari/encoding.c b/atari/encoding.c index 75d0fec90..0212d51c4 100644 --- a/atari/encoding.c +++ b/atari/encoding.c @@ -39,7 +39,7 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len, char **result) { - return utf8_from_enc(string, "ATARIST", len, result); + return utf8_from_enc(string, "ATARIST", len, result, NULL); } diff --git a/gtk/dialogs/source.c b/gtk/dialogs/source.c index 4499ccae6..fe9005cc6 100644 --- a/gtk/dialogs/source.c +++ b/gtk/dialogs/source.c @@ -48,6 +48,7 @@ struct nsgtk_source_window { gchar *url; char *data; + size_t data_len; GtkWindow *sourcewindow; GtkTextView *gv; struct browser_window *bw; @@ -113,7 +114,7 @@ static void nsgtk_attach_source_menu_handlers(GtkBuilder *xml, gpointer g) static gboolean nsgtk_source_destroy_event(GtkBuilder *window, gpointer g) { - struct nsgtk_source_window *nsg = (struct nsgtk_source_window *) g; + struct nsgtk_source_window *nsg = (struct nsgtk_source_window *) g; if (nsg->next != NULL) nsg->next->prev = nsg->prev; @@ -136,17 +137,17 @@ static gboolean nsgtk_source_delete_event(GtkWindow * window, gpointer g) } void nsgtk_source_dialog_init(GtkWindow *parent, struct browser_window *bw) -{ +{ char glade_Location[strlen(res_dir_location) + SLEN("source.gtk2.ui") + 1]; if (content_get_type(bw->current_content) != CONTENT_HTML) return; - + if (nsoption_bool(source_tab)) { nsgtk_source_tab_init(parent, bw); return; } - + sprintf(glade_Location, "%ssource.gtk2.ui", res_dir_location); GError* error = NULL; @@ -162,15 +163,17 @@ void nsgtk_source_dialog_init(GtkWindow *parent, struct browser_window *bw) const char *source_data; unsigned long source_size; char *data = NULL; + size_t data_len; - source_data = content_get_source_data(bw->current_content, + source_data = content_get_source_data(bw->current_content, &source_size); utf8_convert_ret r = utf8_from_enc( source_data, html_get_encoding(bw->current_content), source_size, - &data); + &data, + &data_len); if (r == UTF8_CONVERT_NOMEM) { warn_user("NoMemory",0); return; @@ -194,8 +197,8 @@ void nsgtk_source_dialog_init(GtkWindow *parent, struct browser_window *bw) gtk_widget_set_sensitive(deletebutton, FALSE); /* for now */ gtk_widget_set_sensitive(printbutton, FALSE); - - struct nsgtk_source_window *thiswindow = + + struct nsgtk_source_window *thiswindow = malloc(sizeof(struct nsgtk_source_window)); if (thiswindow == NULL) { free(data); @@ -213,7 +216,8 @@ void nsgtk_source_dialog_init(GtkWindow *parent, struct browser_window *bw) } thiswindow->data = data; - + thiswindow->data_len = data_len; + thiswindow->sourcewindow = wndSource; thiswindow->bw = bw; @@ -225,20 +229,20 @@ void nsgtk_source_dialog_init(GtkWindow *parent, struct browser_window *bw) if (nsgtk_source_list != NULL) nsgtk_source_list->prev = thiswindow; nsgtk_source_list = thiswindow; - + nsgtk_attach_source_menu_handlers(glade_File, thiswindow); - + gtk_window_set_title(wndSource, title); - + g_signal_connect(G_OBJECT(wndSource), "destroy", G_CALLBACK(nsgtk_source_destroy_event), thiswindow); - g_signal_connect(G_OBJECT(wndSource), "delete-event", + g_signal_connect(G_OBJECT(wndSource), "delete-event", G_CALLBACK(nsgtk_source_delete_event), thiswindow); - + GtkTextView *sourceview = GTK_TEXT_VIEW( - gtk_builder_get_object(glade_File, + gtk_builder_get_object(glade_File, "source_view")); PangoFontDescription *fontdesc = @@ -249,7 +253,7 @@ void nsgtk_source_dialog_init(GtkWindow *parent, struct browser_window *bw) GtkTextBuffer *tb = gtk_text_view_get_buffer(sourceview); gtk_text_buffer_set_text(tb, thiswindow->data, -1); - + gtk_widget_show(GTK_WIDGET(wndSource)); } @@ -262,6 +266,7 @@ void nsgtk_source_tab_init(GtkWindow *parent, struct browser_window *bw) const char *source_data; unsigned long source_size; char *ndata = 0; + size_t ndata_len; nsurl *url; nserror error; utf8_convert_ret r; @@ -269,13 +274,14 @@ void nsgtk_source_tab_init(GtkWindow *parent, struct browser_window *bw) char *fileurl; gint handle; - source_data = content_get_source_data(bw->current_content, + source_data = content_get_source_data(bw->current_content, &source_size); r = utf8_from_enc(source_data, html_get_encoding(bw->current_content), source_size, - &ndata); + &ndata, + &ndata_len); if (r == UTF8_CONVERT_NOMEM) { warn_user("NoMemory",0); return; @@ -328,66 +334,33 @@ void nsgtk_source_tab_init(GtkWindow *parent, struct browser_window *bw) free(fileurl); } -static void nsgtk_source_file_save(GtkWindow *parent, const char *filename, - const char *data) +static void nsgtk_source_file_save(GtkWindow *parent, const char *filename, + const char *data, size_t data_size) { FILE *f; - bool auth = true; - char temp[255]; - GtkWidget *notif, *label; + GtkWidget *notif; + GtkWidget *label; - if (!(access(filename, F_OK))) { - GtkWidget *confd = gtk_dialog_new_with_buttons( - messages_get("gtkOverwriteTitle"), - parent, - GTK_DIALOG_DESTROY_WITH_PARENT, - GTK_STOCK_OK, - GTK_RESPONSE_ACCEPT, - GTK_STOCK_CANCEL, - GTK_RESPONSE_REJECT, - NULL); - const char *format = messages_get("gtkOverwrite"); - int len = strlen(filename) + strlen(format) + SLEN("\n\n") + 1; - char warn[len]; - auth = false; - - warn[0] = '\n'; - snprintf(warn + 1, len - 2, format, filename); - len = strlen(warn); - warn[len - 1] = '\n'; - warn[len] = '\0'; - - label = gtk_label_new(warn); - gtk_container_add(GTK_CONTAINER(nsgtk_dialog_get_content_area(GTK_DIALOG(confd))), - label); - gtk_widget_show(label); - if (gtk_dialog_run(GTK_DIALOG(confd)) == GTK_RESPONSE_ACCEPT) { - auth = true; - } - gtk_widget_destroy(confd); - } - - if (auth) { - f = fopen(filename, "w+"); - fprintf(f, "%s", data); + f = fopen(filename, "w+"); + if (f != NULL) { + fwrite(data, data_size, 1, f); fclose(f); - snprintf(temp, sizeof(temp), "\n %s" - " \n", - messages_get("gtkSaveConfirm")); - } else { - snprintf(temp, sizeof(temp), "\n %s" - " \n", - messages_get("gtkSaveCancelled")); + return; } - - notif = gtk_dialog_new_with_buttons(temp, - parent, GTK_DIALOG_MODAL, GTK_STOCK_OK, - GTK_RESPONSE_NONE, NULL); + + /* inform user of faliure */ + notif = gtk_dialog_new_with_buttons(messages_get("gtkSaveFailedTitle"), + parent, + GTK_DIALOG_MODAL, GTK_STOCK_OK, + GTK_RESPONSE_NONE, NULL); + g_signal_connect_swapped(notif, "response", - G_CALLBACK(gtk_widget_destroy), notif); - label = gtk_label_new(temp); + G_CALLBACK(gtk_widget_destroy), notif); + + label = gtk_label_new(messages_get("gtkSaveFailed")); gtk_container_add(GTK_CONTAINER(nsgtk_dialog_get_content_area(GTK_DIALOG(notif))), label); gtk_widget_show_all(notif); + } @@ -395,12 +368,12 @@ gboolean nsgtk_on_source_save_as_activate(GtkMenuItem *widget, gpointer g) { struct nsgtk_source_window *nsg = (struct nsgtk_source_window *) g; GtkWidget *fc = gtk_file_chooser_dialog_new( - messages_get("gtkSourceSave"), + messages_get("gtkSourceSave"), nsg->sourcewindow, - GTK_FILE_CHOOSER_ACTION_SAVE, + GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, - GTK_STOCK_SAVE, + GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL); char *filename; @@ -419,9 +392,12 @@ gboolean nsgtk_on_source_save_as_activate(GtkMenuItem *widget, gpointer g) free(filename); + gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(fc), + TRUE); + if (gtk_dialog_run(GTK_DIALOG(fc)) == GTK_RESPONSE_ACCEPT) { filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(fc)); - nsgtk_source_file_save(nsg->sourcewindow, filename, nsg->data); + nsgtk_source_file_save(nsg->sourcewindow, filename, nsg->data, nsg->data_len); g_free(filename); } @@ -443,7 +419,7 @@ gboolean nsgtk_on_source_close_activate( GtkMenuItem *widget, gpointer g) struct nsgtk_source_window *nsg = (struct nsgtk_source_window *) g; gtk_widget_destroy(GTK_WIDGET(nsg->sourcewindow)); - + return TRUE; } @@ -472,7 +448,7 @@ gboolean nsgtk_on_source_copy_activate(GtkMenuItem *widget, gpointer g) struct nsgtk_source_window *nsg = (struct nsgtk_source_window *) g; GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(nsg->gv)); - gtk_text_buffer_copy_clipboard(buf, + gtk_text_buffer_copy_clipboard(buf, gtk_clipboard_get(GDK_SELECTION_CLIPBOARD)); return TRUE; @@ -515,11 +491,11 @@ static void nsgtk_source_update_zoomlevel(gpointer g) GtkTextIter start, end; - gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(buf), + gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(buf), &start, &end); - gtk_text_buffer_remove_all_tags(GTK_TEXT_BUFFER(buf), + gtk_text_buffer_remove_all_tags(GTK_TEXT_BUFFER(buf), &start, &end); - gtk_text_buffer_apply_tag(GTK_TEXT_BUFFER(buf), + gtk_text_buffer_apply_tag(GTK_TEXT_BUFFER(buf), GTK_TEXT_TAG(tag), &start, &end); } nsg = nsg->next; @@ -561,4 +537,3 @@ gboolean nsgtk_on_source_about_activate(GtkMenuItem *widget, gpointer g) return TRUE; } - diff --git a/riscos/ucstables.c b/riscos/ucstables.c index 0a57022a1..970caf122 100644 --- a/riscos/ucstables.c +++ b/riscos/ucstables.c @@ -618,7 +618,7 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len, /* 0 length has a special meaning to utf8_from_enc */ if (off - prev_off > 0) { err = utf8_from_enc(string + prev_off, enc, - off - prev_off, &temp); + off - prev_off, &temp, NULL); if (err != UTF8_CONVERT_OK) { assert(err != UTF8_CONVERT_BADENC); LOG(("utf8_from_enc failed")); @@ -660,7 +660,7 @@ utf8_convert_ret utf8_from_local_encoding(const char *string, size_t len, * NB. 0 length has a special meaning to utf8_from_enc */ if (prev_off < len) { err = utf8_from_enc(string + prev_off, enc, len - prev_off, - &temp); + &temp, NULL); if (err != UTF8_CONVERT_OK) { assert(err != UTF8_CONVERT_BADENC); LOG(("utf8_from_enc failed")); diff --git a/utils/utf8.c b/utils/utf8.c index 127ffe642..8e9587d20 100644 --- a/utils/utf8.c +++ b/utils/utf8.c @@ -33,9 +33,6 @@ #include "utils/log.h" #include "utils/utf8.h" -static utf8_convert_ret utf8_convert(const char *string, size_t len, - const char *from, const char *to, char **result); - /** * Convert a UTF-8 multibyte sequence into a single UCS4 character * @@ -217,35 +214,6 @@ void utf8_finalise(void) utf8_clear_cd_cache(); } -/** - * Convert a UTF8 string into the named encoding - * - * \param string The NULL-terminated string to convert - * \param encname The encoding name (suitable for passing to iconv) - * \param len Length of input string to consider (in bytes), or 0 - * \param result Pointer to location to store result (allocated on heap) - * \return Appropriate utf8_convert_ret value - */ -utf8_convert_ret utf8_to_enc(const char *string, const char *encname, - size_t len, char **result) -{ - return utf8_convert(string, len, "UTF-8", encname, result); -} - -/** - * Convert a string in the named encoding into a UTF-8 string - * - * \param string The NULL-terminated string to convert - * \param encname The encoding name (suitable for passing to iconv) - * \param len Length of input string to consider (in bytes), or 0 - * \param result Pointer to location to store result (allocated on heap) - * \return Appropriate utf8_convert_ret value - */ -utf8_convert_ret utf8_from_enc(const char *string, const char *encname, - size_t len, char **result) -{ - return utf8_convert(string, len, encname, "UTF-8", result); -} /** * Convert a string from one encoding to another @@ -254,11 +222,13 @@ utf8_convert_ret utf8_from_enc(const char *string, const char *encname, * \param len Length of input string to consider (in bytes), or 0 * \param from The encoding name to convert from * \param to The encoding name to convert to - * \param result Pointer to location in which to store result + * \param result Pointer to location in which to store result. + * \param result_len Pointer to location in which to store result length. * \return Appropriate utf8_convert_ret value */ -utf8_convert_ret utf8_convert(const char *string, size_t len, - const char *from, const char *to, char **result) +static utf8_convert_ret utf8_convert(const char *string, size_t len, + const char *from, const char *to, + char **result, size_t *result_len) { iconv_t cd; char *temp, *out, *in; @@ -356,9 +326,43 @@ utf8_convert_ret utf8_convert(const char *string, size_t len, * converted to UTF-32 */ memset((*result) + (out - temp), 0, 4); + if (result_len != NULL) { + *result_len = (out - temp); + } + return UTF8_CONVERT_OK; } +/** + * Convert a UTF8 string into the named encoding + * + * \param string The NULL-terminated string to convert + * \param encname The encoding name (suitable for passing to iconv) + * \param len Length of input string to consider (in bytes), or 0 + * \param result Pointer to location to store result (allocated on heap) + * \return Appropriate utf8_convert_ret value + */ +utf8_convert_ret utf8_to_enc(const char *string, const char *encname, + size_t len, char **result) +{ + return utf8_convert(string, len, "UTF-8", encname, result, NULL); +} + +/** + * Convert a string in the named encoding into a UTF-8 string + * + * \param string The NULL-terminated string to convert + * \param encname The encoding name (suitable for passing to iconv) + * \param len Length of input string to consider (in bytes), or 0 + * \param result Pointer to location to store result (allocated on heap) + * \return Appropriate utf8_convert_ret value + */ +utf8_convert_ret utf8_from_enc(const char *string, const char *encname, + size_t len, char **result, size_t *result_len) +{ + return utf8_convert(string, len, encname, "UTF-8", result, result_len); +} + static utf8_convert_ret utf8_convert_html_chunk(iconv_t cd, const char *chunk, size_t inlen, char **out, size_t *outlen) diff --git a/utils/utf8.h b/utils/utf8.h index eb043c227..68d42d3ea 100644 --- a/utils/utf8.h +++ b/utils/utf8.h @@ -47,7 +47,7 @@ size_t utf8_next(const char *s, size_t l, size_t o); utf8_convert_ret utf8_to_enc(const char *string, const char *encname, size_t len, char **result); utf8_convert_ret utf8_from_enc(const char *string, const char *encname, - size_t len, char **result); + size_t len, char **result, size_t *result_len); utf8_convert_ret utf8_to_html(const char *string, const char *encname, size_t len, char **result);