mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] [PATCH] fgetws: fix checking for fgetwc errors
@ 2022-02-09 18:47 Pinghao Wu
  2022-02-09 20:11 ` Rich Felker
  0 siblings, 1 reply; 2+ messages in thread
From: Pinghao Wu @ 2022-02-09 18:47 UTC (permalink / raw)
  To: musl; +Cc: Pinghao Wu

This corrects checking for fgetwc errors by arming dummy errno before
each invocation, and checking errors only if it returns WEOF.
---
 src/stdio/fgetws.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/src/stdio/fgetws.c b/src/stdio/fgetws.c
index b08b3049..edcd769b 100644
--- a/src/stdio/fgetws.c
+++ b/src/stdio/fgetws.c
@@ -12,18 +12,19 @@ wchar_t *fgetws(wchar_t *restrict s, int n, FILE *restrict f)
 
 	FLOCK(f);
 
-	/* Setup a dummy errno so we can detect EILSEQ. This is
-	 * the only way to catch encoding errors in the form of a
-	 * partial character just before EOF. */
-	errno = EAGAIN;
+	wint_t c;
 	for (; n; n--) {
-		wint_t c = __fgetwc_unlocked(f);
+		/* Setup a dummy errno so we can detect EILSEQ. This is
+		 * the only way to catch encoding errors in the form of a
+		 * partial character just before EOF. */
+		errno = EAGAIN;
+		c = __fgetwc_unlocked(f);
 		if (c == WEOF) break;
 		*p++ = c;
 		if (c == '\n') break;
 	}
 	*p = 0;
-	if (ferror(f) || errno==EILSEQ) p = s;
+	if (c == WEOF && (ferror(f) || errno==EILSEQ)) p = s;
 
 	FUNLOCK(f);
 
-- 
2.35.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [musl] [PATCH] fgetws: fix checking for fgetwc errors
  2022-02-09 18:47 [musl] [PATCH] fgetws: fix checking for fgetwc errors Pinghao Wu
@ 2022-02-09 20:11 ` Rich Felker
  0 siblings, 0 replies; 2+ messages in thread
From: Rich Felker @ 2022-02-09 20:11 UTC (permalink / raw)
  To: Pinghao Wu; +Cc: musl

On Thu, Feb 10, 2022 at 02:47:36AM +0800, Pinghao Wu wrote:
> This corrects checking for fgetwc errors by arming dummy errno before
> each invocation, and checking errors only if it returns WEOF.
> ---
>  src/stdio/fgetws.c | 13 +++++++------
>  1 file changed, 7 insertions(+), 6 deletions(-)
> 
> diff --git a/src/stdio/fgetws.c b/src/stdio/fgetws.c
> index b08b3049..edcd769b 100644
> --- a/src/stdio/fgetws.c
> +++ b/src/stdio/fgetws.c
> @@ -12,18 +12,19 @@ wchar_t *fgetws(wchar_t *restrict s, int n, FILE *restrict f)
>  
>  	FLOCK(f);
>  
> -	/* Setup a dummy errno so we can detect EILSEQ. This is
> -	 * the only way to catch encoding errors in the form of a
> -	 * partial character just before EOF. */
> -	errno = EAGAIN;
> +	wint_t c;
>  	for (; n; n--) {
> -		wint_t c = __fgetwc_unlocked(f);
> +		/* Setup a dummy errno so we can detect EILSEQ. This is
> +		 * the only way to catch encoding errors in the form of a
> +		 * partial character just before EOF. */
> +		errno = EAGAIN;
> +		c = __fgetwc_unlocked(f);
>  		if (c == WEOF) break;
>  		*p++ = c;
>  		if (c == '\n') break;
>  	}
>  	*p = 0;
> -	if (ferror(f) || errno==EILSEQ) p = s;
> +	if (c == WEOF && (ferror(f) || errno==EILSEQ)) p = s;
>  
>  	FUNLOCK(f);
>  
> -- 
> 2.35.1

Thanks. I didn't get from your previous patch for fgetwc that there
was a problem in fgetws that you were trying to fix. Now it makes a
lot more sense.

I think the above has at least one bug though: errno and c will both
be uninitialized if n was zero going into the loop (possible if the
initial value of n at function entry was 1). The error handling logic
should probably just be moved inside the loop instead of breaking out
to do it.

Rich

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-02-09 20:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-09 18:47 [musl] [PATCH] fgetws: fix checking for fgetwc errors Pinghao Wu
2022-02-09 20:11 ` Rich Felker

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).