mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Pinghao Wu <xdavidwuph@gmail.com>
To: musl@lists.openwall.com
Cc: Pinghao Wu <xdavidwuph@gmail.com>
Subject: [musl] [PATCH] fgetwc: fix errno when character span aver buffer end
Date: Thu, 10 Feb 2022 01:55:57 +0800	[thread overview]
Message-ID: <20220209175557.25235-1-xdavidwuph@gmail.com> (raw)

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 */
-- 
2.35.1


             reply	other threads:[~2022-02-09 17:57 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-09 17:55 Pinghao Wu [this message]
2022-02-09 18:24 ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220209175557.25235-1-xdavidwuph@gmail.com \
    --to=xdavidwuph@gmail.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).