From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 4848 invoked from network); 12 Jun 2023 20:27:43 -0000 Received: from second.openwall.net (193.110.157.125) by inbox.vuxu.org with ESMTPUTF8; 12 Jun 2023 20:27:43 -0000 Received: (qmail 20109 invoked by uid 550); 12 Jun 2023 20:27:40 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 20077 invoked from network); 12 Jun 2023 20:27:40 -0000 Date: Mon, 12 Jun 2023 16:27:28 -0400 From: Rich Felker To: Bruno Haible Cc: musl@lists.openwall.com Message-ID: <20230612202728.GB4163@brightrain.aerifal.cx> References: <2976941.0vhOF50zNu@nimes> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <2976941.0vhOF50zNu@nimes> User-Agent: Mutt/1.5.21 (2010-09-15) Subject: Re: [musl] swprintf %lc directive does not work for some wide characters On Mon, Jun 12, 2023 at 04:44:44PM +0200, Bruno Haible wrote: > According to ISO C 11 § 7.29.2.1, in the *wprintf family of functions, the > %lc directive works like this: > "[If an l length modifier is present,] the wint_t argument is converted to > wchar_t and written." > > Likewise in ISO C 17 § 7.29.2 and ISO C 23 § 7.31.2.1 and in POSIX:2018 > . > > In musl libc 1.2.4 (as part of Alpine Linux 3.18.0) this does not work for > some characters. > > How to reproduce: > =================================== foo.c =================================== > #include > #include > int main () > { > static wint_t L_invalid = (wchar_t) 0x76543210; > wchar_t buf[3]; > int ret = swprintf (buf, 3, L"%lc", L_invalid); > if (ret >= 0) > fprintf (stderr, "OK, %d characters\n", ret); > else > perror ("swprintf failed"); > } > ============================================================================= > $ gcc -Wall foo.c > $ ./a.out > > Expected output: > OK, 1 characters > > Actual output: > swprintf failed: Illegal byte sequence > > Per my reading of the specification, this is not a bug but is the expected behavior. In addition, all forms of fwprintf() shall fail if: [EILSEQ] A wide-character code that does not correspond to a valid character has been detected. Since the language "has been detected" is used here, this seems to allow for an implementation not to make it an error if the condition is not "detected". We make it an error because all wide stdio takes place through a byte-oriented buffer, and the conversions back and forth inherently "detect" the condition and have no way to pass the invalid wchar_t thru. There is no concept of directly writing wide characters. Note that the error here is happening not as part of the conversion specifier, but the output operation. Rich