# HG changeset patch # User ali # Date 1378503323 -3600 # Node ID 0df25c7f4ed782577aff598cfaac071d15bfb61e # Parent dd8f07bc9c0d121821ed9389c52b9efa56c5a650 Add some resilence against unexpected input diff -r dd8f07bc9c0d -r 0df25c7f4ed7 bookloupe/bookloupe.c --- a/bookloupe/bookloupe.c Thu Sep 05 22:40:10 2013 +0100 +++ b/bookloupe/bookloupe.c Fri Sep 06 22:35:23 2013 +0100 @@ -532,7 +532,7 @@ for (j=0;lines[j];j++) { lbytes=strlen(lines[j]); - while (lines[j][lbytes-1]=='\r') + while (lbytes>0 && lines[j][lbytes-1]=='\r') lines[j][--lbytes]='\0'; llen=g_utf8_strlen(lines[j],lbytes); linecnt++; @@ -606,12 +606,16 @@ } if (strchr(lines[j],'/')) results.fslashline++; - for (s=g_utf8_prev_char(lines[j]+lbytes); - s>lines[j] && g_utf8_get_char(s)<=CHAR_SPACE;s=g_utf8_prev_char(s)) - ; - if (s>g_utf8_next_char(lines[j]) && g_utf8_get_char(s)=='-' && - g_utf8_get_char(g_utf8_prev_char(s))!='-') - results.hyphens++; + if (lbytes>0) + { + for (s=g_utf8_prev_char(lines[j]+lbytes); + s>lines[j] && g_utf8_get_char(s)<=CHAR_SPACE; + s=g_utf8_prev_char(s)) + ; + if (s>g_utf8_next_char(lines[j]) && g_utf8_get_char(s)=='-' && + g_utf8_get_char(g_utf8_prev_char(s))!='-') + results.hyphens++; + } if (llen>LONGEST_PG_LINE) results.longline++; if (llen>WAY_TOO_LONG) @@ -3047,15 +3051,17 @@ g_unichar_isalpha(g_utf8_get_char(s)) || g_utf8_get_char(s)==',' || g_utf8_get_char(s)=='.';s=g_utf8_next_char(s)) g_string_append_unichar(word,g_utf8_get_char(s)); - for (t=g_utf8_next_char(word->str);*g_utf8_next_char(t); - t=g_utf8_next_char(t)) + if (word->len) { - c=g_utf8_get_char(t); - pc=g_utf8_get_char(g_utf8_prev_char(t)); - if ((c=='.' || c==',') && g_unichar_isdigit(pc)) + for (t=g_utf8_next_char(word->str);*t;t=g_utf8_next_char(t)) { - *ptr=s; - return g_string_free(word,FALSE); + c=g_utf8_get_char(t); + pc=g_utf8_get_char(g_utf8_prev_char(t)); + if ((c=='.' || c==',') && g_unichar_isdigit(pc)) + { + *ptr=s; + return g_string_free(word,FALSE); + } } } /* we didn't find a punctuated number - do the regular getword thing */