mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: "Érico Nogueira" <ericonr@disroot.org>
Cc: Szabolcs Nagy <nsz@port70.net>,
	musl@lists.openwall.com, Alexander Vitiuk <suda@ukr.net>
Subject: Re: [musl] swprintf possible bug
Date: Sun, 1 Nov 2020 16:05:32 -0500	[thread overview]
Message-ID: <20201101210530.GP534@brightrain.aerifal.cx> (raw)
In-Reply-To: <C6S810FSG5JT.239HD3YWBGKJ3@mussels>

On Sun, Nov 01, 2020 at 05:48:43PM -0300, Érico Nogueira wrote:
> On Sun Nov 1, 2020 at 6:40 PM -03, Szabolcs Nagy wrote:
> > * Érico Nogueira <ericonr@disroot.org> [2020-11-01 17:17:49 -0300]:
> > > On Sun Nov 1, 2020 at 6:06 PM -03, Alexander Vitiuk wrote:
> > > > It seems, wsprintf() / wprintf() are not working in musl as expected, if
> > > > uses with cyrillic:
> > > >
> > > > C testcase:
> > > > #include <wchar.h>
> > > > int main() {
> > > > wprintf(L"[hello]\n");
> > > > wprintf(L"[Привет]\n");
> > > > return 0;
> > > > }
> > > > on x86_64-linux-gnu prints:
> > > > [hello]
> > > > [Privet]
> > > > and on x86_64-linux-musl prints: [hello]
> > > > [
> > > >
> > > > There are other cases described:
> > > > https://github.com/emscripten-core/emscripten/issues/11947
> > > 
> > > For what it's worth, if this is a bug, it would seem to be in how musl
> > > decides when to print characters (not the formatting functions
> > > themselves), since the below program doesn't print anything:
> > > 
> > > #include <wchar.h>
> > > #include <stdio.h>
> > > 
> > > int main() {
> > >   fputws(L"[Привет Василий]\n", stdout);
> > >   // I don't know if I'm accessing a wchar_t appropriately here
> > >   fputwc(L"[Привет Василий]\n"[3], stdout);
> > >   return 0;
> > > }
> > > 
> > > I tried tracing the execution from fputws, and not printing anything
> > > seems to be caused by the return value of wcsrtombs().
> >
> > these functions return an error code..
> >
> > in this case they must return -1 and set errno to EILSEQ,
> > since the selected multibyte encoding (LC_CTYPE=C) cannot
> > represent the printed wide characters.
> >
> > i think the musl behaviour is correct, you can try adding
> > setlocale(LC_CTYPE,"") at the start of main to make it work.
> 
> Thanks, that did fix it. For reference:
> 
> #include <wchar.h>
> #include <stdio.h>
> #include <locale.h>
> 
> int main() {
>   setlocale(LC_CTYPE, "");
>   fputws(L"[Привет Василий]\n", stdout);
>   fputwc(L"[Привет Василий]\n"[3], stdout);
>   return 0;
> }
> 
> I wonder what glibc's behavior is that it allows this; and how
> emscripten folks can work around the musl behavior as well.
> 
> Which environment variables could I set to control this, or is that not
> possible?

There are no environment variables that control the initial state of
the program before setlocale() is called. The environment variables
only affect the behavior of setlocale when it requests the
system's/user's default locale ("").

If you don't want the behavior to be dependent on locale, don't use
the wide functions. fputs("[Привет Василий]\n", stdout) would work
regardless of locale and would simply output the byte sequence. Of
course to be meaningful this depends on both the translation charset
and runtime charset being the same; they should of course both be
UTF-8 anytime in this millennium.

Rich

  reply	other threads:[~2020-11-01 21:05 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-01 19:06 Alexander Vitiuk
2020-11-01 20:17 ` Érico Nogueira
2020-11-01 20:40   ` Szabolcs Nagy
2020-11-01 20:48     ` Érico Nogueira
2020-11-01 21:05       ` Rich Felker [this message]
2020-11-01 20:48   ` Wolf

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=20201101210530.GP534@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=ericonr@disroot.org \
    --cc=musl@lists.openwall.com \
    --cc=nsz@port70.net \
    --cc=suda@ukr.net \
    /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).