On Tue, 1 Nov 2016, Christos Zoulas wrote:
On Nov 2, 8:49am, paul%whooppee.com@localhost (Paul Goyette) wrote: -- Subject: Re: CVS commit: src/sys/dev/pci | > Why *len = 1 here? Shouldn't it be 0 since there is no more room left? | | No. :) | | The maximum number of characters actually written by vsnprintf() will | never exceed (len - 1). So, dest gets incremented by the max, and len | gets decremented by the max. | | There is always enough room left for vsnprintf() to create a new | trailing NUL. But that's doing extra work for no reason...
Perhaps. But, since dest points to the last valid character of the output buffer (not to the first character after the buffer), there really is room for one more character.
Even in the non-overflow case, if you write x-1 chars (not including the trailing NUL) to a buffer of size x, the updated value of len will be 1 (and dest would point to the last valid character of the buffer). We would need additional code in the non-overflow path
if (*len == 1) *len = 0; to maintain consistency with the overflow path. +------------------+--------------------------+------------------------+ | Paul Goyette | PGP Key fingerprint: | E-mail addresses: | | (Retired) | FA29 0E3B 35AF E8AE 6651 | paul at whooppee.com | | Kernel Developer | 0786 F758 55DE 53BA 7731 | pgoyette at netbsd.org | +------------------+--------------------------+------------------------+