[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [bug-gnulib] Handling of invalid multibyte character sequences in fn
From: |
Paul Eggert |
Subject: |
Re: [bug-gnulib] Handling of invalid multibyte character sequences in fnmatch() |
Date: |
Tue, 07 Jun 2005 00:03:27 -0700 |
User-agent: |
Gnus/5.1006 (Gnus v5.10.6) Emacs/21.4 (gnu/linux) |
James Youngman <address@hidden> writes:
> Any ideas/suggestions?
Does the following untested patch fix things? It attempts to mimic
what Bash does.
*** fnmatch.c Fri May 13 23:03:58 2005
--- /tmp/fnmatch.c Tue Jun 7 00:02:03 2005
*************** fnmatch (const char *pattern, const char
*** 319,372 ****
wide characters. */
memset (&ps, '\0', sizeof (ps));
patsize = mbsrtowcs (NULL, &pattern, 0, &ps) + 1;
! if (__builtin_expect (patsize == 0, 0))
! /* Something wrong.
! XXX Do we have to set `errno' to something which mbsrtows hasn't
! already done? */
! return -1;
! assert (mbsinit (&ps));
! strsize = mbsrtowcs (NULL, &string, 0, &ps) + 1;
! if (__builtin_expect (strsize == 0, 0))
! /* Something wrong.
! XXX Do we have to set `errno' to something which mbsrtows hasn't
! already done? */
! return -1;
! assert (mbsinit (&ps));
! totsize = patsize + strsize;
! if (__builtin_expect (! (patsize <= totsize
! && totsize <= SIZE_MAX / sizeof (wchar_t)),
! 0))
{
! errno = ENOMEM;
! return -1;
! }
!
! /* Allocate room for the wide characters. */
! if (__builtin_expect (totsize < ALLOCA_LIMIT, 1))
! wpattern = (wchar_t *) alloca (totsize * sizeof (wchar_t));
! else
! {
! wpattern = malloc (totsize * sizeof (wchar_t));
! if (__builtin_expect (! wpattern, 0))
{
! errno = ENOMEM;
! return -1;
}
}
- wstring = wpattern + patsize;
-
- /* Convert the strings into wide characters. */
- mbsrtowcs (wpattern, &pattern, patsize, &ps);
- assert (mbsinit (&ps));
- mbsrtowcs (wstring, &string, strsize, &ps);
-
- res = internal_fnwmatch (wpattern, wstring, wstring + strsize - 1,
- flags & FNM_PERIOD, flags);
-
- if (__builtin_expect (! (totsize < ALLOCA_LIMIT), 0))
- free (wpattern);
- return res;
}
# endif /* HANDLE_MULTIBYTE */
return internal_fnmatch (pattern, string, string + strlen (string),
--- 319,369 ----
wide characters. */
memset (&ps, '\0', sizeof (ps));
patsize = mbsrtowcs (NULL, &pattern, 0, &ps) + 1;
! if (__builtin_expect (patsize != 0, 1))
{
! assert (mbsinit (&ps));
! strsize = mbsrtowcs (NULL, &string, 0, &ps) + 1;
! if (__builtin_expect (strsize != 0, 1))
{
! assert (mbsinit (&ps));
! totsize = patsize + strsize;
! if (__builtin_expect (! (patsize <= totsize
! && totsize <= SIZE_MAX / sizeof
(wchar_t)),
! 0))
! {
! errno = ENOMEM;
! return -1;
! }
!
! /* Allocate room for the wide characters. */
! if (__builtin_expect (totsize < ALLOCA_LIMIT, 1))
! wpattern = (wchar_t *) alloca (totsize * sizeof (wchar_t));
! else
! {
! wpattern = malloc (totsize * sizeof (wchar_t));
! if (__builtin_expect (! wpattern, 0))
! {
! errno = ENOMEM;
! return -1;
! }
! }
! wstring = wpattern + patsize;
!
! /* Convert the strings into wide characters. */
! mbsrtowcs (wpattern, &pattern, patsize, &ps);
! assert (mbsinit (&ps));
! mbsrtowcs (wstring, &string, strsize, &ps);
!
! res = internal_fnwmatch (wpattern, wstring, wstring + strsize - 1,
! flags & FNM_PERIOD, flags);
!
! if (__builtin_expect (! (totsize < ALLOCA_LIMIT), 0))
! free (wpattern);
! return res;
}
}
}
+
# endif /* HANDLE_MULTIBYTE */
return internal_fnmatch (pattern, string, string + strlen (string),