Coverage Report

Created: 2026-05-30 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/proftpd/lib/pr_fnmatch.c
Line
Count
Source
1
/* Copyright (C) 1991,1992,1993,1996,1997,1998,1999,2000,2001,2002,2003,2007
2
  Free Software Foundation, Inc.
3
   This file is part of the GNU C Library.
4
5
   The GNU C Library is free software; you can redistribute it and/or
6
   modify it under the terms of the GNU Lesser General Public
7
   License as published by the Free Software Foundation; either
8
   version 2.1 of the License, or (at your option) any later version.
9
10
   The GNU C Library is distributed in the hope that it will be useful,
11
   but WITHOUT ANY WARRANTY; without even the implied warranty of
12
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
   Lesser General Public License for more details.
14
15
   You should have received a copy of the GNU Lesser General Public
16
   License along with the GNU C Library; if not, see
17
   <https://www.gnu.org/licenses/>.  */
18
19
/* This file comes from the GNU C Library and has been modified for use in
20
 * ProFTPD.
21
 *
22
 * Changes are released under the GNU Public License, version 2.
23
 * Copyright (C) 2000 MacGyver aka Habeeb J. Dihu <macgyver@tos.net>
24
 * Copyright (C) 2010-2026 The ProFTPD Project
25
 */
26
27
/* AIX requires this to be the first thing in the file.  */
28
#if defined _AIX && !defined __GNUC__
29
 #pragma alloca
30
#endif
31
32
#include <config.h>
33
34
/* Make alloca work the best possible way.  */
35
#ifdef __GNUC__
36
#define alloca __builtin_alloca
37
#else /* not __GNUC__ */
38
#if HAVE_ALLOCA_H
39
#include <alloca.h>
40
#else /* not __GNUC__ or HAVE_ALLOCA_H */
41
#ifndef _AIX /* Already did AIX, up at the top.  */
42
char *alloca ();
43
#endif /* not _AIX */
44
#endif /* not HAVE_ALLOCA_H */
45
#endif /* not __GNUC__ */
46
47
/* Necessary for platforms which don't use __alloca. */
48
0
#define __alloca alloca
49
50
/* Required to tell conf.h not to include the standard ProFTPD
51
 * header files
52
 */
53
54
#define __PROFTPD_SUPPORT_LIBRARY
55
56
#include "conf.h"
57
#include "base.h"
58
59
#if 0 /* Not used in ProFTPD */
60
/* Enable GNU extensions in fnmatch.h.  */
61
#ifndef _GNU_SOURCE
62
# define _GNU_SOURCE  1
63
#endif
64
65
#include <assert.h>
66
#include <errno.h>
67
#include <fnmatch.h>
68
#include <ctype.h>
69
70
#if HAVE_STRING_H || defined _LIBC
71
# include <string.h>
72
#else
73
# include <strings.h>
74
#endif
75
76
#if defined STDC_HEADERS || defined _LIBC
77
# include <stdlib.h>
78
#endif
79
80
/* For platform which support the ISO C amendement 1 functionality we
81
   support user defined character classes.  */
82
#if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
83
/* Solaris 2.5 has a bug: <wchar.h> must be included before <wctype.h>.  */
84
# include <wchar.h>
85
# include <wctype.h>
86
#endif
87
88
/* We need some of the locale data (the collation sequence information)
89
   but there is no interface to get this information in general.  Therefore
90
   we support a correct implementation only in glibc.  */
91
#ifdef _LIBC
92
# include "../locale/localeinfo.h"
93
# include "../locale/elem-hash.h"
94
# include "../locale/coll-lookup.h"
95
# include <shlib-compat.h>
96
97
# define CONCAT(a,b) __CONCAT(a,b)
98
# define mbsrtowcs __mbsrtowcs
99
# define fnmatch __fnmatch
100
extern int fnmatch (const char *pattern, const char *string, int flags);
101
#endif
102
#endif /* Not used in ProFTPD */
103
104
/* We often have to test for FNM_FILE_NAME and FNM_PERIOD being both set.  */
105
#define NO_LEADING_PERIOD(flags) \
106
0
  ((flags & (PR_FNM_FILE_NAME | PR_FNM_PERIOD)) == (PR_FNM_FILE_NAME | PR_FNM_PERIOD))
107
108
/* Comment out all this code if we are using the GNU C Library, and are not
109
   actually compiling the library itself.  This code is part of the GNU C
110
   Library, but also included in many other GNU distributions.  Compiling
111
   and linking in this code is a waste when using the GNU C library
112
   (especially if it is a shared library).  Rather than having every GNU
113
   program understand `configure --with-gnu-libc' and omit the object files,
114
   it is simpler to just do this in the source for each such file.  */
