Re: [PATCH 1/4] lib/sprintf: add scnprintf_append() helper function

From: Junrui Luo

Date: Tue Nov 11 2025 - 08:31:07 EST


On Mon, Nov 10, 2025 at 03:13:56PM +0100, Petr Mladek wrote:
> On Fri 2025-11-07 17:51:23, David Laight wrote:
> > On Fri, 7 Nov 2025 13:52:27 +0100
> > Petr Mladek <pmladek@xxxxxxxx> wrote:
> >
> > > On Fri 2025-11-07 11:35:35, Andy Shevchenko wrote:
> > > > On Fri, Nov 07, 2025 at 09:12:46AM +0000, David Laight wrote:
> > > > > On Thu, 6 Nov 2025 21:38:33 -0800
> > > > > Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> wrote:
> > > > > > On Fri, 7 Nov 2025 13:16:13 +0800 Junrui Luo <moonafterrain@xxxxxxxxxxx> wrote:
> > > >
> > > > ...
> > > >
> > > > > That is true for all the snprintf() functions.
> > > > >
> > > > > > 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.
> > > > >
> > > > > I'd be tempted to avoid the strlen() by passing in the offset.
> > > > > So (say):
> > > > > #define scnprintf_at(buf, len, off, ...) \
> > > > > scnprintf((buf) + off, (len) - off, __VA_ARGS__)
> > >
> > > It does not handle correctly the situation when len < off.
> > > Othersise, it looks good.
> >
> > That shouldn't happen unless the calling code is really buggy.
> > There is also a WARN_ON_ONCE() at the top of snprintf().
>
> Fair enough.
>
> BTW: I have found there exists a userspace library which implements
> this idea, the funtion is called vsnoprintf(), see
> https://arpa2.gitlab.io/arpa2common/group__snoprintf.html
>
> I know that it is cryptic. But I like the name. The letters "no"
> match the ordering of the parameters "size, offset".
>
> In our case, it would be scnoprintf() ...
>

Thanks for the feedback. Based on the discussion above, I plan to prepare a v2 patch.
int scnprintf_append(char *buf, size_t size, const char *fmt, ...)
{
va_list args;
size_t len;

len = strnlen(buf, size);
if (len == size)
return len;
va_start(args, fmt);
len += vscnprintf(buf + len, size - len, fmt, args);
va_end(args);
return len;
}
EXPORT_SYMBOL(scnprintf_append);

I agree that using a macro like David suggested, with an explicit offset, is a reasonable and efficient approach.
The `scnprintf_append()` function, however, does not require such a variable; though if used improperly, it could introduce an extra `strlen()` overhead.

However, if the consensus is to prefer the macro approach, I can rework the series to use `scnoprintf()`, as suggested by Petr instead.

That said, I believe `scnprintf_append()` also has its merits:
it simplifies one-off string constructions and provides built-in bound checking for safety.
Some existing code that appends strings in the kernel lacks proper bound checks, and this function could serve as a graceful replacement.
The benefits were also demonstrated in other patches.

Thanks,
Junrui