Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Date: Wed, 9 Feb 2022 13:24:55 -0500
From: Rich Felker <dalias@...c.org>
To: Pinghao Wu <xdavidwuph@...il.com>
Cc: musl@...ts.openwall.com
Subject: Re: [PATCH] fgetwc: fix errno when character span aver buffer
 end

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

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.