diff options
Diffstat (limited to 'posix')
-rw-r--r-- | posix/fnmatch.c | 79 | ||||
-rw-r--r-- | posix/regcomp.c | 2 | ||||
-rw-r--r-- | posix/regex_internal.c | 6 | ||||
-rw-r--r-- | posix/regex_internal.h | 9 | ||||
-rw-r--r-- | posix/regexec.c | 14 |
5 files changed, 89 insertions, 21 deletions
diff --git a/posix/fnmatch.c b/posix/fnmatch.c index e409ed7300..3fa7c322d1 100644 --- a/posix/fnmatch.c +++ b/posix/fnmatch.c @@ -327,31 +327,74 @@ fnmatch (pattern, string, flags) { mbstate_t ps; size_t n; + const char *p; wchar_t *wpattern; wchar_t *wstring; /* Convert the strings into wide characters. */ memset (&ps, '\0', sizeof (ps)); - n = mbsrtowcs (NULL, &pattern, 0, &ps); - if (__builtin_expect (n == (size_t) -1, 0)) - /* Something wrong. - XXX Do we have to set `errno' to something which mbsrtows hasn't - already done? */ - return -1; - wpattern = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t)); - assert (mbsinit (&ps)); - (void) mbsrtowcs (wpattern, &pattern, n + 1, &ps); + p = pattern; +#ifdef _LIBC + n = strnlen (pattern, 1024); +#else + n = strlen (pattern); +#endif + if (__builtin_expect (n < 1024, 1)) + { + wpattern = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t)); + n = mbsrtowcs (wpattern, &p, n + 1, &ps); + if (__builtin_expect (n == (size_t) -1, 0)) + /* Something wrong. + XXX Do we have to set `errno' to something which mbsrtows hasn't + already done? */ + return -1; + if (p) + memset (&ps, '\0', sizeof (ps)); + } + if (__builtin_expect (p != NULL, 0)) + { + n = mbsrtowcs (NULL, &pattern, 0, &ps); + if (__builtin_expect (n == (size_t) -1, 0)) + /* Something wrong. + XXX Do we have to set `errno' to something which mbsrtows hasn't + already done? */ + return -1; + wpattern = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t)); + assert (mbsinit (&ps)); + (void) mbsrtowcs (wpattern, &pattern, n + 1, &ps); + } assert (mbsinit (&ps)); - n = mbsrtowcs (NULL, &string, 0, &ps); - if (__builtin_expect (n == (size_t) -1, 0)) - /* Something wrong. - XXX Do we have to set `errno' to something which mbsrtows hasn't - already done? */ - return -1; - wstring = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t)); - assert (mbsinit (&ps)); - (void) mbsrtowcs (wstring, &string, n + 1, &ps); +#ifdef _LIBC + n = strnlen (string, 1024); +#else + n = strlen (string); +#endif + p = string; + if (__builtin_expect (n < 1024, 1)) + { + wstring = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t)); + n = mbsrtowcs (wstring, &p, n + 1, &ps); + if (__builtin_expect (n == (size_t) -1, 0)) + /* Something wrong. + XXX Do we have to set `errno' to something which mbsrtows hasn't + already done? */ + return -1; + if (p) + memset (&ps, '\0', sizeof (ps)); + } + if (__builtin_expect (p != NULL, 0)) + { + n = mbsrtowcs (NULL, &string, 0, &ps); + if (__builtin_expect (n == (size_t) -1, 0)) + /* Something wrong. + XXX Do we have to set `errno' to something which mbsrtows hasn't + already done? */ + return -1; + wstring = (wchar_t *) alloca ((n + 1) * sizeof (wchar_t)); + assert (mbsinit (&ps)); + (void) mbsrtowcs (wstring, &string, n + 1, &ps); + } return internal_fnwmatch (wpattern, wstring, wstring + n, flags & FNM_PERIOD, flags); diff --git a/posix/regcomp.c b/posix/regcomp.c index 72bf187b14..2100bb5420 100644 --- a/posix/regcomp.c +++ b/posix/regcomp.c @@ -779,6 +779,8 @@ re_compile_internal (preg, pattern, length, syntax) } preg->used = sizeof (re_dfa_t); + __libc_lock_init (dfa->lock); + err = init_dfa (dfa, length); if (BE (err != REG_NOERROR, 0)) { diff --git a/posix/regex_internal.c b/posix/regex_internal.c index 001b50b134..d590104fce 100644 --- a/posix/regex_internal.c +++ b/posix/regex_internal.c @@ -214,7 +214,8 @@ build_wcs_buffer (pstr) re_string_t *pstr; { #ifdef _LIBC - unsigned char buf[pstr->mb_cur_max]; + unsigned char buf[MB_LEN_MAX]; + assert (MB_LEN_MAX >= pstr->mb_cur_max); #else unsigned char buf[64]; #endif @@ -282,7 +283,8 @@ build_wcs_upper_buffer (pstr) mbstate_t prev_st; int src_idx, byte_idx, end_idx, mbclen, remain_len; #ifdef _LIBC - unsigned char buf[pstr->mb_cur_max]; + unsigned char buf[MB_LEN_MAX]; + assert (MB_LEN_MAX >= pstr->mb_cur_max); #else unsigned char buf[64]; #endif diff --git a/posix/regex_internal.h b/posix/regex_internal.h index 18865a7266..e48627766f 100644 --- a/posix/regex_internal.h +++ b/posix/regex_internal.h @@ -39,6 +39,14 @@ #if defined HAVE_WCTYPE_H || defined _LIBC # include <wctype.h> #endif /* HAVE_WCTYPE_H || _LIBC */ +#if defined _LIBC +# include <bits/libc-lock.h> +#else +# define __libc_lock_define(CLASS,NAME) +# define __libc_lock_init(NAME) do { } while (0) +# define __libc_lock_lock(NAME) do { } while (0) +# define __libc_lock_unlock(NAME) do { } while (0) +#endif /* In case that the system doesn't have isblank(). */ #if !defined _LIBC && !defined HAVE_ISBLANK && !defined isblank @@ -650,6 +658,7 @@ struct re_dfa_t #ifdef DEBUG char* re_str; #endif + __libc_lock_define (, lock) }; #ifndef RE_NO_INTERNAL_PROTOTYPES diff --git a/posix/regexec.c b/posix/regexec.c index 91b48dd4a2..021fcf4060 100644 --- a/posix/regexec.c +++ b/posix/regexec.c @@ -219,6 +219,7 @@ regexec (preg, string, nmatch, pmatch, eflags) { reg_errcode_t err; int start, length; + re_dfa_t *dfa = (re_dfa_t *)preg->buffer; if (eflags & ~(REG_NOTBOL | REG_NOTEOL | REG_STARTEND)) return REG_BADPAT; @@ -233,12 +234,15 @@ regexec (preg, string, nmatch, pmatch, eflags) start = 0; length = strlen (string); } + + __libc_lock_lock (dfa->lock); if (preg->no_sub) err = re_search_internal (preg, string, length, start, length - start, length, 0, NULL, eflags); else err = re_search_internal (preg, string, length, start, length - start, length, nmatch, pmatch, eflags); + __libc_lock_unlock (dfa->lock); return err != REG_NOERROR; } @@ -402,6 +406,7 @@ re_search_stub (bufp, string, length, start, range, stop, regs, ret_len) regmatch_t *pmatch; int nregs, rval; int eflags = 0; + re_dfa_t *dfa = (re_dfa_t *)bufp->buffer; /* Check for out-of-range. */ if (BE (start < 0 || start > length, 0)) @@ -411,6 +416,8 @@ re_search_stub (bufp, string, length, start, range, stop, regs, ret_len) else if (BE (start + range < 0, 0)) range = -start; + __libc_lock_lock (dfa->lock); + eflags |= (bufp->not_bol) ? REG_NOTBOL : 0; eflags |= (bufp->not_eol) ? REG_NOTEOL : 0; @@ -439,7 +446,10 @@ re_search_stub (bufp, string, length, start, range, stop, regs, ret_len) nregs = bufp->re_nsub + 1; pmatch = re_malloc (regmatch_t, nregs); if (BE (pmatch == NULL, 0)) - return -2; + { + rval = -2; + goto out; + } result = re_search_internal (bufp, string, length, start, range, stop, nregs, pmatch, eflags); @@ -469,6 +479,8 @@ re_search_stub (bufp, string, length, start, range, stop, regs, ret_len) rval = pmatch[0].rm_so; } re_free (pmatch); + out: + __libc_lock_unlock (dfa->lock); return rval; } |