115
116
/* The comment above notwithstanding, we do want to use our own version to
117
 * ensure cross-platform compatibility...among other things.
118
 */
119
#undef _LIBC
120
#undef __GNU_LIBRARY__
121
122
/* Provide an implementation of the gcc-specific __builtin_expect macro,
123
 * for the non-gcc compilers out there.
124
 */
125
#ifndef __builtin_expect
126
0
# define __builtin_expect(e, n) (e)
127
#endif /* __builtin_expect */
128
129
#if defined _LIBC || !defined __GNU_LIBRARY__
130
131
132
# if defined STDC_HEADERS || !defined isascii
133
0
#  define ISASCII(c) 1
134
# else
135
#  define ISASCII(c) isascii(c)
136
# endif
137
138
# ifdef isblank
139
0
#  define ISBLANK(c) (ISASCII (c) && isblank (c))
140
# else
141
#  define ISBLANK(c) ((c) == ' ' || (c) == '\t')
142
# endif
143
# ifdef isgraph
144
0
#  define ISGRAPH(c) (ISASCII (c) && isgraph (c))
145
# else
146
#  define ISGRAPH(c) (ISASCII (c) && isprint (c) && !isspace (c))
147
# endif
148
149
0
# define ISPRINT(c) (ISASCII (c) && isprint (c))
150
0
# define ISDIGIT(c) (ISASCII (c) && isdigit (c))
151
0
# define ISALNUM(c) (ISASCII (c) && isalnum (c))
152
0
# define ISALPHA(c) (ISASCII (c) && isalpha (c))
153
0
# define ISCNTRL(c) (ISASCII (c) && iscntrl (c))
154
0
# define ISLOWER(c) (ISASCII (c) && islower (c))
155
0
# define ISPUNCT(c) (ISASCII (c) && ispunct (c))
156
0
# define ISSPACE(c) (ISASCII (c) && isspace (c))
157
0
# define ISUPPER(c) (ISASCII (c) && isupper (c))
158
0
# define ISXDIGIT(c) (ISASCII (c) && isxdigit (c))
159
160
0
# define STREQ(s1, s2) ((strcmp (s1, s2) == 0))
161
162
# define HANDLE_MULTIBYTE       0
163
164
# if defined _LIBC || (defined HAVE_WCTYPE_H && defined HAVE_WCHAR_H)
165
/* The GNU C library provides support for user-defined character classes
166
   and the functions from ISO C amendement 1.  */
167
#  ifdef CHARCLASS_NAME_MAX
168
#   define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
169
#  else
170
/* This shouldn't happen but some implementation might still have this
171
   problem.  Use a reasonable default value.  */
172
#   define CHAR_CLASS_MAX_LENGTH 256
173
#  endif
174
175
#  ifdef _LIBC
176
#   define IS_CHAR_CLASS(string) __wctype (string)
177
#  else
178
#   define IS_CHAR_CLASS(string) wctype (string)
179
#  endif
180
181
#  ifdef _LIBC
182
#   define ISWCTYPE(WC, WT) __iswctype (WC, WT)
183
#  else
184
#   define ISWCTYPE(WC, WT) iswctype (WC, WT)
185
#  endif
186
187
#  if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC
188
/* In this case we are implementing the multibyte character handling.  */
189
#   define HANDLE_MULTIBYTE 1
190
#  endif
191
192
# else
193
0
#  define CHAR_CLASS_MAX_LENGTH  6 /* Namely, `xdigit'.  */
194
195
#  define IS_CHAR_CLASS(string)                 \
196
   (STREQ (string, "alpha") || STREQ (string, "upper")            \
197
    || STREQ (string, "lower") || STREQ (string, "digit")         \
198
    || STREQ (string, "alnum") || STREQ (string, "xdigit")          \
199
    || STREQ (string, "space") || STREQ (string, "print")         \
200
    || STREQ (string, "punct") || STREQ (string, "graph")         \
201
    || STREQ (string, "cntrl") || STREQ (string, "blank"))
202
# endif
203
204
/* Avoid depending on library functions or files
205
   whose names are inconsistent.  */
206
207
# ifndef errno
208
extern int errno;
209
# endif
210
211
/* Global variable.  */
212
static int posixly_correct;
213
214
# if HANDLE_MULTIBYTE && !defined HAVE___STRCHRNUL && !defined _LIBC
215
static wchar_t *
216
__wcschrnul (const wchar_t *s, wint_t c)
217
{
218
  wchar_t *result = wcschr (s, c);
219
  if (result == NULL)
220
    result = wcschr (s, '\0');
221
  return result;
222
}
223
# endif
224
225
# ifndef internal_function
226
/* Inside GNU libc we mark some function in a special way.  In other
227
   environments simply ignore the marking.  */
