vfprintf typing problem

David Miller davem@davemloft.net
Sun Apr 1 05:30:00 GMT 2012


From: "Carlos O'Donell" <carlos@systemhalted.org>
Date: Sun, 1 Apr 2012 00:25:20 -0400

> Shucks... if you look closely I *did* do the math wrong.
> 
> It should be (retval > (INT_MAX - digit)/10)

Unfortunately that's a lot more expensive than any of the previous
attempts at this test.  At least with the "INT_MAX/10 - digit"
variant, no real divide would be involved.

Now we're going to incur a real divide calculation every loop
iteration.

It's definitely cheaper to do something like:

	int overflow = 0;

	while (...) {
	...
		retval *= 10;
		overflow |= ((int) retval < 0);
		retval += digit;
		overflow |= ((int) retval < 0);
	}
	return overflow ? -1 : (int) retval;

This also has the benefit, unlike all of the previous variants, of
advancing past all of the digits in the format string for the caller,
not just the ones we hit up until the point where we detect overflow.



More information about the Libc-alpha mailing list