RE: [RFC[ Alloc in vsprintf

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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)




[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Bugtraq]     [Linux OMAP]     [Linux MIPS]     [eCos]     [Asterisk Internet PBX]     [Linux API]

  Powered by Linux