Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date: Thu, 10 Feb 2022 01:55:57 +0800
From: Pinghao Wu <>
Cc: Pinghao Wu <>
Subject: [PATCH] fgetwc: fix errno when character span aver buffer end

If attempt on converting character from buffer failed, errno is set to
EILSEQ by mbtowc. As a result, if further byte-by-byte conversion
succeeds, fgetwc will return a valid wchar with a misleading EILSEQ as
errno. This fixes it by saving errno before the from buffer attempt, and
restore if it fails.

This also fixes fgetws which find the misleading EILSEQ and fails in
this case.
 src/stdio/fgetwc.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/src/stdio/fgetwc.c b/src/stdio/fgetwc.c
index aa10b818..9dad0505 100644
--- a/src/stdio/fgetwc.c
+++ b/src/stdio/fgetwc.c
@@ -8,6 +8,7 @@ static wint_t __fgetwc_unlocked_internal(FILE *f)
 	wchar_t wc;
 	int c;
 	size_t l;
+	int errno_save = errno;
 	/* Convert character from buffer if possible */
 	if (f->rpos != f->rend) {
@@ -16,6 +17,7 @@ static wint_t __fgetwc_unlocked_internal(FILE *f)
 			f->rpos += l + !l; /* l==0 means 1 byte, null */
 			return wc;
+		errno = errno_save;
 	/* Convert character byte-by-byte */

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.