From 57e642365baa4f4353a06ffcfd816ca003c80f73 Mon Sep 17 00:00:00 2001 From: Mark Johnston Date: Thu, 9 Jan 2020 20:49:26 +0000 Subject: [PATCH] libc: Fix a few bugs in the xlocale collation code. - Fix checks for mmap() failures. [1] - Set the "map" and "maplen" fields of struct xlocale_collate so that the table destructor actually does something. - Free an already-mapped collation file before loading a new one into the global table. - Harmonize the prototype and definition of __collate_load_tables_l() by adding the "static" qualifier to the latter. PR: 243195 Reported by: cem [1] Reviewed by: cem, yuripv MFC after: 1 week Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D23109 --- lib/libc/locale/collate.c | 9 +++++++-- lib/libc/locale/rune.c | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/libc/locale/collate.c b/lib/libc/locale/collate.c index 15ce3b2f52e8..c67f7bcd646e 100644 --- a/lib/libc/locale/collate.c +++ b/lib/libc/locale/collate.c @@ -109,7 +109,7 @@ __collate_load_tables(const char *encoding) return (__collate_load_tables_l(encoding, &__xlocale_global_collate)); } -int +static int __collate_load_tables_l(const char *encoding, struct xlocale_collate *table) { int i, chains, z; @@ -147,7 +147,7 @@ __collate_load_tables_l(const char *encoding, struct xlocale_collate *table) } map = mmap(NULL, sbuf.st_size, PROT_READ, MAP_PRIVATE, fd, 0); (void) _close(fd); - if ((TMP = map) == NULL) { + if ((TMP = map) == MAP_FAILED) { return (_LDP_ERROR); } @@ -181,6 +181,11 @@ __collate_load_tables_l(const char *encoding, struct xlocale_collate *table) return (_LDP_ERROR); } + if (table->map && (table->maplen > 0)) { + (void) munmap(table->map, table->maplen); + } + table->map = map; + table->maplen = sbuf.st_size; table->info = info; table->char_pri_table = (void *)TMP; TMP += sizeof (collate_char_t) * (UCHAR_MAX + 1); diff --git a/lib/libc/locale/rune.c b/lib/libc/locale/rune.c index 6fed13d72d63..b7334636f654 100644 --- a/lib/libc/locale/rune.c +++ b/lib/libc/locale/rune.c @@ -94,7 +94,7 @@ _Read_RuneMagi(const char *fname) fdata = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0); (void) _close(fd); - if (fdata == NULL) { + if (fdata == MAP_FAILED) { errno = EINVAL; return (NULL); }