|
Message-ID: <20201101204832.zk7xmsdazrw7tz52@wolfsden.cz>
Date: Sun, 1 Nov 2020 21:48:32 +0100
From: Wolf <wolf@...fsden.cz>
To: musl@...ts.openwall.com
Subject: Re: swprintf possible bug
On 2020-11-01 17:17:49 -0300, Érico Nogueira wrote:
> 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().
That seems to be on the right track, since when you actually check the
return code from fputws:
#include <wchar.h>
#include <stdio.h>
int main(void) {
if (fputws(L"[Привет Василий]\n", stdout) == -1) {
perror("fputws");
}
}
you get this:
# ./a
fputws: Illegal byte sequence
I think it is caused by C (or POSIX) locale being default on entry to
main, so you need to actually activate the system locale by calling
setlocale(LC_ALL, "").
Indeed, if you modify the program to:
#include <wchar.h>
#include <stdio.h>
#include <locale.h>
int main(void) {
setlocale(LC_ALL, "");
if (fputws(L"[Привет Василий]\n", stdout) == -1) {
perror("fputws");
}
}
It starts to work:
# ./a
[Привет Василий]
W.
--
There are only two hard things in Computer Science:
cache invalidation, naming things and off-by-one errors.
Download attachment "signature.asc" of type "application/pgp-signature" (834 bytes)
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.