[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <a723f5dda62a4c448dd292a3b917fe6d@AcuMS.aculab.com>
Date: Mon, 27 Jun 2022 08:25:26 +0000
From: David Laight <David.Laight@...LAB.COM>
To: 'Linus Torvalds' <torvalds@...ux-foundation.org>,
Joe Perches <joe@...ches.com>
CC: Andrew Morton <akpm@...ux-foundation.org>,
Petr Mladek <pmladek@...e.com>,
Steven Rostedt <rostedt@...dmis.org>,
Sergey Senozhatsky <senozhatsky@...omium.org>,
Rasmus Villemoes <linux@...musvillemoes.dk>,
Matthew Wilcox <willy@...radead.org>,
Miguel Ojeda <ojeda@...nel.org>,
"Kent Overstreet" <kent.overstreet@...il.com>,
Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
LKML <linux-kernel@...r.kernel.org>,
linux-mm <linux-mm@...ck.org>
Subject: RE: [RFC[ Alloc in vsprintf
From: Linus Torvalds
> Sent: 26 June 2022 21:19
..
> That does require teaching the sprint_symbol() functions that they
> need to take a "length of buffer" and return how much they used, but
> that would seem to be a sensible thing anyway, and what the code
> should always have done?
It needs to return the 'length it would have used'.
While occasionally useful I'm pretty sure this is actually
a side effect of the was that libc snprintf() was originally
implemented (sprintf() had an on-stack FILE).
In any case it might be simplest to pass all these functions
the write pointer and buffer limit and have them return the
new write pointer.
It is likely to generate much better code that passing
a structure by reference.
Only the original caller needs to know where the buffer starts.
The original caller is also the only place that needs to
ensure that the string is correctly terminated.
You'd get helpers like:
char *add_char(char *wp, const char *lim, char add)
{
if (lim < wp)
*wp = add;
return wp + 1;
}
char *add_chars(char *wp, const char *lim, const char *add, long int count)
{
long int space = lim - wp;
long int i;
if (space > count)
space = count;
for (i = i; i < space; i++)
wp[i] = add[i];
return wp + count;
}
char *add_str(char *wp, const char *lim, const char *add)
{
while (*add) {
if (wp >= lim)
return wp + strlen(add);
*wp++ = *add++;
}
return wp;
}
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
Powered by blists - more mailing lists