From cd9e93b2d10be1c3f996df94b911ad7b4f81b351 Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Fri, 29 Jan 2010 10:23:33 +0200 Subject: [PATCH 1/2] Ticket #1982: Segfault while opening gzip archive MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. touch /tmp/IzV-Útěk_do_bezpečí.gz 2. open mc 3. move cursor at /tmp/IzV-Útěk_do_bezpečí.gz 4. press F4 5. you will see an error message, press Dismiss 6. segfault Fix issue: length of command line string for pipeing now calculated automatically (via g_strdup_printf()) rathen than calculating via str_term_width1() function. Signed-off-by: Slava Zanko --- src/editor/edit.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/editor/edit.c b/src/editor/edit.c index 7f94c9f39..5fa2ca543 100644 --- a/src/editor/edit.c +++ b/src/editor/edit.c @@ -355,15 +355,13 @@ static int edit_find_filter (const char *filename) static char * edit_get_filter (const char *filename) { - int i, l; + int i; char *p, *quoted_name; i = edit_find_filter (filename); if (i < 0) return 0; quoted_name = name_quote (filename, 0); - l = str_term_width1 (quoted_name); - p = g_malloc0 (str_term_width1 (all_filters[i].read) + l + 2); - sprintf (p, all_filters[i].read, quoted_name); + p = g_strdup_printf(all_filters[i].read, quoted_name); g_free (quoted_name); return p; } @@ -371,15 +369,13 @@ edit_get_filter (const char *filename) char * edit_get_write_filter (const char *write_name, const char *filename) { - int i, l; + int i; char *p, *writename; i = edit_find_filter (filename); if (i < 0) return 0; writename = name_quote (write_name, 0); - l = str_term_width1 (writename); - p = g_malloc0 (str_term_width1 (all_filters[i].write) + l + 2); - sprintf (p, all_filters[i].write, writename); + p = g_strdup_printf(all_filters[i].write, writename); g_free (writename); return p; } From 59754181638274b41804c4e92e9a0785aebe2052 Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Fri, 29 Jan 2010 10:30:38 +0200 Subject: [PATCH 2/2] edit.c: Some code refactoring&cleanup Signed-off-by: Slava Zanko --- src/editor/edit.c | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/editor/edit.c b/src/editor/edit.c index 5fa2ca543..4f1952515 100644 --- a/src/editor/edit.c +++ b/src/editor/edit.c @@ -340,8 +340,10 @@ static const struct edit_filters { static int edit_find_filter (const char *filename) { size_t i, l, e; - if (!filename) + + if (filename == NULL) return -1; + l = strlen (filename); for (i = 0; i < sizeof (all_filters) / sizeof (all_filters[0]); i++) { e = strlen (all_filters[i].extension); @@ -357,9 +359,11 @@ edit_get_filter (const char *filename) { int i; char *p, *quoted_name; + i = edit_find_filter (filename); if (i < 0) - return 0; + return NULL; + quoted_name = name_quote (filename, 0); p = g_strdup_printf(all_filters[i].read, quoted_name); g_free (quoted_name); @@ -371,9 +375,11 @@ edit_get_write_filter (const char *write_name, const char *filename) { int i; char *p, *writename; + i = edit_find_filter (filename); if (i < 0) - return 0; + return NULL; + writename = name_quote (write_name, 0); p = g_strdup_printf(all_filters[i].write, writename); g_free (writename); @@ -484,26 +490,28 @@ int edit_insert_file (WEdit *edit, const char *filename) { char *p; - if ((p = edit_get_filter (filename))) { + + p = edit_get_filter (filename); + if (p != NULL) { FILE *f; long current = edit->curs1; f = (FILE *) popen (p, "r"); - if (f) { + if (f != NULL) { edit_insert_stream (edit, f); edit_cursor_move (edit, current - edit->curs1); if (pclose (f) > 0) { - GString *errmsg = g_string_new (NULL); - g_string_sprintf (errmsg, _(" Error reading from pipe: %s "), p); - edit_error_dialog (_("Error"), errmsg->str); - g_string_free (errmsg, TRUE); + char *errmsg; + errmsg = g_strdup_printf(_(" Error reading from pipe: %s "), p); + edit_error_dialog (_("Error"), errmsg); + g_free (errmsg); g_free (p); return 0; } } else { - GString *errmsg = g_string_new (NULL); - g_string_sprintf (errmsg, _(" Cannot open pipe for reading: %s "), p); - edit_error_dialog (_("Error"), errmsg->str); - g_string_free (errmsg, TRUE); + char *errmsg; + errmsg = g_strdup_printf (_(" Cannot open pipe for reading: %s "), p); + edit_error_dialog (_("Error"), errmsg); + g_free (errmsg); g_free (p); return 0; }