mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] swprintf produces garbage after a null wide character
@ 2023-03-22 16:24 Bruno Haible
  2023-03-22 16:56 ` Rich Felker
  0 siblings, 1 reply; 2+ messages in thread
From: Bruno Haible @ 2023-03-22 16:24 UTC (permalink / raw)
  To: musl

When swprintf is meant to produce output with a null wide character, in
musl libc 1.2.3, it produces a correct return value, but fills the entire
destination buffer with null wide characters. I.e. in this case, the number
of written wide characters is larger than the return value + 1.

How to reproduce:
==================================== foo.c ====================================
#include <stdio.h>
#include <wchar.h>

int main ()
{
  {
    wchar_t buf[5] = { 0xBEEF, 0xBEEF, 0xBEEF, 0xBEEF, 0xBEEF };
    int ret = swprintf (buf, 4, L"%c", '\0');
    printf ("ret = %d, buf[0] = 0x%x, buf[1] = 0x%x, buf[2] = 0x%x, buf[3] = 0x%x, buf[4] = 0x%x\n",
            ret,
            (unsigned int) buf[0], (unsigned int) buf[1],
            (unsigned int) buf[2], (unsigned int) buf[3],
            (unsigned int) buf[4]);
  }
  {
    wchar_t buf[5] = { 0xBEEF, 0xBEEF, 0xBEEF, 0xBEEF, 0xBEEF };
    int ret = swprintf (buf, 4, L"%cz", '\0');
    printf ("ret = %d, buf[0] = 0x%x, buf[1] = 0x%x, buf[2] = 0x%x, buf[3] = 0x%x, buf[4] = 0x%x\n",
            ret,
            (unsigned int) buf[0], (unsigned int) buf[1],
            (unsigned int) buf[2], (unsigned int) buf[3],
            (unsigned int) buf[4]);
  }
  return 0;
}
/*
glibc, Solaris 11, Cygwin:
  ret = 1, buf[0] = 0x0, buf[1] = 0x0, buf[2] = 0xbeef, buf[3] = 0xbeef, buf[4] = 0xbeef
  ret = 2, buf[0] = 0x0, buf[1] = 0x7a, buf[2] = 0x0, buf[3] = 0xbeef, buf[4] = 0xbeef
musl libc:
  ret = 1, buf[0] = 0x0, buf[1] = 0x0, buf[2] = 0x0, buf[3] = 0x0, buf[4] = 0xbeef
  ret = 2, buf[0] = 0x0, buf[1] = 0x0, buf[2] = 0x0, buf[3] = 0x0, buf[4] = 0xbeef
FreeBSD 13, NetBSD 9, OpenBSD 7.2, macOS 12.5, AIX 7.1:
  ret = 1, buf[0] = 0x0, buf[1] = 0xbeef, buf[2] = 0xbeef, buf[3] = 0xbeef, buf[4] = 0xbeef
  ret = 2, buf[0] = 0x0, buf[1] = 0xbeef, buf[2] = 0xbeef, buf[3] = 0xbeef, buf[4] = 0xbeef
*/
===============================================================================

$ gcc -Wall foo.c
$ ./a.out

Expected output:
ret = 1, buf[0] = 0x0, buf[1] = 0x0, buf[2] = 0xbeef, buf[3] = 0xbeef, buf[4] = 0xbeef
ret = 2, buf[0] = 0x0, buf[1] = 0x7a, buf[2] = 0x0, buf[3] = 0xbeef, buf[4] = 0xbeef

Actual output:
ret = 1, buf[0] = 0x0, buf[1] = 0x0, buf[2] = 0x0, buf[3] = 0x0, buf[4] = 0xbeef
ret = 2, buf[0] = 0x0, buf[1] = 0x0, buf[2] = 0x0, buf[3] = 0x0, buf[4] = 0xbeef

Bruno




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [musl] swprintf produces garbage after a null wide character
  2023-03-22 16:24 [musl] swprintf produces garbage after a null wide character Bruno Haible
@ 2023-03-22 16:56 ` Rich Felker
  0 siblings, 0 replies; 2+ messages in thread
From: Rich Felker @ 2023-03-22 16:56 UTC (permalink / raw)
  To: Bruno Haible; +Cc: musl

