locale: ensure dcngettext() preserves errno

Submitted by A. Wilcox on Jan. 28, 2019, 3:34 a.m.

Details

Message ID 20190128033457.10635-1-AWilcox@Wilcox-Tech.com
State New
Series "locale: ensure dcngettext() preserves errno"
Headers show

Commit Message

A. Wilcox Jan. 28, 2019, 3:34 a.m.
Some packages call gettext to format a message to be sent to strerror(3).
If the currently set user locale points to a non-existent .mo file,
open(2) via __map_file() in dcngettext() will set errno to ENOENT.

Simple test case is to compile coreutils with NLS enabled and then run
`ls /root` as an unprivileged user.

Before this commit:
ls: cannot open directory '/root': No such file or directory

After this commit:
ls: cannot open directory '/root': Permission denied

Originally reported by aranea on #Adelie.
---
 src/locale/dcngettext.c | 3 +++
 1 file changed, 3 insertions(+)

Patch hide | download patch | download mbox

diff --git a/src/locale/dcngettext.c b/src/locale/dcngettext.c
index 8b891d00..4c304393 100644
--- a/src/locale/dcngettext.c
+++ b/src/locale/dcngettext.c
@@ -122,6 +122,7 @@  char *dcngettext(const char *domainname, const char *msgid1, const char *msgid2,
 	const struct __locale_map *lm;
 	size_t domlen;
 	struct binding *q;
+	int old_errno = errno;
 
 	if ((unsigned)category >= LC_ALL) goto notrans;
 
@@ -138,6 +139,7 @@  char *dcngettext(const char *domainname, const char *msgid1, const char *msgid2,
 	lm = loc->cat[category];
 	if (!lm) {
 notrans:
+		errno = old_errno;
 		return (char *) ((n == 1) ? msgid1 : msgid2);
 	}
 
@@ -250,6 +252,7 @@  notrans:
 			trans += l+1;
 		}
 	}
+	errno = old_errno;
 	return (char *)trans;
 }
 

Comments

A. Wilcox Jan. 28, 2019, 3:38 a.m.
On 01/27/19 21:34, A. Wilcox wrote:
> Some packages call gettext to format a message to be sent to strerror(3).

Of course, I meant perror(3).  Derpus.

--arw