From 12eae8c8f346cb459a388259ca98faebdac47038 Mon Sep 17 00:00:00 2001 From: "Andrey A. Chernov" Date: Thu, 14 Jul 2016 09:07:25 +0000 Subject: [PATCH] 1) Eliminate possibility to call __*collate_range_cmp() with inclomplete locale (which cause core dump) by removing whole 'table' argument by which it passed. 2) Restore __collate_range_cmp() in __sccl(). 3) Collating [a-z] range in regcomp() only for single bytes locales (we can't do it now for other ones). In previous state only first 256 wchars are considered and all others are just silently dropped from the range. --- lib/libc/gen/fnmatch.c | 4 ++-- lib/libc/gen/glob.c | 4 ++-- lib/libc/locale/collate.h | 4 ++-- lib/libc/locale/collcmp.c | 13 ++++--------- lib/libc/regex/regcomp.c | 11 +++++------ lib/libc/stdio/vfscanf.c | 6 +++--- 6 files changed, 18 insertions(+), 24 deletions(-) diff --git a/lib/libc/gen/fnmatch.c b/lib/libc/gen/fnmatch.c index 8cdcaf1fbd37..65efe20727b6 100644 --- a/lib/libc/gen/fnmatch.c +++ b/lib/libc/gen/fnmatch.c @@ -296,8 +296,8 @@ rangematch(const char *pattern, wchar_t test, int flags, char **newp, if (table->__collate_load_error ? c <= test && test <= c2 : - __wcollate_range_cmp(table, c, test) <= 0 - && __wcollate_range_cmp(table, test, c2) <= 0 + __wcollate_range_cmp(c, test) <= 0 + && __wcollate_range_cmp(test, c2) <= 0 ) ok = 1; } else if (c == test) diff --git a/lib/libc/gen/glob.c b/lib/libc/gen/glob.c index 62aa8ad75f46..29d44868f5d8 100644 --- a/lib/libc/gen/glob.c +++ b/lib/libc/gen/glob.c @@ -832,8 +832,8 @@ match(Char *name, Char *pat, Char *patend) if ((*pat & M_MASK) == M_RNG) { if (table->__collate_load_error ? CHAR(c) <= CHAR(k) && CHAR(k) <= CHAR(pat[1]) : - __wcollate_range_cmp(table, CHAR(c), CHAR(k)) <= 0 - && __wcollate_range_cmp(table, CHAR(k), CHAR(pat[1])) <= 0 + __wcollate_range_cmp(CHAR(c), CHAR(k)) <= 0 + && __wcollate_range_cmp(CHAR(k), CHAR(pat[1])) <= 0 ) ok = 1; pat += 2; diff --git a/lib/libc/locale/collate.h b/lib/libc/locale/collate.h index 3253d9cb67d4..c8968c69f5a6 100644 --- a/lib/libc/locale/collate.h +++ b/lib/libc/locale/collate.h @@ -128,8 +128,8 @@ int __collate_load_tables(const char *); int __collate_equiv_value(locale_t, const wchar_t *, size_t); void _collate_lookup(struct xlocale_collate *,const wchar_t *, int *, int *, int, const int **); -int __collate_range_cmp(struct xlocale_collate *, char, char); -int __wcollate_range_cmp(struct xlocale_collate *, wchar_t, wchar_t); +int __collate_range_cmp(char, char); +int __wcollate_range_cmp(wchar_t, wchar_t); size_t _collate_wxfrm(struct xlocale_collate *, const wchar_t *, wchar_t *, size_t); size_t _collate_sxfrm(struct xlocale_collate *, const wchar_t *, char *, diff --git a/lib/libc/locale/collcmp.c b/lib/libc/locale/collcmp.c index b90c379cb193..ce71a714f42f 100644 --- a/lib/libc/locale/collcmp.c +++ b/lib/libc/locale/collcmp.c @@ -34,14 +34,13 @@ __FBSDID("$FreeBSD$"); #include #include -#include #include "collate.h" /* * Compare two characters using collate */ -int __collate_range_cmp(struct xlocale_collate *table, char c1, char c2) +int __collate_range_cmp(char c1, char c2) { char s1[2], s2[2]; @@ -49,12 +48,10 @@ int __collate_range_cmp(struct xlocale_collate *table, char c1, char c2) s1[1] = '\0'; s2[0] = c2; s2[1] = '\0'; - struct _xlocale l = {{0}}; - l.components[XLC_COLLATE] = (struct xlocale_component *)table; - return (strcoll_l(s1, s2, &l)); + return (strcoll(s1, s2)); } -int __wcollate_range_cmp(struct xlocale_collate *table, wchar_t c1, wchar_t c2) +int __wcollate_range_cmp(wchar_t c1, wchar_t c2) { wchar_t s1[2], s2[2]; @@ -62,7 +59,5 @@ int __wcollate_range_cmp(struct xlocale_collate *table, wchar_t c1, wchar_t c2) s1[1] = L'\0'; s2[0] = c2; s2[1] = L'\0'; - struct _xlocale l = {{0}}; - l.components[XLC_COLLATE] = (struct xlocale_component *)table; - return (wcscoll_l(s1, s2, &l)); + return (wcscoll(s1, s2)); } diff --git a/lib/libc/regex/regcomp.c b/lib/libc/regex/regcomp.c index 53332334fed7..b2b12555d25e 100644 --- a/lib/libc/regex/regcomp.c +++ b/lib/libc/regex/regcomp.c @@ -51,7 +51,6 @@ __FBSDID("$FreeBSD$"); #include #include #include -#include #include #include @@ -817,14 +816,14 @@ p_b_term(struct parse *p, cset *cs) if (start == finish) CHadd(p, cs, start); else { - if (table->__collate_load_error) { - (void)REQUIRE((uch)start <= (uch)finish, REG_ERANGE); + if (table->__collate_load_error || MB_CUR_MAX > 1) { + (void)REQUIRE(start <= finish, REG_ERANGE); CHaddrange(p, cs, start, finish); } else { - (void)REQUIRE(__wcollate_range_cmp(table, start, finish) <= 0, REG_ERANGE); + (void)REQUIRE(__wcollate_range_cmp(start, finish) <= 0, REG_ERANGE); for (i = 0; i <= UCHAR_MAX; i++) { - if ( __wcollate_range_cmp(table, start, i) <= 0 - && __wcollate_range_cmp(table, i, finish) <= 0 + if ( __wcollate_range_cmp(start, i) <= 0 + && __wcollate_range_cmp(i, finish) <= 0 ) CHadd(p, cs, i); } diff --git a/lib/libc/stdio/vfscanf.c b/lib/libc/stdio/vfscanf.c index e09a5b22eef9..e49a9e1ed134 100644 --- a/lib/libc/stdio/vfscanf.c +++ b/lib/libc/stdio/vfscanf.c @@ -873,7 +873,7 @@ __sccl(char *tab, const u_char *fmt) n = *fmt; if (n == ']' || (table->__collate_load_error ? n < c : - __wcollate_range_cmp(table, n, c) < 0 + __collate_range_cmp(n, c) < 0 ) ) { c = '-'; @@ -887,8 +887,8 @@ __sccl(char *tab, const u_char *fmt) } while (c < n); } else { for (i = 0; i < 256; i ++) - if (__wcollate_range_cmp(table, c, i) < 0 && - __wcollate_range_cmp(table, i, n) <= 0 + if (__collate_range_cmp(c, i) <= 0 && + __collate_range_cmp(i, n) <= 0 ) tab[i] = v; }