From fe5ed6852a51d6b6ea9c407f10019ecd427d4eb0 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sat, 4 Jun 2022 15:41:38 -0700 Subject: [PATCH] =?UTF-8?q?grep:=20don=E2=80=99t=20diagnose=20"grep=20'\-c?= =?UTF-8?q?'"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * src/dfasearch.c (GEAcompile): Skip past leading backslash of a pattern that begins with "\-". Inspired by a remark by Bruno Haible: https://lists.gnu.org/r/bug-gnulib/2022-06/msg00022.html --- src/dfasearch.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/dfasearch.c b/src/dfasearch.c index 8d832f0..730d0e4 100644 --- a/src/dfasearch.c +++ b/src/dfasearch.c @@ -203,6 +203,13 @@ GEAcompile (char *pattern, idx_t size, reg_syntax_t syntax_bits, dfasyntax (dc->dfa, &localeinfo, syntax_bits, dfaopts); bool bs_safe = !localeinfo.multibyte | localeinfo.using_utf8; + /* If a pattern starts with "\-" omit the backslash, to suppress a + stray-backslash warning if a script uses the non-POSIX + "grep '\-x'" to avoid treating "-x" as an option. */ + bool skip_bs = pattern[0] == '\\' && pattern[1] == '-'; + pattern += skip_bs; + size -= skip_bs; + /* For GNU regex, pass the patterns separately to detect errors like "[\nallo\n]\n", where the patterns are "[", "allo" and "]", and this should be a syntax error. The same for backref, where the -- 2.36.1