"Rational" way of getting a number in human readable form?

Bruce Evans brde at optusnet.com.au
Fri Nov 30 23:21:30 PST 2007


On Fri, 30 Nov 2007, Xin LI wrote:

> It seems that we have some code getenv_quad() that accepts numbers in
> "50M" "36G" alike form.  Is it reasonable to add some sort of format
> characters to vsscanf(9) so that it would be able to handle this, or is
> it more appropriate to grow my own one?

No.  The scanf family shouldn't exist except for compatibility in userland,
since it gives uncontrollable undefined behaviour on numeric overflow, so
it is unusable except for reading certain character data.  The strto*()
family should be used for reading numeric data.  Unfortunately, strtol(9)
has a broken API -- errno doesn't exist in the kernel, but the strto*()
family depends on it for reporting overflow.

getenv_quad() has its own overflow bugs (blind multiplication by the
scale factors...) and shouldn't exist for other reasons.

Bruce


More information about the freebsd-arch mailing list