From b90825fee42104e67f15477bf1b31e49c50db9dc Mon Sep 17 00:00:00 2001 From: Ilia Maslakov Date: Wed, 9 Dec 2009 13:28:20 +0300 Subject: [PATCH] added NULL check before call g_utf8_find_prev_char, g_utf8_get_char_validated for more safety. Signed-off-by: Ilia Maslakov --- edit/edit.c | 51 +++++++++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/edit/edit.c b/edit/edit.c index 7ba73451b..c52fb482c 100644 --- a/edit/edit.c +++ b/edit/edit.c @@ -140,7 +140,6 @@ int edit_get_byte (WEdit * edit, long byte_index) char *edit_get_byte_ptr (WEdit * edit, long byte_index) { unsigned long p; - if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) return NULL; @@ -156,13 +155,11 @@ char *edit_get_buf_ptr (WEdit * edit, long byte_index) { unsigned long p; - if (byte_index >= (edit->curs1 + edit->curs2) ) { - byte_index -= 1; - } + if (byte_index >= (edit->curs1 + edit->curs2)) + byte_index--; - if ( byte_index < 0 ) { + if (byte_index < 0) return NULL; - } if (byte_index >= edit->curs1) { p = edit->curs1 + edit->curs2 - 1; @@ -185,18 +182,23 @@ int edit_get_utf (WEdit * edit, long byte_index, int *char_width) return '\n'; } - str = edit_get_byte_ptr (edit, byte_index); + + if (str == NULL) { + *char_width = 0; + return 0; + } + res = g_utf8_get_char_validated (str, -1); - if ( res < 0 ) { + if (res < 0) { ch = *str; width = 0; } else { ch = res; /* Calculate UTF-8 char width */ - next_ch = g_utf8_next_char(str); - if ( next_ch ) { + next_ch = g_utf8_next_char (str); + if (next_ch) { width = next_ch - str; } else { ch = 0; @@ -215,36 +217,41 @@ int edit_get_prev_utf (WEdit * edit, long byte_index, int *char_width) gchar *next_ch = NULL; int width = 0; - if ( byte_index > 0 ) { + if (byte_index > 0) byte_index--; - } - ch = edit_get_utf (edit, byte_index, &width); - if ( width == 1 ) { - *char_width = width; - return ch; - } - - if ( byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0 ) { + if (byte_index >= (edit->curs1 + edit->curs2) || byte_index < 0) { *char_width = 0; return 0; } + ch = edit_get_utf (edit, byte_index, &width); + + if (width == 1) { + *char_width = width; + return ch; + } + str = edit_get_byte_ptr (edit, byte_index); buf = edit_get_buf_ptr (edit, byte_index); + if (str == NULL || buf == NULL) { + *char_width = 0; + return 0; + } /* get prev utf8 char */ - if ( str != buf ) + if (str != buf) str = g_utf8_find_prev_char (buf, str); res = g_utf8_get_char_validated (str, -1); - if ( res < 0 ) { + + if (res < 0) { ch = *str; width = 0; } else { ch = res; /* Calculate UTF-8 char width */ next_ch = g_utf8_next_char(str); - if ( next_ch ) { + if (next_ch) { width = next_ch - str; } else { ch = 0;