228
#  define internal_function
229
# endif
230
231
/* Note that this evaluates C many times.  */
232
# ifdef _LIBC
233
#  define FOLD(c) ((flags & PR_FNM_CASEFOLD) ? tolower (c) : (c))
234
# else
235
0
#  define FOLD(c) ((flags & PR_FNM_CASEFOLD) && ISUPPER (c) ? tolower (c) : (c))
236
# endif
237
0
# define CHAR char
238
0
# define UCHAR  unsigned char
239
# define INT  int
240
0
# define FCT  internal_fnmatch
241
0
# define EXT  ext_match
242
0
# define END  end_pattern
243
# define STRUCT fnmatch_struct
244
0
# define L(CS)  CS
245
# ifdef _LIBC
246
#  define BTOWC(C)  __btowc (C)
247
# else
248
#  define BTOWC(C)  btowc (C)
249
# endif
250
0
# define STRLEN(S) strlen (S)
251
0
# define STRCAT(D, S) strcat (D, S)
252
# ifdef HAVE_MEMPCPY
253
0
#  define MEMPCPY(D, S, N) mempcpy (D, S, N)
254
# else
255
#  define MEMPCPY(D, S, N) __mempcpy (D, S, N)
256
# endif
257
0
# define MEMCHR(S, C, N) memchr (S, C, N)
258
# define STRCOLL(S1, S2) strcoll (S1, S2)
259
# include "pr_fnmatch_loop.c"
260
261
262
# if HANDLE_MULTIBYTE
263
/* Note that this evaluates C many times.  */
264
#  ifdef _LIBC
265
#   define FOLD(c) ((flags & PR_FNM_CASEFOLD) ? towlower (c) : (c))
266
#  else
267
#   define FOLD(c) ((flags & PR_FNM_CASEFOLD) && ISUPPER (c) ? towlower (c) : (c))
268
#  endif
269
#  define CHAR  wchar_t
270
#  define UCHAR wint_t
271
#  define INT wint_t
272
#  define FCT internal_fnwmatch
273
#  define EXT ext_wmatch
274
#  define END end_wpattern
275
#  define STRUCT fnwmatch_struct
276
#  define L(CS) L##CS
277
#  define BTOWC(C)  (C)
278
#  define STRLEN(S) __wcslen (S)
279
#  define STRCAT(D, S) __wcscat (D, S)
280
#  define MEMPCPY(D, S, N) __wmempcpy (D, S, N)
281
#  define MEMCHR(S, C, N) wmemchr (S, C, N)
282
#  define STRCOLL(S1, S2) wcscoll (S1, S2)
283
#  define WIDE_CHAR_VERSION 1
284
285
#  undef IS_CHAR_CLASS
286
/* We have to convert the wide character string in a multibyte string.  But
287
   we know that the character class names consist of alphanumeric characters
288
   from the portable character set, and since the wide character encoding
289
   for a member of the portable character set is the same code point as
290
   its single-byte encoding, we can use a simplified method to convert the
291
   string to a multibyte character string.  */
