Re: [PATCH 1/4] lib/sprintf: add scnprintf_append() helper function
From: Takashi Iwai
Date: Sat Nov 08 2025 - 04:11:41 EST
On Sat, 08 Nov 2025 01:11:30 +0100,
Andrew Morton wrote:
>
> On Fri, 7 Nov 2025 09:12:46 +0000 David Laight <david.laight.linux@xxxxxxxxx> wrote:
>
> > > I wonder if we should instead implement a kasprintf() version of this
> > > which reallocs each time and then switch all the callers over to that.
> >
> > That adds the cost of a malloc, and I, like kasprintf() probably ends up
> > doing all the work of snprintf twice.
>
> There is no need at all to optimize the performance of scruffy once-off
> string pasting functions. For these it's better to optimize for
> readability, reliability. maintainability.
Actually this scnprintf_append() helper was my suggestion in another
threads:
https://lore.kernel.org/ME2PR01MB3156CEC4F31F253C9B540FB7AFFDA@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
https://lore.kernel.org/SYBPR01MB7881987D79C62D8122B655FEAFC6A@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Basically its use is for filling a substring with s*printf() inside a
fixed string such as a field in a struct. Through a quick grep, there
are many kernel code doing it without bounce checks, and it's for
helping those. So it's a bit different from what you assumed with the
re-allocatable buffers.
The most merit of this API is that it can just be a kind of drop-in
replacement without extra variable to keep the offset, as found in
this patch series.
Though, it won't change too much to introduce an offset variable as
the API David suggested, which looks nice, so I myself don't mind
either way (it's a bike-shed topic, after all :)
thanks,
Takashi