Fix missing NUL-terminator in grep
Currently, grep read()s into a buffer and then uses the buffer as a string. Since there's no NUL-terminator, this can cause it to falsely identify line breaks and matches from leftover data on earlier lines and, if a line fills up the entire buffer, to read past the end of the buffer. Fix this by NUL-terminating any data returned by read(). Thanks to Keiichi Watanabe for the report.
This commit is contained in:
parent
3d2dedd427
commit
7443b9649a
1 changed files with 2 additions and 1 deletions
3
grep.c
3
grep.c
|
@ -14,8 +14,9 @@ grep(char *pattern, int fd)
|
|||
char *p, *q;
|
||||
|
||||
m = 0;
|
||||
while((n = read(fd, buf+m, sizeof(buf)-m)) > 0){
|
||||
while((n = read(fd, buf+m, sizeof(buf)-m-1)) > 0){
|
||||
m += n;
|
||||
buf[m] = '\0';
|
||||
p = buf;
|
||||
while((q = strchr(p, '\n')) != 0){
|
||||
*q = 0;
|
||||
|
|
Loading…
Reference in a new issue