Add some resilence against unexpected input
authorali <ali@juiblex.co.uk>
Fri Sep 06 22:35:23 2013 +0100 (2013-09-06)
changeset 820df25c7f4ed7
parent 81 dd8f07bc9c0d
child 83 e0bdb7839f12
Add some resilence against unexpected input
bookloupe/bookloupe.c
     1.1 --- a/bookloupe/bookloupe.c	Thu Sep 05 22:40:10 2013 +0100
     1.2 +++ b/bookloupe/bookloupe.c	Fri Sep 06 22:35:23 2013 +0100
     1.3 @@ -532,7 +532,7 @@
     1.4      for (j=0;lines[j];j++)
     1.5      {
     1.6  	lbytes=strlen(lines[j]);
     1.7 -	while (lines[j][lbytes-1]=='\r')
     1.8 +	while (lbytes>0 && lines[j][lbytes-1]=='\r')
     1.9  	    lines[j][--lbytes]='\0';
    1.10  	llen=g_utf8_strlen(lines[j],lbytes);
    1.11  	linecnt++;
    1.12 @@ -606,12 +606,16 @@
    1.13  	}
    1.14  	if (strchr(lines[j],'/'))
    1.15  	    results.fslashline++;
    1.16 -	for (s=g_utf8_prev_char(lines[j]+lbytes);
    1.17 -	  s>lines[j] && g_utf8_get_char(s)<=CHAR_SPACE;s=g_utf8_prev_char(s))
    1.18 -	    ;
    1.19 -	if (s>g_utf8_next_char(lines[j]) && g_utf8_get_char(s)=='-' &&
    1.20 -	  g_utf8_get_char(g_utf8_prev_char(s))!='-')
    1.21 -	    results.hyphens++;
    1.22 +	if (lbytes>0)
    1.23 +	{
    1.24 +	    for (s=g_utf8_prev_char(lines[j]+lbytes);
    1.25 +	      s>lines[j] && g_utf8_get_char(s)<=CHAR_SPACE;
    1.26 +	      s=g_utf8_prev_char(s))
    1.27 +		;
    1.28 +	    if (s>g_utf8_next_char(lines[j]) && g_utf8_get_char(s)=='-' &&
    1.29 +	      g_utf8_get_char(g_utf8_prev_char(s))!='-')
    1.30 +		results.hyphens++;
    1.31 +	}
    1.32  	if (llen>LONGEST_PG_LINE)
    1.33  	    results.longline++;
    1.34  	if (llen>WAY_TOO_LONG)
    1.35 @@ -3047,15 +3051,17 @@
    1.36        g_unichar_isalpha(g_utf8_get_char(s)) ||
    1.37        g_utf8_get_char(s)==',' || g_utf8_get_char(s)=='.';s=g_utf8_next_char(s))
    1.38  	g_string_append_unichar(word,g_utf8_get_char(s));
    1.39 -    for (t=g_utf8_next_char(word->str);*g_utf8_next_char(t);
    1.40 -      t=g_utf8_next_char(t))
    1.41 +    if (word->len)
    1.42      {
    1.43 -	c=g_utf8_get_char(t);
    1.44 -	pc=g_utf8_get_char(g_utf8_prev_char(t));
    1.45 -	if ((c=='.' || c==',') && g_unichar_isdigit(pc))
    1.46 +	for (t=g_utf8_next_char(word->str);*t;t=g_utf8_next_char(t))
    1.47  	{
    1.48 -	    *ptr=s;
    1.49 -	    return g_string_free(word,FALSE);
    1.50 +	    c=g_utf8_get_char(t);
    1.51 +	    pc=g_utf8_get_char(g_utf8_prev_char(t));
    1.52 +	    if ((c=='.' || c==',') && g_unichar_isdigit(pc))
    1.53 +	    {
    1.54 +		*ptr=s;
    1.55 +		return g_string_free(word,FALSE);
    1.56 +	    }
    1.57  	}
    1.58      }
    1.59      /* we didn't find a punctuated number - do the regular getword thing */