diff --git a/lib/libc/nls/catopen.3 b/lib/libc/nls/catopen.3 --- a/lib/libc/nls/catopen.3 +++ b/lib/libc/nls/catopen.3 @@ -130,16 +130,17 @@ is set to indicate the error. .Sh ERRORS .Bl -tag -width Er -.It Bq Er EINVAL -Argument -.Fa name -does not point to a valid message catalog, or catalog is corrupt. .It Bq Er ENAMETOOLONG An entire path to the message catalog exceeded 1024 characters. .It Bq Er ENOENT -The named message catalog does not exists, or the +Argument .Fa name -argument points to an empty string. +does not point to a valid message catalog name, +or it points to an empty string. +.It Bq Er ENOENT +The named message catalog does not exist. +.It Bq Er ENOENT +The named message catalog file is in wrong format. .It Bq Er ENOMEM Insufficient memory is available. .El diff --git a/lib/libc/nls/msgcat.c b/lib/libc/nls/msgcat.c --- a/lib/libc/nls/msgcat.c +++ b/lib/libc/nls/msgcat.c @@ -136,7 +136,7 @@ /* sanity checking */ if (name == NULL || *name == '\0') - NLRETERR(EINVAL); + NLRETERR(ENOENT); if (strchr(name, '/') != NULL) /* have a pathname */ @@ -390,7 +390,7 @@ struct catentry *np; void *data; char *copy_path, *copy_name, *copy_lang; - int fd; + int fd, saved_errno; /* path/name will never be NULL here */ @@ -414,9 +414,17 @@ } if (_fstat(fd, &st) != 0) { + saved_errno = errno; _close(fd); - SAVEFAIL(name, lang, EFTYPE); - NLRETERR(EFTYPE); + SAVEFAIL(name, lang, saved_errno); + NLRETERR(saved_errno); + } + + /* The file is too small to contain a _NLS_MAGIC. */ + if (st.st_size < sizeof(u_int32_t)) { + _close(fd); + SAVEFAIL(name, lang, ENOENT); + NLRETERR(ENOENT); } /* @@ -426,13 +434,13 @@ */ if (st.st_size > SIZE_T_MAX) { _close(fd); - SAVEFAIL(name, lang, EFBIG); - NLRETERR(EFBIG); + SAVEFAIL(name, lang, ENOENT); + NLRETERR(ENOENT); } - if ((data = mmap(0, (size_t)st.st_size, PROT_READ, - MAP_FILE|MAP_SHARED, fd, (off_t)0)) == MAP_FAILED) { - int saved_errno = errno; + data = mmap(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); + if (data == MAP_FAILED) { + saved_errno = errno; _close(fd); SAVEFAIL(name, lang, saved_errno); NLRETERR(saved_errno); @@ -442,8 +450,8 @@ if (ntohl((u_int32_t)((struct _nls_cat_hdr *)data)->__magic) != _NLS_MAGIC) { munmap(data, (size_t)st.st_size); - SAVEFAIL(name, lang, EFTYPE); - NLRETERR(EFTYPE); + SAVEFAIL(name, lang, ENOENT); + NLRETERR(ENOENT); } copy_name = strdup(name);