commit 3350a41744998de1b153f50d3e96be8bf7787e3d
parent 7ced46775c172ef29a9b91c4ab189838a01c02e5
Author: phoebos <ben@bvnf.space>
Date: Thu, 3 Feb 2022 00:40:17 +0000
grep: check for regex errors
Diffstat:
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/grep.c b/grep.c
@@ -15,7 +15,6 @@ enum {
int
match(const char *string, regex_t *re) {
int status = regexec(re, string, 0, NULL, 0);
-
if (status != 0)
return 0;
return 1;
@@ -106,7 +105,14 @@ main(int argc, char **argv) {
char *regexp = *++argv;
regex_t re;
- regcomp(&re, regexp, REGFLAGS);
+ int regerr = regcomp(&re, regexp, REGFLAGS);
+ if (regerr != 0) {
+ char errbuf[100] = {'\0'};
+ regerror(regerr, &re, errbuf, 100);
+ fprintf(stderr, "grep: bad regex: %s\n", errbuf);
+ regfree(&re);
+ return 1;
+ }
if (argc == 1) {
ret = grep(stdin, &re, flags);