From: Bruno Haible Date: Wed, 13 Sep 2023 14:23:35 +0000 (+0200) Subject: mbfile: Fix major bug (regression 2023-07-04). X-Git-Tag: v1.0~785 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=8ea0b6ff1eee2643da52136c432eae69c5db87e7;p=gnulib.git mbfile: Fix major bug (regression 2023-07-04). Reported by Gleb Fotengauer-Malinovskiy in . * lib/mbfile.h (mbfile_multi_getc): If bytes != -1, -2, -3, add the previous mbf->bufcount to bytes. --- diff --git a/ChangeLog b/ChangeLog index ac57b7b5bc..10be2e2aad 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2023-09-13 Bruno Haible + + mbfile: Fix major bug (regression 2023-07-04). + Reported by Gleb Fotengauer-Malinovskiy in + . + * lib/mbfile.h (mbfile_multi_getc): If bytes != -1, -2, -3, add the + previous mbf->bufcount to bytes. + 2023-09-11 Pádraig Brady gnu-web-doc-update: fix updating of manual directory diff --git a/lib/mbfile.h b/lib/mbfile.h index dccdedb03c..ad61c19308 100644 --- a/lib/mbfile.h +++ b/lib/mbfile.h @@ -141,11 +141,11 @@ mbfile_multi_getc (struct mbchar *mbc, struct mbfile_multi *mbf) { /* Feed the bytes one by one into mbrtoc32. */ bytes = mbrtoc32 (&mbc->wc, &mbf->buf[mbf->bufcount], new_bufcount - mbf->bufcount, &mbf->state); - mbf->bufcount = new_bufcount; if (bytes == (size_t) -1) { /* An invalid multibyte sequence was encountered. */ + mbf->bufcount = new_bufcount; /* Return a single byte. */ bytes = 1; mbc->wc_valid = false; @@ -156,6 +156,7 @@ mbfile_multi_getc (struct mbchar *mbc, struct mbfile_multi *mbf) else if (bytes == (size_t) -2) { /* An incomplete multibyte character. */ + mbf->bufcount = new_bufcount; if (mbf->bufcount == MBCHAR_BUF_SIZE) { /* An overlong incomplete multibyte sequence was encountered. */ @@ -182,19 +183,27 @@ mbfile_multi_getc (struct mbchar *mbc, struct mbfile_multi *mbf) } else { - if (bytes == 0) + #if !GNULIB_MBRTOC32_REGULAR + if (bytes == (size_t) -3) { - /* A null 32-bit wide character was encountered. */ - bytes = 1; - assert (mbf->buf[0] == '\0'); - assert (mbc->wc == 0); + /* The previous multibyte sequence produced an additional 32-bit + wide character. */ + mbf->bufcount = new_bufcount; + bytes = 0; } - #if !GNULIB_MBRTOC32_REGULAR - else if (bytes == (size_t) -3) - /* The previous multibyte sequence produced an additional 32-bit - wide character. */ - bytes = 0; + else #endif + { + bytes = mbf->bufcount + bytes; + mbf->bufcount = new_bufcount; + if (bytes == 0) + { + /* A null 32-bit wide character was encountered. */ + bytes = 1; + assert (mbf->buf[0] == '\0'); + assert (mbc->wc == 0); + } + } mbc->wc_valid = true; break; }