[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20251106213833.546c8eaba8aec6aa6a5e30b6@linux-foundation.org>
Date: Thu, 6 Nov 2025 21:38:33 -0800
From: Andrew Morton <akpm@...ux-foundation.org>
To: Junrui Luo <moonafterrain@...look.com>
Cc: linux-kernel@...r.kernel.org, pmladek@...e.com, rostedt@...dmis.org,
andriy.shevchenko@...ux.intel.com, tiwai@...e.com, perex@...ex.cz,
linux-sound@...r.kernel.org, mchehab@...nel.org, awalls@...metrocast.net,
linux-media@...r.kernel.org, davem@...emloft.net, edumazet@...gle.com,
kuba@...nel.org, pabeni@...hat.com, netdev@...r.kernel.org
Subject: Re: [PATCH 1/4] lib/sprintf: add scnprintf_append() helper function
On Fri, 7 Nov 2025 13:16:13 +0800 Junrui Luo <moonafterrain@...look.com> wrote:
> +/**
> + * scnprintf_append - Append a formatted string to a buffer
> + * @buf: The buffer to append to (must be null-terminated)
> + * @size: The size of the buffer
> + * @fmt: Format string
> + * @...: Arguments for the format string
> + *
> + * This function appends a formatted string to an existing null-terminated
> + * buffer. It is safe to use in a chain of calls, as it returns the total
> + * length of the string.
> + *
> + * Returns: The total length of the string in @buf
It wouldn't hurt to describe the behavior when this runs out of space
in *buf.
The whole thing is a bit unweildy - how much space must the caller
allocate for `buf'? I bet that's a wild old guess.
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.
um,
int kasprintf_append(char **pbuf, gfp_t gfp_flags, const char *fmt, ...);
int caller()
{
char *buf = NULL;
while (...) {
x = kasprintf_append(&buf, GFP_KERNEL, "%whatever", stuff...);
if (x == -ENOMEM)
return x;
}
...
kfree(buf);
}
So much nicer!
Powered by blists - more mailing lists