public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: Takashi Yano <takashi.yano@nifty.ne.jp>
To: newlib@sourceware.org
Subject: Re: fprintf() crashes on wide-oriented stream.
Date: Tue, 3 Oct 2023 17:30:17 +0900	[thread overview]
Message-ID: <20231003173017.e0108aee52bc579bde71abe0@nifty.ne.jp> (raw)
In-Reply-To: <20230926173013.30bcc1e76ca307935d4d5950@nifty.ne.jp>

Ping?

Is this Corinna's domain?

On Tue, 26 Sep 2023 17:30:13 +0900
Takashi Yano wrote:
> On Tue, 26 Sep 2023 12:41:47 +0900
> Takashi Yano wrote:
> > Hi,
> > 
> > I noticed that the following test case crashes at printf() with current
> > newlib.
> > 
> > 
> > #include <stdio.h>
> > #include <wchar.h>
> > #include <locale.h>
> > 
> > int main()
> > {
> > 	setlocale(LC_ALL, "C.UTF-8");
> > 	wprintf(L"%ls\n", L"aaaa"); /* or fwide(stdout, 1); */
> > 	printf("%ls\n", L"bbbb");  /* <--- crash here */
> > 	return 0;
> > }
> > 
> > 
> > I looked into this problem and found the cause.
> > 
> > A narrow char string which can be odd bytes in length is cast into
> > a wide char string which should be even bytes in length in __sprint_r/
> > __sfputs_r based on the __SWID flag. As a result, if the length is
> > odd bytes, the reading buffer runs over the buffer length, which causes
> > a crash. If the length is even bytes, crash does not happen, but garbage
> > is printed. This hapens if printf("%ls\r\n", L"bbbb"); is used instead.
> >                                       ^^
> > 
> > The same issue seemed to be reported ten years ago.
> > https://sourceware.org/pipermail/newlib/2013/010831.html
> > 
> > I have built a patch attached for this issue.
> > 
> > With this patch, __sfputs_r/__sprint_r is split into two versions, one
> > is for vfprintf which does not handle wide string, and the other (newly
> > introduced __sfputws_r/__swprin_r) is for vfwprintf which handles wide
> > string. Please note that fprintf gets working for wide orient stream
> > just like BSD libc, which behaves differently from GNU libc.
> > 
> > This patch also fixes nano-vfprintf.c as well as vfprintf.c/vfwprintf.c
> > in the same manner.
> 
> v2: Remove __sprint_r from nano-vfprintf.c which does not seem to be used
>     anymore.
> 
> -- 
> Takashi Yano <takashi.yano@nifty.ne.jp>


-- 
Takashi Yano <takashi.yano@nifty.ne.jp>

  reply	other threads:[~2023-10-03  8:30 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-26  3:41 Takashi Yano
2023-09-26  8:30 ` Takashi Yano
2023-10-03  8:30   ` Takashi Yano [this message]
2023-10-03 17:31     ` Jeff Johnston
2023-10-03 20:12       ` Takashi Yano
2023-10-03 18:07     ` Brian Inglis
2023-09-28  3:58 ` Takashi Yano
2023-09-28  8:42   ` Takashi Yano
2023-09-28 20:06     ` Brian Inglis
2023-10-04 20:16   ` Jeff Johnston
2023-10-05 10:18     ` Takashi Yano
2023-10-05 15:18       ` Takashi Yano
2023-10-05 18:20         ` Torbjorn SVENSSON
2023-11-07 13:24         ` Corinna Vinschen
2023-11-07 16:50           ` Brian Inglis
2023-11-07 19:12             ` Corinna Vinschen
2023-11-08 12:05           ` Takashi Yano
2023-11-02 18:53     ` Corinna Vinschen
2023-11-06 18:26       ` Brian Inglis

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=20231003173017.e0108aee52bc579bde71abe0@nifty.ne.jp \
    --to=takashi.yano@nifty.ne.jp \
    --cc=newlib@sourceware.org \
    /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.
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 read-only IMAP folder(s) and NNTP newsgroup(s).