Re: [v2 PATCH] expand: Always quote caret when using fnmatch

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 21/02/2022 16:39, Eric Blake wrote:
On Sun, Feb 20, 2022 at 07:15:44AM +0000, Stephane Chazelas wrote:
2022-01-19 16:37:54 +1100, Herbert Xu:
[...]
This patch forces ^ to be a literal when we use fnmatch.
[...]
The "fix" also seems incomplete and causes at least this
regression:

$ ltrace -e fnmatch ./bin/dash -c 'case "\\" in ($1) echo match; esac' sh '[\^]'
dash->fnmatch("[\\\\^]", "\\", 0)                              = 0
match

See how that added \ caused it to be included in the set. Compare with:

+++ exited (status 0) +++
$ ltrace -e fnmatch ./bin/dash -c 'case "\\" in ($1) echo match; esac' sh '[\!]'
dash->fnmatch("[\\!]", "\\", 0)                                = 1
+++ exited (status 0) +++
$ ltrace -e fnmatch ./bin/dash -c 'case "\\" in ($1) echo match; esac' sh '[\x]'
dash->fnmatch("[\\x]", "\\", 0)                                = 1
+++ exited (status 0) +++

If you want to escape ^ to always be treated as a literal when calling
fnmatch(), you have to spell it something like [[=^=]...], not [\^...]
(that is, since \^ inside [] does NOT treat the \ as an escape
character, but as yet another literal character as part of the brakcet
group, you instead have to resort to calling out the literal character
^ via alternative spellings such as by a collation equivalent).

This is correct for regular expressions, unspecified for shell patterns in general after bug 1234 was resolved, and always incorrect for fnmatch() after bug 1190 was resolved. In fnmatch(), backslash has to work even inside bracket expressions. Whether it had to may not previously have been clear in the standard, but it has been made clear now and that is also how it works in glibc. That part of the patch is not a problem.

Cheers,
Harald van Dijk



[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux