Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20230506175527.ic4ibgxo6e7mquq4@gen2.localdomain>
Date: Sat, 6 May 2023 23:55:27 +0600
From: NRK <nrk@...root.org>
To: musl@...ts.openwall.com
Subject: Re: Question: Why vfprintf call twice printf_core?

On Sat, May 06, 2023 at 08:25:25AM +0200, Markus Wichmann wrote:
> 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.

Agreed. However...

> 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);

strcat (and friends) are the opposite of performance:
https://en.wikipedia.org/wiki/Joel_Spolsky#Schlemiel_the_Painter.27s_algorithm

Better alternative: have your string copy function return a pointer to the
nul-byte. This pointer can be both used for efficient concat as well as
determining the string length.

Example using POSIX stpcpy(3) (minus bounds checking):

	char *p = stpcpy(buf, "this is a more typical error message with detail: ");
	p = stpcpy(p, "No such file or directory");
	write(2, buf, p - buf);

Additionally, consider getting rid of nul-strings altogether and only
use them in interface boundaries that require them.

- NRK

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.