Ticket #3449: mc-3449-glib-crashes-on-invalid-utf8.patch

File mc-3449-glib-crashes-on-invalid-utf8.patch, 1.4 KB (added by egmont, 9 years ago)

Fix

  • lib/search/regex.c

    diff --git a/lib/search/regex.c b/lib/search/regex.c
    index 082fa2c..1711af4 100644
    a b mc_search__run_regex (mc_search_t * lc_mc_search, const void *user_data, 
    877877            virtual_pos = current_pos; 
    878878        } 
    879879 
     880#ifdef SEARCH_TYPE_GLIB 
     881        /* Glib doesn't like invalid UTF-8 so sanitize it first: ticket 3449. 
     882           Be careful: there might be embedded NULs in the strings. */ 
     883        if (lc_mc_search->is_utf8) 
     884        { 
     885            char *p = lc_mc_search->regex_buffer->str; 
     886            char *end = p + lc_mc_search->regex_buffer->len; 
     887            while (p < end) 
     888            { 
     889                gunichar c = g_utf8_get_char_validated (p, -1); 
     890                if (c != (gunichar) (-1) && c != (gunichar) (-2)) 
     891                { 
     892                    p = g_utf8_next_char (p); 
     893                } 
     894                else 
     895                { 
     896                    /* U+FFFD would be the proper choice, but then we'd have to 
     897                       create a new string and maintain mapping between old and new offsets. 
     898                       So rather do a byte by byte replacement. */ 
     899                    *p++ = '\0'; 
     900                } 
     901            } 
     902        } 
     903#endif /* SEARCH_TYPE_GLIB */ 
     904 
    880905        switch (mc_search__regex_found_cond (lc_mc_search, lc_mc_search->regex_buffer)) 
    881906        { 
    882907        case COND__FOUND_OK: