mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] [PATCH] mbsrtowcs: fix wrong *src update in case of EILSEQ with non-initial mbstate_t
@ 2023-05-29 18:33 Alexey Izbyshev
  0 siblings, 0 replies; only message in thread
From: Alexey Izbyshev @ 2023-05-29 18:33 UTC (permalink / raw)
  To: musl

If mbsrtowcs is called with non-initial conversion state, it resumes
from the point where normally the first byte of a multibyte sequence has
already been consumed. If the multibyte sequence can't be completed, s is
decremented with the intention to point it to the start of that
sequence, but in this case it ends up equal to src-1.

Fix that by remembering the start of the last (sub)sequence that we
tried to convert instead of decrementing s. Do this only if ws is not
NULL, since we don't update *src otherwise.
---
 src/multibyte/mbsrtowcs.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/src/multibyte/mbsrtowcs.c b/src/multibyte/mbsrtowcs.c
index 9b2f2dfb..cbab539d 100644
--- a/src/multibyte/mbsrtowcs.c
+++ b/src/multibyte/mbsrtowcs.c
@@ -7,12 +7,13 @@
 
 size_t mbsrtowcs(wchar_t *restrict ws, const char **restrict src, size_t wn, mbstate_t *restrict st)
 {
-	const unsigned char *s = (const void *)*src;
+	const unsigned char *s = (const void *)*src, *s0;
 	size_t wn0 = wn;
 	unsigned c = 0;
 
 	if (st && (c = *(unsigned *)st)) {
 		if (ws) {
+			s0 = s;
 			*(unsigned *)st = 0;
 			goto resume;
 		} else {
@@ -55,13 +56,13 @@ size_t mbsrtowcs(wchar_t *restrict ws, const char **restrict src, size_t wn, mbs
 		if (*s-SA > SB-SA) break;
 		c = bittab[*s++-SA];
 resume0:
-		if (OOB(c,*s)) { s--; break; }
+		if (OOB(c,*s)) break;
 		s++;
 		if (c&(1U<<25)) {
-			if (*s-0x80u >= 0x40) { s-=2; break; }
+			if (*s-0x80u >= 0x40) break;
 			s++;
 			if (c&(1U<<19)) {
-				if (*s-0x80u >= 0x40) { s-=3; break; }
+				if (*s-0x80u >= 0x40) break;
 				s++;
 			}
 		}
@@ -89,16 +90,17 @@ resume0:
 			wn--;
 			continue;
 		}
+		s0 = s;
 		if (*s-SA > SB-SA) break;
 		c = bittab[*s++-SA];
 resume:
-		if (OOB(c,*s)) { s--; break; }
+		if (OOB(c,*s)) break;
 		c = (c<<6) | *s++-0x80;
 		if (c&(1U<<31)) {
-			if (*s-0x80u >= 0x40) { s-=2; break; }
+			if (*s-0x80u >= 0x40) break;
 			c = (c<<6) | *s++-0x80;
 			if (c&(1U<<31)) {
-				if (*s-0x80u >= 0x40) { s-=3; break; }
+				if (*s-0x80u >= 0x40) break;
 				c = (c<<6) | *s++-0x80;
 			}
 		}
@@ -115,6 +117,6 @@ resume:
 		return wn0-wn;
 	}
 	errno = EILSEQ;
-	if (ws) *src = (const void *)s;
+	if (ws) *src = (const void *)s0;
 	return -1;
 }
-- 
2.39.2


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-05-29 18:34 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-29 18:33 [musl] [PATCH] mbsrtowcs: fix wrong *src update in case of EILSEQ with non-initial mbstate_t Alexey Izbyshev

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