292
static wctype_t
293
is_char_class (const wchar_t *wcs)
294
{
295
  char s[CHAR_CLASS_MAX_LENGTH + 1];
296
  char *cp = s;
297
298
  do
299
    {
300
      /* Test for a printable character from the portable character set.  */
301
#  ifdef _LIBC
302
      if (*wcs < 0x20 || *wcs > 0x7e
303
    || *wcs == 0x24 || *wcs == 0x40 || *wcs == 0x60)
304
  return (wctype_t) 0;
305
#  else
306
      switch (*wcs)
307
  {
308
  case L' ': case L'!': case L'"': case L'#': case L'%':
309
  case L'&': case L'\'': case L'(': case L')': case L'*':
310
  case L'+': case L',': case L'-': case L'.': case L'/':
311
  case L'0': case L'1': case L'2': case L'3': case L'4':
312
  case L'5': case L'6': case L'7': case L'8': case L'9':
313
  case L':': case L';': case L'<': case L'=': case L'>':
314
  case L'?':
315
  case L'A': case L'B': case L'C': case L'D': case L'E':
316
  case L'F': case L'G': case L'H': case L'I': case L'J':
317
  case L'K': case L'L': case L'M': case L'N': case L'O':
318
  case L'P': case L'Q': case L'R': case L'S': case L'T':
319
  case L'U': case L'V': case L'W': case L'X': case L'Y':
320
  case L'Z':
321
  case L'[': case L'\\': case L']': case L'^': case L'_':
322
  case L'a': case L'b': case L'c': case L'd': case L'e':
323
  case L'f': case L'g': case L'h': case L'i': case L'j':
324
  case L'k': case L'l': case L'm': case L'n': case L'o':
325
  case L'p': case L'q': case L'r': case L's': case L't':
326
  case L'u': case L'v': case L'w': case L'x': case L'y':
327
  case L'z': case L'{': case L'|': case L'}': case L'~':
328
    break;
329
  default:
330
    return (wctype_t) 0;
331
  }
332
#  endif
333
334
      /* Avoid overrunning the buffer.  */
335
      if (cp == s + CHAR_CLASS_MAX_LENGTH)
336
  return (wctype_t) 0;
337
338
      *cp++ = (char) *wcs++;
339
    }
340
  while (*wcs != L'\0');
341
342
  *cp = '\0';
343
344
#  ifdef _LIBC
345
  return __wctype (s);
346
#  else
347
  return wctype (s);
348
#  endif
349
}
350
#  define IS_CHAR_CLASS(string) is_char_class (string)
351
352
#  include "pr_fnmatch_loop.c"
353
# endif
354
355
356
int
357
pr_fnmatch (const char *pattern, const char *string, int flags)
358
0
{
359
# if HANDLE_MULTIBYTE
360
  if (__builtin_expect (MB_CUR_MAX, 1) != 1)
361
    {
362
      mbstate_t ps;
363
      size_t n;
364
      const char *p;
365
      wchar_t *wpattern;
366
      wchar_t *wstring;
367
368
      /* Convert the strings into wide characters.  */
369
      memset (&ps, '\0', sizeof (ps));
370
      p = pattern;
371
#ifdef _LIBC
372
      n = strnlen (pattern, 1024);
373
#else
374
      n = strlen (pattern);
375
#endif
376
      if (__builtin_expect (n < 1024, 1))
377
  {
378
    wpattern = (wchar_t *) __alloca ((n + 1) * sizeof (wchar_t));
379
    n = mbsrtowcs (wpattern, &p, n + 1, &ps);
380
    if (__builtin_expect (n == (size_t) -1, 0))
381
      /* Something wrong.
382
         XXX Do we have to set `errno' to something which mbsrtows hasn't
383
         already done?  */
384
      return -1;
385
    if (p)
386
      {
387
        memset (&ps, '\0', sizeof (ps));
388
        goto prepare_wpattern;
389
      }
390
  }
391
      else
392
  {
393
  prepare_wpattern:
394
    n = mbsrtowcs (NULL, &pattern, 0, &ps);
395
    if (__builtin_expect (n == (size_t) -1, 0))
396
      /* Something wrong.
397
         XXX Do we have to set `errno' to something which mbsrtows hasn't
398
         already done?  */
399
      return -1;
400
    wpattern = (wchar_t *) __alloca ((n + 1) * sizeof (wchar_t));
401
    assert (mbsinit (&ps));
402
    (void) mbsrtowcs (wpattern, &pattern, n + 1, &ps);
403
  }
404
405
      assert (mbsinit (&ps));
406
#ifdef _LIBC
407
      n = strnlen (string, 1024);
408
#else
409
      n = strlen (string);
410
#endif
411
      p = string;
412
      if (__builtin_expect (n < 1024, 1))
413
  {
414
    wstring = (wchar_t *) __alloca ((n + 1) * sizeof (wchar_t));
415
    n = mbsrtowcs (wstring, &p, n + 1, &ps);
416
    if (__builtin_expect (n == (size_t) -1, 0))
417
      /* Something wrong.
418
         XXX Do we have to set `errno' to something which mbsrtows hasn't
419
         already done?  */
420
      return -1;
421
    if (p)
422
      {
423
        memset (&ps, '\0', sizeof (ps));
424
        goto prepare_wstring;
425
      }
426
  }
427
      else
428
  {
429
  prepare_wstring:
430
    n = mbsrtowcs (NULL, &string, 0, &ps);
431
    if (__builtin_expect (n == (size_t) -1, 0))
432
      /* Something wrong.
433
         XXX Do we have to set `errno' to something which mbsrtows hasn't
434
         already done?  */
435
      return -1;
436
    wstring = (wchar_t *) __alloca ((n + 1) * sizeof (wchar_t));
437
    assert (mbsinit (&ps));
438
    (void) mbsrtowcs (wstring, &string, n + 1, &ps);
439
  }
440
441
      return internal_fnwmatch (wpattern, wstring, wstring + n,
442
        flags & PR_FNM_PERIOD, flags, NULL);
443
    }
444
# endif  /* mbstate_t and mbsrtowcs or _LIBC.  */
445
446
0
  return internal_fnmatch (pattern, string, string + strlen (string),
447
0
         flags & PR_FNM_PERIOD, flags, NULL);
448
0
}
449
450
#endif  /* _LIBC or not __GNU_LIBRARY__.  */