mailing list of musl libc
 help / color / mirror / code / Atom feed
From: =?gb18030?B?ODQ3NTY3MTYx?= <847567161@qq.com>
To: =?gb18030?B?bXVzbA==?= <musl@lists.openwall.com>
Subject: [musl] =?gb18030?B?u9i4tKO6IFttdXNsXSBRdWVzdGlvbjogV2h5IHZmcHJpbnRmIGNhbGwgdHdpY2UgcHJpbnRmX2NvcmU/?=
Date: Sun, 7 May 2023 09:44:19 +0800	[thread overview]
Message-ID: <tencent_267CE389F3DC36CCE34A825784819E25E109@qq.com> (raw)
In-Reply-To: <20230507011738.GJ4163@brightrain.aerifal.cx>

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="gb18030", Size: 3988 bytes --]

1¡¢I see musl will visit the format&nbsp;Whether %n$ exists or not£¬&nbsp;If %n$ does not exist, maybe the first call is redundant.
2¡¢I test this following format with benchmark£¬ the result show 145+ns-&gt;110+ns if I remove the first call.
"snprintf(buf, sizeof(buf), "this is a more typical error message with detail: %s", "No such file or directory");"


This is all irrelevant because calling printf with an invalid format&nbsp;string has undefined behavior.
--------------
3¡¢So I think we should find a way to get positional args&nbsp; when we see it rather than visit format firstly anyway.
------------------&nbsp;ԭʼÓʼþ&nbsp;------------------
·¢¼þÈË:                                                                                                                        "musl"                                                                                    <dalias@libc.org&gt;;
·¢ËÍʱ¼ä:&nbsp;2023Äê5ÔÂ7ÈÕ(ÐÇÆÚÌì) ÉÏÎç9:17
ÊÕ¼þÈË:&nbsp;"Markus Wichmann"<nullplan@gmx.net&gt;;
³­ËÍ:&nbsp;"musl"<musl@lists.openwall.com&gt;;
Ö÷Ìâ:&nbsp;Re: [musl] Question: Why vfprintf call twice printf_core?



On Sat, May 06, 2023 at 08:25:25AM +0200, Markus Wichmann wrote:
&gt; Am Sat, May 06, 2023 at 01:24:15PM +0800 schrieb 847567161:
&gt; &gt; snprintf(buf, sizeof(buf), "this is a more typical error message with detail: %s", "No such file or directory");
&gt; 
&gt; OK, that call is correct. It should not error out.
&gt; 
&gt; &gt;&gt; First call to printf_core() checks to see if there are any major&amp;nbsp;problems with the format string
&gt; &gt; Maybe the second call can also checks the format error£¿
&gt; &gt;
&gt; 
&gt; POSIX says that to the extent possible, all functions are supposed to
&gt; either fail with no side effects or succeed with side effects. There are
&gt; some functions that can fail with side effects, but we make some effort
&gt; to minimize that. By testing the format string first, if it is broken,
&gt; we can fail without side effects. If only the second call tested that,
&gt; you would get a partial output before failure.
&gt; 
&gt; Actually, in this case it was probably the other way around: Because
&gt; POSIX requires that positional arguments work, which requires an extra
&gt; pass over the format string, we got a side-effect free test for validity
&gt; for free.

This is all irrelevant because calling printf with an invalid format
string has undefined behavior. There is no requirement at all on the
implementation in this case. We could (and probably should) trap on
it; the current behavior of bailing out when it's bad is just a
consequence of how I implemnted the localization-form %n$ positional
args.

&gt; &gt;&gt; if the string is using positional arguments (e.g. "%2$d"), also
&gt; &gt;&gt; establishes the types of these arguments and writes them into an
&gt; &gt;&gt; array.
&gt; &gt; I use above format string£¬I think it's a&amp;nbsp;typical error message,
&gt; &gt; I found the first printf_core do string traversal and cost some time
&gt; &gt; showed in perf.
&gt; &gt;
&gt; &gt; If we remove the first function call when we don't use ("%2$d"), is
&gt; &gt; there any problem£¿Or do you have some advice for impove the vfprintf
&gt; &gt; performance in common scenarios£¿
&gt; 
&gt; vfprintf() can't know whether the format string contains positional
&gt; arguments without passing over the format string. Which is what the
&gt; first call does.
&gt; 
&gt; In any case, yes, you can patch your copy of musl to remove the first
&gt; call to printf_core(). You will no longer be able to use positional
&gt; arguments, and you will get partial output on format string error, but
&gt; if you can live with that, it should work.

Yes, I don't see any reason why this wouldn't work, but I also don't
see any good reason it would help. If passing over the format string
is taking a long time, maybe we should figure out why that's
happening...?

Rich

[-- Attachment #2: Type: text/html, Size: 4539 bytes --]

      reply	other threads:[~2023-05-07  1:44 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-06  3:29 [musl] Question: Why vfprintf call twice printf_core? 847567161
2023-05-06  3:53 ` Markus Wichmann
2023-05-06  5:24   ` =?gb18030?B?ODQ3NTY3MTYx?=
2023-05-06  6:25     ` Markus Wichmann
2023-05-06 17:55       ` NRK
2023-05-07  1:17       ` Rich Felker
2023-05-07  1:44         ` =?gb18030?B?ODQ3NTY3MTYx?= [this message]

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=tencent_267CE389F3DC36CCE34A825784819E25E109@qq.com \
    --to=847567161@qq.com \
    --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).