improve gtk source save to use gtk overwrite protection feature coverity 1109873

This commit is contained in:
Vincent Sanders 2013-10-25 20:58:19 +01:00
parent 1d326a8a1c
commit f29306cc9a
7 changed files with 100 additions and 121 deletions

View File

@ -159,7 +159,7 @@ char *ami_clipboard_cat_collection(struct CollectionItem *ci, LONG codeset, size
utf8_from_enc(ci_curr->ci_Data, utf8_from_enc(ci_curr->ci_Data,
(const char *)ObtainCharsetInfo(DFCS_NUMBER, (const char *)ObtainCharsetInfo(DFCS_NUMBER,
codeset, DFCS_MIMENAME), 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); ci_next->ci_Size = strlen(ci_next->ci_Data);
len += ci_next->ci_Size; len += ci_next->ci_Size;
break; break;

View File

@ -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); encname = (const char *) ObtainCharsetInfo(DFCS_NUMBER, charset, DFCS_MIMENAME);
#endif #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, utf8_convert_ret utf8_to_local_encoding(const char *string, size_t len,

View File

@ -39,7 +39,7 @@ utf8_convert_ret utf8_from_local_encoding(const char *string,
size_t len, size_t len,
char **result) char **result)
{ {
return utf8_from_enc(string, "ATARIST", len, result); return utf8_from_enc(string, "ATARIST", len, result, NULL);
} }

View File

@ -48,6 +48,7 @@
struct nsgtk_source_window { struct nsgtk_source_window {
gchar *url; gchar *url;
char *data; char *data;
size_t data_len;
GtkWindow *sourcewindow; GtkWindow *sourcewindow;
GtkTextView *gv; GtkTextView *gv;
struct browser_window *bw; 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) 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) if (nsg->next != NULL)
nsg->next->prev = nsg->prev; 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) void nsgtk_source_dialog_init(GtkWindow *parent, struct browser_window *bw)
{ {
char glade_Location[strlen(res_dir_location) + SLEN("source.gtk2.ui") char glade_Location[strlen(res_dir_location) + SLEN("source.gtk2.ui")
+ 1]; + 1];
if (content_get_type(bw->current_content) != CONTENT_HTML) if (content_get_type(bw->current_content) != CONTENT_HTML)
return; return;
if (nsoption_bool(source_tab)) { if (nsoption_bool(source_tab)) {
nsgtk_source_tab_init(parent, bw); nsgtk_source_tab_init(parent, bw);
return; return;
} }
sprintf(glade_Location, "%ssource.gtk2.ui", res_dir_location); sprintf(glade_Location, "%ssource.gtk2.ui", res_dir_location);
GError* error = NULL; GError* error = NULL;
@ -162,15 +163,17 @@ void nsgtk_source_dialog_init(GtkWindow *parent, struct browser_window *bw)
const char *source_data; const char *source_data;
unsigned long source_size; unsigned long source_size;
char *data = NULL; 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); &source_size);
utf8_convert_ret r = utf8_from_enc( utf8_convert_ret r = utf8_from_enc(
source_data, source_data,
html_get_encoding(bw->current_content), html_get_encoding(bw->current_content),
source_size, source_size,
&data); &data,
&data_len);
if (r == UTF8_CONVERT_NOMEM) { if (r == UTF8_CONVERT_NOMEM) {
warn_user("NoMemory",0); warn_user("NoMemory",0);
return; return;
@ -194,8 +197,8 @@ void nsgtk_source_dialog_init(GtkWindow *parent, struct browser_window *bw)
gtk_widget_set_sensitive(deletebutton, FALSE); gtk_widget_set_sensitive(deletebutton, FALSE);
/* for now */ /* for now */
gtk_widget_set_sensitive(printbutton, FALSE); gtk_widget_set_sensitive(printbutton, FALSE);
struct nsgtk_source_window *thiswindow = struct nsgtk_source_window *thiswindow =
malloc(sizeof(struct nsgtk_source_window)); malloc(sizeof(struct nsgtk_source_window));
if (thiswindow == NULL) { if (thiswindow == NULL) {
free(data); free(data);
@ -213,7 +216,8 @@ void nsgtk_source_dialog_init(GtkWindow *parent, struct browser_window *bw)
} }
thiswindow->data = data; thiswindow->data = data;
thiswindow->data_len = data_len;
thiswindow->sourcewindow = wndSource; thiswindow->sourcewindow = wndSource;
thiswindow->bw = bw; thiswindow->bw = bw;
@ -225,20 +229,20 @@ void nsgtk_source_dialog_init(GtkWindow *parent, struct browser_window *bw)
if (nsgtk_source_list != NULL) if (nsgtk_source_list != NULL)
nsgtk_source_list->prev = thiswindow; nsgtk_source_list->prev = thiswindow;
nsgtk_source_list = thiswindow; nsgtk_source_list = thiswindow;
nsgtk_attach_source_menu_handlers(glade_File, thiswindow); nsgtk_attach_source_menu_handlers(glade_File, thiswindow);
gtk_window_set_title(wndSource, title); gtk_window_set_title(wndSource, title);
g_signal_connect(G_OBJECT(wndSource), "destroy", g_signal_connect(G_OBJECT(wndSource), "destroy",
G_CALLBACK(nsgtk_source_destroy_event), G_CALLBACK(nsgtk_source_destroy_event),
thiswindow); thiswindow);
g_signal_connect(G_OBJECT(wndSource), "delete-event", g_signal_connect(G_OBJECT(wndSource), "delete-event",
G_CALLBACK(nsgtk_source_delete_event), G_CALLBACK(nsgtk_source_delete_event),
thiswindow); thiswindow);
GtkTextView *sourceview = GTK_TEXT_VIEW( GtkTextView *sourceview = GTK_TEXT_VIEW(
gtk_builder_get_object(glade_File, gtk_builder_get_object(glade_File,
"source_view")); "source_view"));
PangoFontDescription *fontdesc = 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); GtkTextBuffer *tb = gtk_text_view_get_buffer(sourceview);
gtk_text_buffer_set_text(tb, thiswindow->data, -1); gtk_text_buffer_set_text(tb, thiswindow->data, -1);
gtk_widget_show(GTK_WIDGET(wndSource)); 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; const char *source_data;
unsigned long source_size; unsigned long source_size;
char *ndata = 0; char *ndata = 0;
size_t ndata_len;
nsurl *url; nsurl *url;
nserror error; nserror error;
utf8_convert_ret r; utf8_convert_ret r;
@ -269,13 +274,14 @@ void nsgtk_source_tab_init(GtkWindow *parent, struct browser_window *bw)
char *fileurl; char *fileurl;
gint handle; gint handle;
source_data = content_get_source_data(bw->current_content, source_data = content_get_source_data(bw->current_content,
&source_size); &source_size);
r = utf8_from_enc(source_data, r = utf8_from_enc(source_data,
html_get_encoding(bw->current_content), html_get_encoding(bw->current_content),
source_size, source_size,
&ndata); &ndata,
&ndata_len);
if (r == UTF8_CONVERT_NOMEM) { if (r == UTF8_CONVERT_NOMEM) {
warn_user("NoMemory",0); warn_user("NoMemory",0);
return; return;
@ -328,66 +334,33 @@ void nsgtk_source_tab_init(GtkWindow *parent, struct browser_window *bw)
free(fileurl); free(fileurl);
} }
static void nsgtk_source_file_save(GtkWindow *parent, const char *filename, static void nsgtk_source_file_save(GtkWindow *parent, const char *filename,
const char *data) const char *data, size_t data_size)
{ {
FILE *f; FILE *f;
bool auth = true; GtkWidget *notif;
char temp[255]; GtkWidget *label;
GtkWidget *notif, *label;
if (!(access(filename, F_OK))) { f = fopen(filename, "w+");
GtkWidget *confd = gtk_dialog_new_with_buttons( if (f != NULL) {
messages_get("gtkOverwriteTitle"), fwrite(data, data_size, 1, f);
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);
fclose(f); fclose(f);
snprintf(temp, sizeof(temp), "\n %s" return;
" \n",
messages_get("gtkSaveConfirm"));
} else {
snprintf(temp, sizeof(temp), "\n %s"
" \n",
messages_get("gtkSaveCancelled"));
} }
notif = gtk_dialog_new_with_buttons(temp, /* inform user of faliure */
parent, GTK_DIALOG_MODAL, GTK_STOCK_OK, notif = gtk_dialog_new_with_buttons(messages_get("gtkSaveFailedTitle"),
GTK_RESPONSE_NONE, NULL); parent,
GTK_DIALOG_MODAL, GTK_STOCK_OK,
GTK_RESPONSE_NONE, NULL);
g_signal_connect_swapped(notif, "response", g_signal_connect_swapped(notif, "response",
G_CALLBACK(gtk_widget_destroy), notif); G_CALLBACK(gtk_widget_destroy), notif);
label = gtk_label_new(temp);
label = gtk_label_new(messages_get("gtkSaveFailed"));
gtk_container_add(GTK_CONTAINER(nsgtk_dialog_get_content_area(GTK_DIALOG(notif))), label); gtk_container_add(GTK_CONTAINER(nsgtk_dialog_get_content_area(GTK_DIALOG(notif))), label);
gtk_widget_show_all(notif); 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; struct nsgtk_source_window *nsg = (struct nsgtk_source_window *) g;
GtkWidget *fc = gtk_file_chooser_dialog_new( GtkWidget *fc = gtk_file_chooser_dialog_new(
messages_get("gtkSourceSave"), messages_get("gtkSourceSave"),
nsg->sourcewindow, nsg->sourcewindow,
GTK_FILE_CHOOSER_ACTION_SAVE, GTK_FILE_CHOOSER_ACTION_SAVE,
GTK_STOCK_CANCEL, GTK_STOCK_CANCEL,
GTK_RESPONSE_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_SAVE, GTK_STOCK_SAVE,
GTK_RESPONSE_ACCEPT, GTK_RESPONSE_ACCEPT,
NULL); NULL);
char *filename; char *filename;
@ -419,9 +392,12 @@ gboolean nsgtk_on_source_save_as_activate(GtkMenuItem *widget, gpointer g)
free(filename); free(filename);
gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(fc),
TRUE);
if (gtk_dialog_run(GTK_DIALOG(fc)) == GTK_RESPONSE_ACCEPT) { if (gtk_dialog_run(GTK_DIALOG(fc)) == GTK_RESPONSE_ACCEPT) {
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(fc)); 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); 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; struct nsgtk_source_window *nsg = (struct nsgtk_source_window *) g;
gtk_widget_destroy(GTK_WIDGET(nsg->sourcewindow)); gtk_widget_destroy(GTK_WIDGET(nsg->sourcewindow));
return TRUE; 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; struct nsgtk_source_window *nsg = (struct nsgtk_source_window *) g;
GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(nsg->gv)); 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)); gtk_clipboard_get(GDK_SELECTION_CLIPBOARD));
return TRUE; return TRUE;
@ -515,11 +491,11 @@ static void nsgtk_source_update_zoomlevel(gpointer g)
GtkTextIter start, end; GtkTextIter start, end;
gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(buf), gtk_text_buffer_get_bounds(GTK_TEXT_BUFFER(buf),
&start, &end); &start, &end);
gtk_text_buffer_remove_all_tags(GTK_TEXT_BUFFER(buf), gtk_text_buffer_remove_all_tags(GTK_TEXT_BUFFER(buf),
&start, &end); &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); GTK_TEXT_TAG(tag), &start, &end);
} }
nsg = nsg->next; nsg = nsg->next;
@ -561,4 +537,3 @@ gboolean nsgtk_on_source_about_activate(GtkMenuItem *widget, gpointer g)
return TRUE; return TRUE;
} }

