PERFORCE change 143466 for review

Gabor Kovesdan gabor at FreeBSD.org
Sat Jun 14 19:44:36 UTC 2008


http://perforce.freebsd.org/chv.cgi?CH=143466

Change 143466 by gabor at gabor_server on 2008/06/14 19:44:15

	- Add some checks after calling malloc
	- Improve --color, it almost works now

Affected files ...

.. //depot/projects/soc2008/gabor_textproc/grep/util.c#16 edit

Differences ...

==== //depot/projects/soc2008/gabor_textproc/grep/util.c#16 (text+ko) ====

@@ -228,19 +228,24 @@
 /* XXX: this color stuff does not work yet :( */
 				char	*tmp, *begin, *matched, *end;
 
-				begin = malloc(strlen(l->dat) - pmatch.rm_so + 2);
-				matched = malloc((pmatch.rm_eo - pmatch.rm_so + 1) * sizeof(char));
-				end = malloc(strlen(l->dat) - pmatch.rm_eo + 1);
+				if ((begin = malloc(strlen(l->dat) - pmatch.rm_so + 20)) == NULL)
+					errx(2, NULL);
+				if ((matched = malloc((pmatch.rm_eo - pmatch.rm_so + 20) * sizeof(char))) == NULL)
+					errx(2, NULL);
+				if ((end = malloc(strlen(l->dat) - pmatch.rm_eo + 20)) == NULL)
+					errx(2, NULL);
 
 				strlcpy(begin, l->dat, pmatch.rm_so + 1);
 				strlcpy(matched, &(l->dat[pmatch.rm_so]), pmatch.rm_eo - pmatch.rm_so + 1);
-				strlcpy(end, &(l->dat[pmatch.rm_eo]), strlen(l->dat) - strlen(begin) - strlen(matched) + 1);
+				strlcpy(end, &(l->dat[pmatch.rm_eo]), strlen(l->dat) - pmatch.rm_eo + 1);
+
 				asprintf(&tmp, "%s\33[%sm%s\33[00m%s", begin, color, matched, end);
 
 				free(begin);
 				free(matched);
 				free(end);
 
+				l->len += strlen(color) + 8;
 				l->dat = tmp;
 			}
 			if (r == 0 && oflag) {


More information about the p4-projects mailing list