From: Rich Felker Date: Tue, 9 Apr 2013 03:09:11 +0000 (-0400) Subject: mbrtowc: do not leave mbstate_t in permanent-fail state after EILSEQ X-Git-Tag: v0.9.10~3 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=23ab8c255543a7e0876c7e1858ef0d4bbd562729;p=oweals%2Fmusl.git mbrtowc: do not leave mbstate_t in permanent-fail state after EILSEQ the standard is clear that the old behavior is conforming: "In this case, [EILSEQ] shall be stored in errno and the conversion state is undefined." however, the specification of mbrtowc has one peculiarity when the source argument is a null pointer: in this case, it's required to behave as mbrtowc(NULL, "", 1, ps). no motivation is provided for this requirement, but the natural one that comes to mind is that the intent is to reset the mbstate_t object. for stateful encodings, such behavior is actually specified: "If the corresponding wide character is the null wide character, the resulting state described shall be the initial conversion state." but in the case of UTF-8 where the mbstate_t object contains a partially-decoded character rather than a shift state, a subsequent '\0' byte indicates that the previous partial character is incomplete and thus an illegal sequence. naturally, applications using their own mbstate_t object should clear it themselves after an error, but the standard presently provides no way to clear the builtin mbstate_t object used when the ps argument is a null pointer. I suspect this issue may be addressed in the future by specifying that a null source argument resets the state, as this seems to have been the intent all along. for what it's worth, this change also slightly reduces code size. --- diff --git a/src/multibyte/mbrtowc.c b/src/multibyte/mbrtowc.c index ec323859..db803661 100644 --- a/src/multibyte/mbrtowc.c +++ b/src/multibyte/mbrtowc.c @@ -51,7 +51,7 @@ loop: *(unsigned *)st = c; return -2; ilseq: - *(unsigned *)st = FAILSTATE; + *(unsigned *)st = 0; errno = EILSEQ; return -1; }