mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: musl@lists.openwall.com
Subject: [musl] swprintf produces garbage after a null wide character
Date: Wed, 22 Mar 2023 17:24:18 +0100	[thread overview]
Message-ID: <15375070.O9o76ZdvQC@nimes> (raw)

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




             reply	other threads:[~2023-03-22 16:25 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-22 16:24 Bruno Haible [this message]
2023-03-22 16:56 ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=15375070.O9o76ZdvQC@nimes \
    --to=bruno@clisp.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).