about summary refs log tree commit diff
path: root/lr.c
diff options
context:
space:
mode:
authorChristian Neukirchen <chneukirchen@gmail.com>2015-12-14 14:59:01 +0100
committerChristian Neukirchen <chneukirchen@gmail.com>2015-12-14 14:59:01 +0100
commit1f2e2e1537193bae202c5f9cb7bece7dca802bc4 (patch)
tree77ab9bfc45aa3db85a7462f9fc87897252abe542 /lr.c
parent2e3e51de6b370e03752b215b05599155bf117941 (diff)
downloadlr-1f2e2e1537193bae202c5f9cb7bece7dca802bc4.tar.gz
lr-1f2e2e1537193bae202c5f9cb7bece7dca802bc4.tar.xz
lr-1f2e2e1537193bae202c5f9cb7bece7dca802bc4.zip
Abort on invalid regex. Fixes #7.
Diffstat (limited to 'lr.c')
-rw-r--r--lr.c14
1 files changed, 12 insertions, 2 deletions
diff --git a/lr.c b/lr.c
index 26c5888..1c8b228 100644
--- a/lr.c
+++ b/lr.c
@@ -414,17 +414,27 @@ parse_strcmp()
 
 	char *s;
 	if (parse_string(&s)) {
+		int r = 0;
 		struct expr *e = mkexpr(op);
 		e->a.prop = prop;
 		if (op == EXPR_REGEX) {
 			e->b.regex = malloc(sizeof (regex_t));
-			regcomp(e->b.regex, s, REG_EXTENDED | REG_NOSUB);
+			r = regcomp(e->b.regex, s, REG_EXTENDED | REG_NOSUB);
 		} else if (op == EXPR_REGEXI) {
 			e->b.regex = malloc(sizeof (regex_t));
-			regcomp(e->b.regex, s, REG_EXTENDED | REG_NOSUB | REG_ICASE);
+			r = regcomp(e->b.regex, s, REG_EXTENDED | REG_NOSUB | REG_ICASE);
 		} else {
 			e->b.string = s;
 		}
+
+		if (r != 0) {
+			char msg[256];
+			regerror(r, e->b.regex, msg, sizeof msg);
+			fprintf(stderr, "%s: invalid regex '%s': %s\n",
+			    argv0, s, msg);
+			exit(2);
+		}
+
 		return e;
 	}