Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Sat, 6 May 2023 08:25:25 +0200
From: Markus Wichmann <nullplan@....net>
To: musl@...ts.openwall.com
Subject: Re: Question: Why vfprintf call twice printf_core?

Am Sat, May 06, 2023 at 01:24:15PM +0800 schrieb 847567161:
> snprintf(buf, sizeof(buf), "this is a more typical error message with detail: %s", "No such file or directory");

OK, that call is correct. It should not error out.

>> First call to printf_core() checks to see if there are any major&nbsp;problems with the format string
> Maybe the second call can also checks the format error?
>

POSIX says that to the extent possible, all functions are supposed to
either fail with no side effects or succeed with side effects. There are
some functions that can fail with side effects, but we make some effort
to minimize that. By testing the format string first, if it is broken,
we can fail without side effects. If only the second call tested that,
you would get a partial output before failure.

Actually, in this case it was probably the other way around: Because
POSIX requires that positional arguments work, which requires an extra
pass over the format string, we got a side-effect free test for validity
for free.

>> if the string is using positional arguments (e.g. "%2$d"), also
>> establishes the types of these arguments and writes them into an
>> array.
> I use above format string,I think it's a&nbsp;typical error message,
> I found the first printf_core do string traversal and cost some time
> showed in perf.
>
> If we remove the first function call when we don't use ("%2$d"), is
> there any problem?Or do you have some advice for impove the vfprintf
> performance in common scenarios?

vfprintf() can't know whether the format string contains positional
arguments without passing over the format string. Which is what the
first call does.

In any case, yes, you can patch your copy of musl to remove the first
call to printf_core(). You will no longer be able to use positional
arguments, and you will get partial output on format string error, but
if you can live with that, it should work.

If you're looking for performance, however, I suggest steering clear of
the printf() family of functions. They contain complex logic that is
typically way overpowered for common needs, and just straight string
manipulation will always be faster. E.g. the above call could be turned
into

strlcpy(buf, "this is a more typical error message with detail: ", sizeof buf);
strlcat(buf, "No such file or directory", sizeof buf);

Of course, within ISO-C it gets more complicated, since strlcpy() and
strlcat() are BSD functions.

Ciao,
Markus

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.