From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 23223 invoked from network); 9 Feb 2022 18:25:11 -0000 Received: from mother.openwall.net (195.42.179.200) by inbox.vuxu.org with ESMTPUTF8; 9 Feb 2022 18:25:11 -0000 Received: (qmail 29774 invoked by uid 550); 9 Feb 2022 18:25:09 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 29742 invoked from network); 9 Feb 2022 18:25:08 -0000 Date: Wed, 9 Feb 2022 13:24:55 -0500 From: Rich Felker To: Pinghao Wu Cc: musl@lists.openwall.com Message-ID: <20220209182455.GA7074@brightrain.aerifal.cx> References: <20220209175557.25235-1-xdavidwuph@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220209175557.25235-1-xdavidwuph@gmail.com> User-Agent: Mutt/1.5.21 (2010-09-15) Subject: Re: [musl] [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