Ticket #1591 (mcview segfault)

fix: segfault mcviev in case when the source_codepage is not equal to the display_codepage.
    refactoring: some code optimization.

Signed-off-by: Ilia Maslakov <il.smind@google.com>
Signed-off-by: Slava Zanko <slavazanko@gmail.com>
This commit is contained in:
Ilia Maslakov 2009-09-08 12:31:15 +00:00 committed by Slava Zanko
parent 3f5968604b
commit 0ce25dce74
4 changed files with 36 additions and 54 deletions

View File

@ -274,6 +274,7 @@ void mcview_toggle_hex_mode (mcview_t *);
gboolean mcview_ok_to_quit (mcview_t *);
void mcview_done (mcview_t *);
void mcview_select_encoding (mcview_t *);
void mcview_set_codeset (mcview_t *);
void mcview_show_error (mcview_t *, const char *);
/* move.c */

View File

@ -51,7 +51,7 @@
/*** global variables ****************************************************************************/
#define OFF_T_BITWIDTH (unsigned int) (sizeof (off_t) * CHAR_BIT - 1)
const off_t INVALID_OFFSET = (off_t) -1;
const off_t INVALID_OFFSET = (off_t) - 1;
const off_t OFFSETTYPE_MAX = ((off_t) 1 << (OFF_T_BITWIDTH - 1)) - 1;
/*** file scope macro definitions ****************************************************************/
@ -195,11 +195,34 @@ mcview_done (mcview_t * view)
g_array_free (view->coord_cache, TRUE), view->coord_cache = NULL;
}
mcview_hexedit_free_change_list (view);
/* FIXME: what about view->search_exp? */
if (view->converter != str_cnv_from_term)
if (!(view->converter == INVALID_CONV || view->converter != str_cnv_from_term)) {
str_close_conv (view->converter);
view->converter = str_cnv_from_term;
}
mcview_hexedit_free_change_list (view);
}
/* --------------------------------------------------------------------------------------------- */
void
mcview_set_codeset (mcview_t * view)
{
const char *cp_id = NULL;
view->utf8 = TRUE;
cp_id = get_codepage_id (source_codepage >= 0 ? source_codepage : display_codepage);
if (cp_id != NULL) {
GIConv conv;
conv = str_crt_conv_from (cp_id);
if (conv != INVALID_CONV) {
if (view->converter != str_cnv_from_term)
str_close_conv (view->converter);
view->converter = conv;
}
view->utf8 = (gboolean) str_isutf8 (cp_id);
}
}
/* --------------------------------------------------------------------------------------------- */
@ -208,23 +231,8 @@ void
mcview_select_encoding (mcview_t * view)
{
#ifdef HAVE_CHARSET
const char *cp_id = NULL;
if (do_select_codepage ()) {
cp_id = get_codepage_id (source_codepage >= 0 ?
source_codepage : display_codepage);
if (cp_id != NULL) {
GIConv conv;
conv = str_crt_conv_from (cp_id);
if (conv != INVALID_CONV) {
if (view->converter != str_cnv_from_term)
str_close_conv (view->converter);
view->converter = conv;
}
}
if (cp_id != NULL)
view->utf8 = (gboolean) str_isutf8 (cp_id);
mcview_set_codeset (view);
}
#endif

View File

@ -210,7 +210,9 @@ mcview_new (int y, int x, int cols, int lines, int is_panel)
view->cursor_row = 0;
view->change_list = NULL;
view->converter = str_cnv_from_term;
#ifdef HAVE_CHARSET
mcview_set_codeset (view);
#endif
/* {status,ruler,data}_area are left uninitialized */
view->dirty = 0;
@ -290,18 +292,8 @@ mcview_load (mcview_t * view, const char *command, const char *file, int start_l
char tmp[BUF_MEDIUM];
char *canon_fname;
struct stat st;
#ifdef HAVE_CHARSET
const char *cp_id = NULL;
#endif
gboolean retval = FALSE;
#ifdef HAVE_CHARSET
cp_id = get_codepage_id (source_codepage);
if (cp_id != NULL && str_isutf8 (cp_id) != 0)
view->utf8 = TRUE;
else
view->utf8 = FALSE;
#endif
gboolean retval = FALSE;
assert (view->bytes_per_line != 0);
mcview_done (view);
@ -374,24 +366,6 @@ mcview_load (mcview_t * view, const char *command, const char *file, int start_l
view->search_end = 0;
view->dpy_text_column = 0;
view->converter = str_cnv_from_term;
#ifdef HAVE_CHARSET
cp_id = get_codepage_id (source_codepage >= 0 ?
source_codepage : display_codepage);
if (cp_id != NULL) {
GIConv conv;
conv = str_crt_conv_from (cp_id);
if (conv != INVALID_CONV) {
if (view->converter != str_cnv_from_term)
str_close_conv (view->converter);
view->converter = conv;
}
}
if (cp_id != NULL)
view->utf8 = (gboolean) str_isutf8 (cp_id);
#endif
mcview_compute_areas (view);
assert (view->bytes_per_line != 0);
if (mcview_remember_file_position && view->filename != NULL && start_line == 0) {
@ -408,7 +382,6 @@ mcview_load (mcview_t * view, const char *command, const char *file, int start_l
view->hexedit_lownibble = FALSE;
view->hexview_in_text = FALSE;
view->change_list = NULL;
return retval;
}

View File

@ -68,7 +68,7 @@ mcview_display_text (mcview_t * view)
screen_dimen row, col;
off_t from;
int cw = 1;
int c;
unsigned int c;
gboolean read_res = TRUE;
struct hexedit_change_node *curr = view->change_list;
@ -136,7 +136,7 @@ mcview_display_text (mcview_t * view)
#ifdef HAVE_CHARSET
if (utf8_display) {
if (!view->utf8) {
c = convert_from_8bit_to_utf_c ((unsigned char) c, view->converter);
c = convert_from_8bit_to_utf_c (c, view->converter);
}
if (!g_unichar_isprint (c))
c = '.';