Re: mbrtowc(3) state after an invalid sequence "undefined" or "unspecified"?

Alejandro Colomar <[email protected]> Thu, 28 May 2026 14:11:35 +0200
Newsgroups org.kernel.vger.linux-man
Message-ID <ahgw1XTxn7iJ6B_3@devuan>
Hi Kang-Che,

On 2026-05-21T23:08:20+0800, Kang-Che Sung wrote:
> Hi, Alejandro (or anyone else interested),
> 
> There's a discrepancy in the wording of the mbrtowc(3) function (and
> similarly, mbsrtowcs(3) function) between in POSIX and ISO C. It could be
> reported as an issue to POSIX (the Austin Group), and I am not sure if you
> can do that.
> 
> In ISO C (I checked in both C99 and C23, in particular the N3220 draft),
> there's a statement that if mbrtowc() returns a (size_t)(-1) as an encoding
> error occurs, "the conversion state is unspecified".
> 
> POSIX (see <
> https://pubs.opengroup.org/onlinepubs/9799919799/functions/mbrtowc.html>),
> for the same part it says "the conversion state is undefined".
> 
> This wording difference matters when the "unspecified behavior" and
> "undefined behavior" are technically different. An example is how the
> mbstate_t object can be reused after an invalid sequence is encountered.
> When the state is said to be "undefined" it's implied to be not usable
> again (unless it is reset, e.g., by an `mbrtowc(NULL, "", 1, ps)` call).
> When it's "unspecified" then implementations can allow the state to be
> reused for certain encodings (possible for UTF-8, for example).
> 
> This is something I discovered accidentally when researching the multibyte
> functions in the C standard library and how they work with an encoding like
> UTF-8.

Thanks!  I've opened this bug report:

<https://www.austingroupbugs.net/view.php?id=1982>


Have a lovely day!
Alex

-- 
<https://www.alejandro-colomar.es>
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCgAdFiEES7Jt9u9GbmlWADAi64mZXMKQwqkFAmoYMPEACgkQ64mZXMKQ
wqmrWhAAmrsmfmyyr+xR3eqvrSYFm8XCsWHquyrq9MlVI9+Q2wjcXyPJOO4X8cLf
tNxB1Lq9LRWU6Ub2tCslG8wFhsiz1h9QDL0NzrEePG4lLVfmj862D/L2dYa53Yk/
JpXAIvewFxbKGiv8Bj5O6ioNGRvT1Mk0AVbfM0TRTjd14ejFj2PLaQo+EaOXYER4
74JHYBiUJia52A1HRLFW0itVK7a8Q125/eJu8IzmNUJqXi19zrXXAmiJym66Yhk8
qaO+tNsf5F/7Ri/GN+2Hf95WkbydkgVFtAVAww7tNTAfnKRW0Cn1+6Lci2bQtMSD
/RIocWKBFOxZtFhJCxmZ5Bsw/lZmA1omkZcuFPpZHDaS+gHoWtMX7SD1bhpoCHaY
QsX1kpERV6BfjWZgGfX5byOeJCA+xnXgfiJN+TC5RuOfcqQ1ShDIloUqb0zAMoBR
qefGW3zwY9VZSZlRFPuSDQlu04/R0AQNPVGrJE8q3nG3cSvw/ddLVcCXxixUwbqP
GdrskCi1k1zBPf6wWamagkEEe04F5qhQVH6jz3lhfUxRC3HOEH8ZxpaXNt9tHmfJ
UBsZH4nj7+OoAbDRppq3pY6wZUibcbJgHeySRgLx/IrC64JRBcEJt3MThof6it6y
stgkEIhfeGrIY517rkpHn4EBMgampoFXAWtG5YOjkkh7XIe3mNI=
=Shh1
-----END PGP SIGNATURE-----