From 2887d5c710bd8da2a1837d2ee7091a1dca01e8cf Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Fri, 4 Dec 2009 11:27:24 +0200 Subject: [PATCH] Ticket #1858: Segmentation fault when search in different encodings Test case: 1. Make two files with same content but with different encodings (for example, in UTF-8 and KOI8-R); 2. Open UTF-8 file for editing. Press Alt+e and set 'UTF-8' encoding; 3. Search someting 4. exit from editor and open file with 'KOI8-R' encoding; 5. You must see a dots. Now press shift+F7 (continue search); 6. Segfault here Fix issue: Now function mc_search__recode_str() returns newly allocated string in any case Signed-off-by: Slava Zanko --- src/search/lib.c | 20 ++++++++++---------- src/search/search.c | 2 -- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/search/lib.c b/src/search/lib.c index 30e7e8bba..236977e9f 100644 --- a/src/search/lib.c +++ b/src/search/lib.c @@ -67,11 +67,19 @@ mc_search__recode_str (const char *str, gsize str_len, } conv = g_iconv_open (charset_to, charset_from); - if (conv == INVALID_CONV) - return NULL; + if (conv == INVALID_CONV) { + *bytes_written = str_len; + return g_strndup (str, str_len); + } ret = g_convert_with_iconv (str, str_len, conv, &bytes_read, bytes_written, NULL); g_iconv_close (conv); + + if (ret == NULL) { + *bytes_written = str_len; + return g_strndup (str, str_len); + } + return ret; } @@ -152,8 +160,6 @@ mc_search__tolower_case_str (const char *charset, const char *str, gsize str_len tmp_str2 = converted_str = mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len); - if (converted_str == NULL) - return NULL; tmp_len = converted_str_len + 1; @@ -166,8 +172,6 @@ mc_search__tolower_case_str (const char *charset, const char *str, gsize str_len tmp_str2 = mc_search__recode_str (converted_str, converted_str_len, cp_display, charset, &tmp_len); g_free (converted_str); - if (tmp_str2 == NULL) - return NULL; ret = g_string_new_len (tmp_str2, tmp_len); g_free (tmp_str2); @@ -206,8 +210,6 @@ mc_search__toupper_case_str (const char *charset, const char *str, gsize str_len tmp_str2 = converted_str = mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len); - if (converted_str == NULL) - return NULL; tmp_len = converted_str_len + 1; @@ -221,8 +223,6 @@ mc_search__toupper_case_str (const char *charset, const char *str, gsize str_len tmp_str2 = mc_search__recode_str (converted_str, converted_str_len, cp_display, charset, &tmp_len); g_free (converted_str); - if (tmp_str2 == NULL) - return NULL; ret = g_string_new_len (tmp_str2, tmp_len); g_free (tmp_str2); diff --git a/src/search/search.c b/src/search/search.c index fbfc5af94..d741f3f4e 100644 --- a/src/search/search.c +++ b/src/search/search.c @@ -197,8 +197,6 @@ mc_search_prepare (mc_search_t * lc_mc_search) buffer = mc_search__recode_str (lc_mc_search->original, lc_mc_search->original_len, cp_source, codepages[loop1].id, &recoded_str_len); - if (buffer == NULL) - continue; g_ptr_array_add (ret, mc_search__cond_struct_new (lc_mc_search, buffer,