View File

@ -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 */ /* 0 length has a special meaning to utf8_from_enc */
if (off - prev_off > 0) { if (off - prev_off > 0) {
err = utf8_from_enc(string + prev_off, enc, err = utf8_from_enc(string + prev_off, enc,
off - prev_off, &temp); off - prev_off, &temp, NULL);
if (err != UTF8_CONVERT_OK) { if (err != UTF8_CONVERT_OK) {
assert(err != UTF8_CONVERT_BADENC); assert(err != UTF8_CONVERT_BADENC);
LOG(("utf8_from_enc failed")); 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 */ * NB. 0 length has a special meaning to utf8_from_enc */
if (prev_off < len) { if (prev_off < len) {
err = utf8_from_enc(string + prev_off, enc, len - prev_off, err = utf8_from_enc(string + prev_off, enc, len - prev_off,
&temp); &temp, NULL);
if (err != UTF8_CONVERT_OK) { if (err != UTF8_CONVERT_OK) {
assert(err != UTF8_CONVERT_BADENC); assert(err != UTF8_CONVERT_BADENC);
LOG(("utf8_from_enc failed")); LOG(("utf8_from_enc failed"));

View File

@ -33,9 +33,6 @@
#include "utils/log.h" #include "utils/log.h"
#include "utils/utf8.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 * Convert a UTF-8 multibyte sequence into a single UCS4 character
* *
@ -217,35 +214,6 @@ void utf8_finalise(void)
utf8_clear_cd_cache(); 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 * 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 len Length of input string to consider (in bytes), or 0
* \param from The encoding name to convert from * \param from The encoding name to convert from
* \param to The encoding name to convert to * \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 * \return Appropriate utf8_convert_ret value
*/ */
utf8_convert_ret utf8_convert(const char *string, size_t len, static utf8_convert_ret utf8_convert(const char *string, size_t len,
const char *from, const char *to, char **result) const char *from, const char *to,
char **result, size_t *result_len)
{ {
iconv_t cd; iconv_t cd;
char *temp, *out, *in; char *temp, *out, *in;
@ -356,9 +326,43 @@ utf8_convert_ret utf8_convert(const char *string, size_t len,
* converted to UTF-32 */ * converted to UTF-32 */
memset((*result) + (out - temp), 0, 4); memset((*result) + (out - temp), 0, 4);
if (result_len != NULL) {
*result_len = (out - temp);
}
return UTF8_CONVERT_OK; 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, static utf8_convert_ret utf8_convert_html_chunk(iconv_t cd,
const char *chunk, size_t inlen, const char *chunk, size_t inlen,
char **out, size_t *outlen) char **out, size_t *outlen)

View File

@ -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, utf8_convert_ret utf8_to_enc(const char *string, const char *encname,
size_t len, char **result); size_t len, char **result);
utf8_convert_ret utf8_from_enc(const char *string, const char *encname, 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, utf8_convert_ret utf8_to_html(const char *string, const char *encname,
size_t len, char **result); size_t len, char **result);