dash.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Harald van Dijk <harald@gigawatt.nl>
To: Christoph Anton Mitterer <calestyo@scientia.org>, dash@vger.kernel.org
Subject: Re: possible wrong behaviour with patterns using a quoted ^ at the start of a bracket expression
Date: Wed, 12 Jan 2022 17:20:54 +0000	[thread overview]
Message-ID: <e341e41f-8c32-b6e4-8be3-8f94fae2677c@gigawatt.nl> (raw)
In-Reply-To: <09fd70edcf08c75b4f9f2e158e3e6c0832d35d90.camel@scientia.org>

Hi,

On 12/01/2022 16:25, Christoph Anton Mitterer wrote:
> The results for the run-circumflex seem pretty odd.
> Apparently, the ^ is taken literally, but the other two are negated.

The ^ is not taken literally. The ^ in the pattern is wrongly taken as 
the negation operator, and the ^ in the argument is then reported as a 
match because it is neither . nor a.

This bug (you're right that it's a bug) is specific to builds that use 
fnmatch(). In dash itself, ^ is always assumed as a literal. In builds 
with --disable-fnmatch you get correct results. In builds with 
--enable-fnmatch, because dash assumes ^ is assumed as a literal, dash 
fails to escape it before passing it on to fnmatch(), and the system 
fnmatch() may choose differently from dash on how to deal with unquoted 
^s. What dash should do to get whatever behaviour the system fnmatch() 
chooses is leave unquoted ^s unquoted, and leave quoted ^s quoted. This 
can be achieved by

--- a/src/mksyntax.c
+++ b/src/mksyntax.c
@@ -178,14 +178,14 @@ main(int argc, char **argv)
         add("$", "CVAR");
         add("}", "CENDVAR");
         /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
-       add("!*?[=~:/-]", "CCTL");
+       add("!*?[^=~:/-]", "CCTL");
         print("dqsyntax");
         init();
         fputs("\n/* syntax table used when in single quotes */\n", cfile);
         add("\n", "CNL");
         add("'", "CENDQUOTE");
         /* ':/' for tilde expansion, '-' for [a\-x] pattern ranges */
-       add("!*?[=~:/-]\\", "CCTL");
+       add("!*?[^=~:/-]\\", "CCTL");
         print("sqsyntax");
         init();
         fputs("\n/* syntax table used when in arithmetic */\n", cfile);

However, whether this is the correct approach is a matter of opinion: 
dash could alternatively choose to always take ^ as a literal and always 
escape it before passing it on to fnmatch(), overriding whatever 
decision the libc people had taken.

Cheers,
Harald van Dijk

  reply	other threads:[~2022-01-12 17:27 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-12 16:25 Christoph Anton Mitterer
2022-01-12 17:20 ` Harald van Dijk [this message]
2022-01-12 17:47   ` Christoph Anton Mitterer
2022-01-12 18:17     ` Harald van Dijk
2022-01-12 18:21       ` Christoph Anton Mitterer
2022-01-18  6:13   ` [PATCH] expand: Always quote caret when using fnmatch Herbert Xu
2022-01-18  8:44     ` Harald van Dijk
2022-01-19  5:37       ` [v2 PATCH] " Herbert Xu
2022-02-20  7:15         ` Stephane Chazelas
2022-02-21 16:39           ` Eric Blake
2022-02-21 17:06             ` Harald van Dijk
2022-02-21 19:15               ` Stephane Chazelas
2022-01-18 14:29     ` [PATCH] " Christoph Anton Mitterer
2022-01-18 14:54       ` Chet Ramey
2022-01-18 22:33       ` Herbert Xu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e341e41f-8c32-b6e4-8be3-8f94fae2677c@gigawatt.nl \
    --to=harald@gigawatt.nl \
    --cc=calestyo@scientia.org \
    --cc=dash@vger.kernel.org \
    --subject='Re: possible wrong behaviour with patterns using a quoted ^ at the start of a bracket expression' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).