changeset 21:ebfc52615768

fix segfault with long input lines
author paulo
date Tue, 29 Jul 2014 23:42:57 -0700
parents dfafe7582bd6
children b18d74b37e9e
files rcg.c
diffstat 1 files changed, 12 insertions(+), 18 deletions(-) [+]
line diff
     1.1 --- a/rcg.c	Tue Mar 12 22:37:52 2013 -0600
     1.2 +++ b/rcg.c	Tue Jul 29 23:42:57 2014 -0700
     1.3 @@ -101,6 +101,14 @@
     1.4  	return EXIT_REALLOC_ERROR;
     1.5  }
     1.6  
     1.7 +void *realloc_buffer(void *buffer, size_t buffer_len)
     1.8 +{
     1.9 +	void *new_buffer = realloc(buffer, buffer_len);
    1.10 +	if (!new_buffer)
    1.11 +		exit(realloc_error());
    1.12 +	return new_buffer;
    1.13 +}	
    1.14 +
    1.15  Exit_code args_error()
    1.16  {
    1.17  	char *usage = "\
    1.18 @@ -195,9 +203,7 @@
    1.19  		if (bgcolor)
    1.20  		{
    1.21  			size_t l = strlen(clr) + 1 + strlen(bgcolor);
    1.22 -			clr = realloc(clr, (l + 1)*sizeof(char));
    1.23 -			if (!clr)
    1.24 -				exit(realloc_error());
    1.25 +			clr = realloc_buffer(clr, (l + 1)*sizeof(char));
    1.26  			strcat(clr, ";");
    1.27  			strcat(clr, bgcolor);
    1.28  		}
    1.29 @@ -220,15 +226,6 @@
    1.30  		{
    1.31  			re_err = regexec(re, &buf[buf_pos], 1, rem, 0);
    1.32  
    1.33 -			//fprintf(stderr, "%s", &buf[buf_pos]);		//d// 20100405 PBA
    1.34 -
    1.35 -			if (out_len >= (BUFSIZE - buf_pos))
    1.36 -				out_len = (BUFSIZE - buf_pos) - 1;
    1.37 -
    1.38 -			out = realloc(out, out_len*sizeof(char));
    1.39 -			if (!out)
    1.40 -				exit(realloc_error());
    1.41 -
    1.42  			so = rem[0].rm_so;
    1.43  			eo = rem[0].rm_eo;
    1.44  
    1.45 @@ -237,20 +234,16 @@
    1.46  			if (colorLine && match_len > 0)
    1.47  			{
    1.48  				so = 0;
    1.49 -				eo = out_len - 2;
    1.50 +				eo = out_len - 1;
    1.51  				match_len = eo - so;
    1.52  			}
    1.53  
    1.54 -			//fprintf(stderr, "%d %d \n", so, eo);		//d// 20100327 PBA
    1.55 -
    1.56  			if (re_err != REG_NOMATCH && so >= 0 && eo >= 0 && match_len > 0)
    1.57  			{
    1.58  				size_t CLR_STRING_len = strlen(CLR_START) + strlen(clr) + strlen(CLR_END) + strlen(CLR_CLEAR);
    1.59  
    1.60  				out_len += CLR_STRING_len;
    1.61 -				out = realloc(out, out_len*sizeof(char));
    1.62 -				if (!out)
    1.63 -					exit(realloc_error());
    1.64 +				out = realloc_buffer(out, out_len*sizeof(char));
    1.65  
    1.66  				snprintf(&out[out_pos], out_len, "%.*s%s%s%s%.*s%s", so, &buf[buf_pos], CLR_START, clr, CLR_END, match_len, &buf[buf_pos + so], CLR_CLEAR);
    1.67  
    1.68 @@ -259,6 +252,7 @@
    1.69  			}
    1.70  			else
    1.71  			{
    1.72 +				out = realloc_buffer(out, out_len*sizeof(char));
    1.73  				strncpy(&out[out_pos], &buf[buf_pos], out_len - out_pos);
    1.74  				out[out_len - 1] = '\0';
    1.75  				re_err = REG_NOMATCH;