From: Rich Felker <dalias@libc.org>
To: Pinghao Wu <xdavidwuph@gmail.com>
Cc: musl@lists.openwall.com
Subject: Re: [musl] [PATCH] fgetwc: fix errno when character span aver buffer end
Date: Wed, 9 Feb 2022 13:24:55 -0500 [thread overview]
Message-ID: <20220209182455.GA7074@brightrain.aerifal.cx> (raw)
In-Reply-To: <20220209175557.25235-1-xdavidwuph@gmail.com>
On Thu, Feb 10, 2022 at 01:55:57AM +0800, Pinghao Wu wrote:
> 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.
errno is only meaningful after failure. A program which is checking it
when the last function called did not fail is incorrect and needs to
be fixed.
> ---
> 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
prev parent reply other threads:[~2022-02-09 18:25 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-09 17:55 Pinghao Wu
2022-02-09 18:24 ` Rich Felker [this message]
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=20220209182455.GA7074@brightrain.aerifal.cx \
--to=dalias@libc.org \
--cc=musl@lists.openwall.com \
--cc=xdavidwuph@gmail.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).