added NULL check before call g_utf8_find_prev_char, g_utf8_get_char_validated for more safety.

Signed-off-by: Ilia Maslakov <il.smind@gmail.com>
This commit is contained in:
Ilia Maslakov 2009-12-09 13:28:20 +03:00 committed by Slava Zanko
parent ec3c4c37c3
commit b90825fee4

View File

@ -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;