Unix Technical Forum

Re: strtonum(3) in FreeBSD?

This is a discussion on Re: strtonum(3) in FreeBSD? within the lucky.openbsd.tech forums, part of the OpenBSD category; --> > strtonum came about because ping had a whole variety of issues with > numerical arguments. i created a ...


Go Back   Unix Technical Forum > Unix Operating Systems > OpenBSD > lucky.openbsd.tech

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-22-2008, 11:27 AM
Theo de Raadt
 
Posts: n/a
Default Re: strtonum(3) in FreeBSD?

> strtonum came about because ping had a whole variety of issues with
> numerical arguments. i created a strtonum function for it that was pretty
> much special case. it doesn't take long to realize that there's also
> ping6. and another thing. so the interface was widened up some. but not
> too big. there was a lot of discussion about exactly what strtonum would
> do, what it wouldn't do, and how one would use it. you don't have to
> agree with our decisions, but it sounds like you're descending in on
> "strtol but not called strtol".


As Tedu explains, strtonum() was designed to resolve the way that
atoi() and strtol() are misused.

And not just in a few places, but everywhere.

Hundreds and thousands of places will take "57b" and say that is the
number 57, because they use atoi().

So people are told to use strtol().

People are saying that strtonum() is a poor interface. Here's how you
have to use strtol() correctly to handle all the cases:


char *ep;
int ival;
long lval;

...

errno = 0;
lval = strtol(buf, &ep, 10);
if (buf[0] == '\0' || *ep != '\0')
goto not_a_number;
if ((errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) ||
(lval > INT_MAX || lval < INT_MIN))
goto out_of_range;
ival = lval;

[This is a quote from our manual page, please read it for more details
about how hard these interfaces are difficult to use perfectly]

This is why strtol() is not an atoi() replacement.

strtonum() is designed to be that replacement. It is easy to use, and
it is easy to take existing code using atoi() or strtol() [incorrectly
used most of the time, too] and convert them to use it.

That is the number 1 goal.

If you don't understand what strtonum()'s reasons for existance are,
of course you will judge it wrong.

If correcting all the atoi() and strtol() or strtoul() bugs in your
source tree doesn't matter to you, then please feel free to ignore
strtonum().

We just got really sick of copying that blob of code above all over
the place.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:13 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com