grep: use len returned from getline and check fmemopen()

This commit is contained in:
Hiltjo Posthuma 2015-03-27 15:54:41 +01:00
parent 44298e54e6
commit fea0a34e13

27
grep.c
View File

@ -10,7 +10,7 @@
enum { Match = 0, NoMatch = 1, Error = 2 }; enum { Match = 0, NoMatch = 1, Error = 2 };
static void addpattern(const char *); static void addpattern(const char *, size_t);
static void addpatternfile(FILE *); static void addpatternfile(FILE *);
static int grep(FILE *, const char *); static int grep(FILE *, const char *);
@ -37,38 +37,41 @@ struct pattern {
static SLIST_HEAD(phead, pattern) phead; static SLIST_HEAD(phead, pattern) phead;
static void static void
addpattern(const char *pattern) addpattern(const char *pattern, size_t patlen)
{ {
struct pattern *pnode; struct pattern *pnode;
char *tmp; char *tmp;
int bol, eol; int bol, eol;
size_t len; size_t len;
if (!patlen)
return;
/* a null BRE/ERE matches every line */ /* a null BRE/ERE matches every line */
if (!Fflag) if (!Fflag)
if (pattern[0] == '\0') if (pattern[0] == '\0')
pattern = "."; pattern = ".";
if (!Fflag && xflag) { if (!Fflag && xflag) {
tmp = enmalloc(Error, strlen(pattern) + 3); tmp = enmalloc(Error, patlen + 3);
snprintf(tmp, strlen(pattern) + 3, "%s%s%s", snprintf(tmp, patlen + 3, "%s%s%s",
pattern[0] == '^' ? "" : "^", pattern[0] == '^' ? "" : "^",
pattern, pattern,
pattern[strlen(pattern) - 1] == '$' ? "" : "$"); pattern[patlen - 1] == '$' ? "" : "$");
} else if (!Fflag && wflag) { } else if (!Fflag && wflag) {
len = strlen(pattern) + 5 + (Eflag ? 2 : 4); len = patlen + 5 + (Eflag ? 2 : 4);
tmp = enmalloc(Error, len); tmp = enmalloc(Error, len);
bol = eol = 0; bol = eol = 0;
if (pattern[0] == '^') if (pattern[0] == '^')
bol = 1; bol = 1;
if (pattern[strlen(pattern) - 1] == '$') if (pattern[patlen - 1] == '$')
eol = 1; eol = 1;
snprintf(tmp, len, "%s\\<%s%.*s%s\\>%s", snprintf(tmp, len, "%s\\<%s%.*s%s\\>%s",
bol ? "^" : "", bol ? "^" : "",
Eflag ? "(" : "\\(", Eflag ? "(" : "\\(",
(int)strlen(pattern) - bol - eol, pattern + bol, (int)patlen - bol - eol, pattern + bol,
Eflag ? ")" : "\\)", Eflag ? ")" : "\\)",
eol ? "$" : ""); eol ? "$" : "");
} else { } else {
@ -90,7 +93,7 @@ addpatternfile(FILE *fp)
while ((len = getline(&buf, &size, fp)) > 0) { while ((len = getline(&buf, &size, fp)) > 0) {
if (len > 0 && buf[len - 1] == '\n') if (len > 0 && buf[len - 1] == '\n')
buf[len - 1] = '\0'; buf[len - 1] = '\0';
addpattern(buf); addpattern(buf, (size_t)len);
} }
if (ferror(fp)) if (ferror(fp))
enprintf(Error, "read error:"); enprintf(Error, "read error:");
@ -190,7 +193,8 @@ main(int argc, char *argv[])
break; break;
case 'e': case 'e':
arg = EARGF(usage()); arg = EARGF(usage());
fp = fmemopen(arg, strlen(arg) + 1, "r"); if(!(fp = fmemopen(arg, strlen(arg) + 1, "r")))
eprintf("fmemopen:");
addpatternfile(fp); addpatternfile(fp);
fclose(fp); fclose(fp);
eflag = 1; eflag = 1;
@ -239,7 +243,8 @@ main(int argc, char *argv[])
/* just add literal pattern to list */ /* just add literal pattern to list */
if (!eflag && !fflag) { if (!eflag && !fflag) {
fp = fmemopen(argv[0], strlen(argv[0]) + 1, "r"); if(!(fp = fmemopen(argv[0], strlen(argv[0]) + 1, "r")))
eprintf("fmemopen:");
addpatternfile(fp); addpatternfile(fp);
fclose(fp); fclose(fp);
argc--; argc--;