On Wed, Mar 22, 2023 at 05:24:18PM +0100, Bruno Haible wrote:
> When swprintf is meant to produce output with a null wide character, in
> musl libc 1.2.3, it produces a correct return value, but fills the entire
> destination buffer with null wide characters. I.e. in this case, the number
> of written wide characters is larger than the return value + 1.
> 
> How to reproduce:
> ==================================== foo.c ====================================
> #include <stdio.h>
> #include <wchar.h>
> 
> int main ()
> {
>   {
>     wchar_t buf[5] = { 0xBEEF, 0xBEEF, 0xBEEF, 0xBEEF, 0xBEEF };
>     int ret = swprintf (buf, 4, L"%c", '\0');
>     printf ("ret = %d, buf[0] = 0x%x, buf[1] = 0x%x, buf[2] = 0x%x, buf[3] = 0x%x, buf[4] = 0x%x\n",
>             ret,
>             (unsigned int) buf[0], (unsigned int) buf[1],
>             (unsigned int) buf[2], (unsigned int) buf[3],
>             (unsigned int) buf[4]);
>   }
>   {
>     wchar_t buf[5] = { 0xBEEF, 0xBEEF, 0xBEEF, 0xBEEF, 0xBEEF };
>     int ret = swprintf (buf, 4, L"%cz", '\0');
>     printf ("ret = %d, buf[0] = 0x%x, buf[1] = 0x%x, buf[2] = 0x%x, buf[3] = 0x%x, buf[4] = 0x%x\n",
>             ret,
>             (unsigned int) buf[0], (unsigned int) buf[1],
>             (unsigned int) buf[2], (unsigned int) buf[3],
>             (unsigned int) buf[4]);
>   }
>   return 0;
> }
> /*
> glibc, Solaris 11, Cygwin:
>   ret = 1, buf[0] = 0x0, buf[1] = 0x0, buf[2] = 0xbeef, buf[3] = 0xbeef, buf[4] = 0xbeef
>   ret = 2, buf[0] = 0x0, buf[1] = 0x7a, buf[2] = 0x0, buf[3] = 0xbeef, buf[4] = 0xbeef
> musl libc:
>   ret = 1, buf[0] = 0x0, buf[1] = 0x0, buf[2] = 0x0, buf[3] = 0x0, buf[4] = 0xbeef
>   ret = 2, buf[0] = 0x0, buf[1] = 0x0, buf[2] = 0x0, buf[3] = 0x0, buf[4] = 0xbeef
> FreeBSD 13, NetBSD 9, OpenBSD 7.2, macOS 12.5, AIX 7.1:
>   ret = 1, buf[0] = 0x0, buf[1] = 0xbeef, buf[2] = 0xbeef, buf[3] = 0xbeef, buf[4] = 0xbeef
>   ret = 2, buf[0] = 0x0, buf[1] = 0xbeef, buf[2] = 0xbeef, buf[3] = 0xbeef, buf[4] = 0xbeef
> */
> ===============================================================================
> 
> $ gcc -Wall foo.c
> $ ./a.out
> 
> Expected output:
> ret = 1, buf[0] = 0x0, buf[1] = 0x0, buf[2] = 0xbeef, buf[3] = 0xbeef, buf[4] = 0xbeef
> ret = 2, buf[0] = 0x0, buf[1] = 0x7a, buf[2] = 0x0, buf[3] = 0xbeef, buf[4] = 0xbeef
> 
> Actual output:
> ret = 1, buf[0] = 0x0, buf[1] = 0x0, buf[2] = 0x0, buf[3] = 0x0, buf[4] = 0xbeef
> ret = 2, buf[0] = 0x0, buf[1] = 0x0, buf[2] = 0x0, buf[3] = 0x0, buf[4] = 0xbeef

Thanks. This one is an easy one-line fix. Caught by the bad mbtowc
behavior of returning 0 rather than 1 when converting a nul character
(thereby not advancing past it in the buffer).

Rich

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-03-22 16:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-22 16:24 [musl] swprintf produces garbage after a null wide character Bruno Haible
2023-03-22 16:56 ` Rich Felker

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).