From 829d963a9dc84f229cc4c720da26537813592be7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Sun, 21 Jul 2019 10:41:30 +0700 Subject: [PATCH] More error checking on some SRE syntax When char-set, w/case, w/nocase, w/ascii or w/unicode is applied on a , only (cadr sre) is taken, the rest is ignored. Which is the right thing to do only if (null? (cddr sre)). If there are more arguments, error out instead of silently ignoring them. --- lib/chibi/regexp.scm | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/chibi/regexp.scm b/lib/chibi/regexp.scm index 1ad20982..0b12654c 100644 --- a/lib/chibi/regexp.scm +++ b/lib/chibi/regexp.scm @@ -676,7 +676,9 @@ (if (string? (car sre)) (maybe-ci (string->char-set (car sre))) (case (car sre) - ((char-set) (maybe-ci (string->char-set (cadr sre)))) + ((char-set) (if (null? (cddr sre)) + (maybe-ci (string->char-set (cadr sre))) + (error "(char-set) takes only one char-set" sre))) ((/ char-range) (->cs `(or ,@(map (lambda (x) @@ -689,10 +691,18 @@ ((~ complement) (char-set-complement (->cs `(or ,@(cdr sre))))) ((- difference) (char-set-difference (->cs (cadr sre)) (->cs `(or ,@(cddr sre))))) - ((w/case) (sre->char-set (cadr sre) (flag-clear flags ~ci?))) - ((w/nocase) (sre->char-set (cadr sre) (flag-join flags ~ci?))) - ((w/ascii) (sre->char-set (cadr sre) (flag-join flags ~ascii?))) - ((w/unicode) (sre->char-set (cadr sre) (flag-clear flags ~ascii?))) + ((w/case) (if (null? (cddr sre)) + (sre->char-set (cadr sre) (flag-clear flags ~ci?)) + (error "w/case takes only one char-set" sre))) + ((w/nocase) (if (null? (cddr sre)) + (sre->char-set (cadr sre) (flag-join flags ~ci?)) + (error "w/nocase takes only one char-set" sre))) + ((w/ascii) (if (null? (cddr sre)) + (sre->char-set (cadr sre) (flag-join flags ~ascii?)) + (error "w/ascii takes only one char-set" sre))) + ((w/unicode) (if (null? (cddr sre)) + (sre->char-set (cadr sre) (flag-clear flags ~ascii?)) + (error "w/unicode takes only one char-set" sre))) (else (error "invalid sre char-set" sre))))) (else (error "invalid sre char-set" sre)))))