diff mbox series

[2/2] posix: correctly detect invalid classes after match in fnmatch()

Message ID 20230523073732.6956-3-carenas@gmail.com
State New
Headers show
Series posix: fix fnmatch() inconsistency BZ#30483 | expand

Commit Message

Carlo Arenas May 23, 2023, 7:37 a.m. UTC
Always validate class names when parsing a [:class:] expression

* posix/fnmatch_loop.c (internal_fnmatch/internal_fnwmatch)
remove comment about an specification bug that is no longer relevant
adjust inequality to include 'z' as a valid character in class names
replace soft failure with a hard one when invalid names are used
---
 posix/fnmatch_loop.c | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)
diff mbox series

Patch

diff --git a/posix/fnmatch_loop.c b/posix/fnmatch_loop.c
index 4be2e20141..a6b5c69a95 100644
--- a/posix/fnmatch_loop.c
+++ b/posix/fnmatch_loop.c
@@ -866,29 +866,30 @@  FCT (const CHAR *pattern, const CHAR *string, const CHAR *string_end,
                   {
                     if (*p == L_('\0'))
                       return FNM_NOMATCH;
-                    /* XXX 1003.2d11 is unclear if this is right.  */
+
                     ++p;
                   }
                 else if (c == L_('[') && *p == L_(':'))
                   {
-                    int c1 = 0;
-                    const CHAR *startp = p;
+                    CHAR str[CHAR_CLASS_MAX_LENGTH + 1];
+                    size_t c1 = 0;
 
                     while (1)
                       {
                         c = *++p;
-                        if (++c1 == CHAR_CLASS_MAX_LENGTH)
-                          return FNM_NOMATCH;
-
-                        if (*p == L_(':') && p[1] == L_(']'))
+                        if (c == L_(':') && p[1] == L_(']'))
                           break;
 
-                        if (c < L_('a') || c >= L_('z'))
-                          {
-                            p = startp - 2;
-                            break;
-                          }
+                        if (c1 == CHAR_CLASS_MAX_LENGTH ||
+                            (c < L_('a') || c > L_('z')))
+                          return FNM_NOMATCH;
+
+                        str[c1++] = c;
                       }
+                    str[c1] = L_('\0');
+                    if (IS_CHAR_CLASS (str) == 0)
+                      return FNM_NOMATCH;
+
                     p += 2;
                   }
                 else if (c == L_('[') && *p == L_('='))