comparison rcg.c @ 21:ebfc52615768

fix segfault with long input lines
author paulo
date Tue, 29 Jul 2014 23:42:57 -0700
parents e618a3ff2027
children b18d74b37e9e
comparison
equal deleted inserted replaced
15:4d3b7392518a 16:e8e64275d424
99 { 99 {
100 fprintf(stderr, "realloc() failure \n"); 100 fprintf(stderr, "realloc() failure \n");
101 return EXIT_REALLOC_ERROR; 101 return EXIT_REALLOC_ERROR;
102 } 102 }
103 103
104 void *realloc_buffer(void *buffer, size_t buffer_len)
105 {
106 void *new_buffer = realloc(buffer, buffer_len);
107 if (!new_buffer)
108 exit(realloc_error());
109 return new_buffer;
110 }
111
104 Exit_code args_error() 112 Exit_code args_error()
105 { 113 {
106 char *usage = "\ 114 char *usage = "\
107 Usage: rcg [options] <PATTERN> \n\ 115 Usage: rcg [options] <PATTERN> \n\
108 \n\ 116 \n\
193 { 201 {
194 char *bgcolor = getBGColor(g_bg); 202 char *bgcolor = getBGColor(g_bg);
195 if (bgcolor) 203 if (bgcolor)
196 { 204 {
197 size_t l = strlen(clr) + 1 + strlen(bgcolor); 205 size_t l = strlen(clr) + 1 + strlen(bgcolor);
198 clr = realloc(clr, (l + 1)*sizeof(char)); 206 clr = realloc_buffer(clr, (l + 1)*sizeof(char));
199 if (!clr)
200 exit(realloc_error());
201 strcat(clr, ";"); 207 strcat(clr, ";");
202 strcat(clr, bgcolor); 208 strcat(clr, bgcolor);
203 } 209 }
204 else 210 else
205 exit(args_error()); 211 exit(args_error());
218 224
219 for (re_err = 0; re_err != REG_NOMATCH;) 225 for (re_err = 0; re_err != REG_NOMATCH;)
220 { 226 {
221 re_err = regexec(re, &buf[buf_pos], 1, rem, 0); 227 re_err = regexec(re, &buf[buf_pos], 1, rem, 0);
222 228
223 //fprintf(stderr, "%s", &buf[buf_pos]); //d// 20100405 PBA
224
225 if (out_len >= (BUFSIZE - buf_pos))
226 out_len = (BUFSIZE - buf_pos) - 1;
227
228 out = realloc(out, out_len*sizeof(char));
229 if (!out)
230 exit(realloc_error());
231
232 so = rem[0].rm_so; 229 so = rem[0].rm_so;
233 eo = rem[0].rm_eo; 230 eo = rem[0].rm_eo;
234 231
235 int match_len = eo - so; 232 int match_len = eo - so;
236 233
237 if (colorLine && match_len > 0) 234 if (colorLine && match_len > 0)
238 { 235 {
239 so = 0; 236 so = 0;
240 eo = out_len - 2; 237 eo = out_len - 1;
241 match_len = eo - so; 238 match_len = eo - so;
242 } 239 }
243
244 //fprintf(stderr, "%d %d \n", so, eo); //d// 20100327 PBA
245 240
246 if (re_err != REG_NOMATCH && so >= 0 && eo >= 0 && match_len > 0) 241 if (re_err != REG_NOMATCH && so >= 0 && eo >= 0 && match_len > 0)
247 { 242 {
248 size_t CLR_STRING_len = strlen(CLR_START) + strlen(clr) + strlen(CLR_END) + strlen(CLR_CLEAR); 243 size_t CLR_STRING_len = strlen(CLR_START) + strlen(clr) + strlen(CLR_END) + strlen(CLR_CLEAR);
249 244
250 out_len += CLR_STRING_len; 245 out_len += CLR_STRING_len;
251 out = realloc(out, out_len*sizeof(char)); 246 out = realloc_buffer(out, out_len*sizeof(char));
252 if (!out)
253 exit(realloc_error());
254 247
255 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); 248 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);
256 249
257 buf_pos += match_len + so; 250 buf_pos += match_len + so;
258 out_pos += CLR_STRING_len + match_len + so; 251 out_pos += CLR_STRING_len + match_len + so;
259 } 252 }
260 else 253 else
261 { 254 {
255 out = realloc_buffer(out, out_len*sizeof(char));
262 strncpy(&out[out_pos], &buf[buf_pos], out_len - out_pos); 256 strncpy(&out[out_pos], &buf[buf_pos], out_len - out_pos);
263 out[out_len - 1] = '\0'; 257 out[out_len - 1] = '\0';
264 re_err = REG_NOMATCH; 258 re_err = REG_NOMATCH;
265 } 259 }
266 } 260 }