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=-0.8 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H2 autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 3290 invoked from network); 19 Mar 2023 23:49:17 -0000 Received: from second.openwall.net (193.110.157.125) by inbox.vuxu.org with ESMTPUTF8; 19 Mar 2023 23:49:17 -0000 Received: (qmail 32298 invoked by uid 550); 19 Mar 2023 23:49:12 -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 32263 invoked from network); 19 Mar 2023 23:49:11 -0000 ARC-Seal: i=1; a=rsa-sha256; t=1679269739; cv=none; d=strato.com; s=strato-dkim-0002; b=bNpUK5z2a+CLAS8NG6UheMGGzGFJinvdvMZN9FOp82C4+ZEIrdYFSKFJ6H9mifJ8Es OPjP2sS8M570umYi1F5foIO1OQytI0nsd98FlhGM+/HxxMUiOvkfPyXCnlNAfHQ08plT WM3AIHDVtmPN312OXjSVMC2lqc+nBq/VH8nhs46jVtsC25DOfZwRUj6Rt0bivimn1J3I VN0hnj+HMr/c7b4p+f9oRKsrzVL5SDNvIvns5dC3KA6KwM6vF8ogZ92Zs7ald4frGjrT jOkzTv7MrBe3BTBIT8vTUWV+LECOvjrHj0WI9H6aWxcuLPAlV2GZL8Ak+JgpbiVELY89 F1fg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1679269739; s=strato-dkim-0002; d=strato.com; h=Message-ID:Date:Subject:To:From:Cc:Date:From:Subject:Sender; bh=Ggi8HrI5JYd+iVS5loM8rQ8VEEMO3blxA5VlVPOSbvE=; b=W8RbdHCc3J6mZaHyigYJD5aOuUjfDuEFKWxRZCLrKZ23fNX8r9whuDDTHb8CQEbCDN TRjLIN7o+7WS4RdZ6Cm0KMogMZSUDgskNMAS/k7esOQP2Unqr3NdOZgpfp4SkbZlOvUo mFBr2JFplXlleIPOKzAZYzzFFkXhE7iWC+9K4rwJDnKuIpQQT/uaAxUU4cCPdG26UxgP J0WC0lydwFYWP2ZSDTw6WDGL7RcjzDsfmI6rDnpDIQuzBotrW5HjtYzoa20z7comIKOq OhkpxXnfpLkot6KWxcyDYUojVrT4CpIISQR9pKa3P16BhOAAifRj+FmoP0h2uU9w6GQf RH2w== ARC-Authentication-Results: i=1; strato.com; arc=none; dkim=none X-RZG-CLASS-ID: mo00 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1679269739; s=strato-dkim-0002; d=clisp.org; h=Message-ID:Date:Subject:To:From:Cc:Date:From:Subject:Sender; bh=Ggi8HrI5JYd+iVS5loM8rQ8VEEMO3blxA5VlVPOSbvE=; b=H8l703T1RW61Yd/226AueaClCCaSALDV5XCfaV7et/vVIld4ugraqSHswUn9bo1kl4 nmA/gn6syxCXmE4rvc69FgUYcRMqF75pZPffPCSuhjiF0nH0nhtl9Imo3/jdLEaoTv5u 3WGZDLM+U922TSu8PLtFtR7KXknlEcxwrQp+dZJqtYRDZ8195y0qaNpliSQ5wzqQJpYc dqicc/K/Ihm0a7mCzAWkqEf3849pgF1nwsi7dIfiOd+gPLaN6RPhWALjVLTLq9GfP4LH LWnUPa8fTMt0ev2+MLX0RMfrHt0a7ugHPaZP8/yPypydnFJ34LaXfGg2wbGlOgWn2hwF VwLA== X-RZG-AUTH: ":Ln4Re0+Ic/6oZXR1YgKryK8brlshOcZlIWs+iCP5vnk6shH0WWb0LNUXbYz27Dm57lGjAq13ulkEiKPsDODB1WZ088XdO7Ef9X0=" From: Bruno Haible To: musl@lists.openwall.com Date: Mon, 20 Mar 2023 00:48:59 +0100 Message-ID: <4620016.0WQXIW03uk@nimes> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="UTF-8" Subject: [musl] swprintf: count returned by %n is wrong after conversion error Hi, On musl-1.2.3 I see this violation of the POSIX specification of swprintf [= 1]: =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D foo1.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D #include #include int main () { static const wchar_t input[] =3D { (wchar_t) 1702057263, 114, 0 }; wchar_t buf[12] =3D { 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF }; int count =3D -1; int ret =3D swprintf (buf, 12, L"%ls%n", input, &count); printf ("ret =3D %d, count =3D %d, buf[0] =3D 0x%x, buf[1] =3D 0x%x, buf[= 2] =3D 0x%x\n", ret, count, (unsigned int) buf[0], (unsigned int) buf[1], (unsigned int) buf[= 2]); return 0; } /* glibc: ret =3D 2, count =3D 2, buf[0] =3D 0x6573552f, buf[1] =3D 0x72,= buf[2] =3D 0x0 musl libc: ret =3D -1, count =3D 2, buf[0] =3D 0x0, buf[1] =3D 0xdeadbeef,= buf[2] =3D 0xdeadbeef =46reeBSD 13: ret =3D -1, count =3D -1, buf[0] =3D 0x0, buf[1] =3D 0xdeadbe= ef, buf[2] =3D 0xdeadbeef Solaris OI: ret =3D 2, count =3D 2, buf[0] =3D 0x6573552f, buf[1] =3D 0x72,= buf[2] =3D 0x0 */ =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D $ gcc -Wall foo1.c $ ./a.out ret =3D -1, count =3D 2, buf[0] =3D 0x0, buf[1] =3D 0xdeadbeef, buf[2] =3D = 0xdeadbeef The POSIX specification says: "The application shall ensure that the argument is a pointer to an integer into which is written the number of wide characters written to the output so far by this call to one of the fwprintf() functions." =46rom the values of buf[0], buf[1], buf[2] it can be seen that the number of wide characters written after the %ls directive is 0, not 2. Therefore the value of count should be 0 or =E2=80=94 if the processing of the format= string stops right after the %ls directive, like it does on FreeBSD 13 =E2=80=94 -= 1. It is OK for the %ls directive to fail, because of the invalid wide charact= ers in the input[] arrary. What is not OK is for the %n directive to report 2 written wide characters, when in fact 0 wide characters have been written. =46or comparison, in snprintf, this case is handled correctly: =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D foo2.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D #include #include #include int main () { static const wchar_t input[] =3D { (wchar_t) 1702057263, 114, 0 }; char buf[12] =3D { 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, = 0xDD, 0xDD, 0xDD }; int count =3D -1; int ret =3D snprintf (buf, 12, "%ls%n", input, &count); printf ("ret =3D %d, count =3D %d, buf[0] =3D 0x%x, buf[1] =3D 0x%x, buf[= 2] =3D 0x%x\n", ret, count, (unsigned char) buf[0], (unsigned char) buf[1], (unsigned char) b= uf[2]); return 0; } /* glibc: ret =3D -1, count =3D -1, buf[0] =3D 0x0, buf[1] =3D 0xdd, buf[= 2] =3D 0xdd musl libc: ret =3D -1, count =3D -1, buf[0] =3D 0x0, buf[1] =3D 0xdd, buf[= 2] =3D 0xdd =46reeBSD 13: ret =3D -1, count =3D -1, buf[0] =3D 0x0, buf[1] =3D 0xdd, bu= f[2] =3D 0xdd Solaris OI: ret =3D -1, count =3D -1, buf[0] =3D 0x0, buf[1] =3D 0xdd, buf[= 2] =3D 0xdd */ =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D $ gcc -Wall foo2.c $ ./a.out ret =3D -1, count =3D -1, buf[0] =3D 0x0, buf[1] =3D 0xdd, buf[2] =3D 0xdd Here, count remains unchanged, =3D -1. Bruno [1] https://pubs.opengroup.org/onlinepubs/9699919799/functions/swprintf.html