On Fri, Nov 07, 2014 at 11:40:03AM +0100, k...@shike2.com wrote: > > >> > * Use strtonum() or estrtol() from sbase instead of atoi(). > >> > >> I agree here if the string comes from the user. If the string > >> is an internal string then there is no problem with atoi. > > > > It is generally unlikely that the string has been validated to > > be an integer before getting to atoi(). With atoi() you cannot > > distinguish between an invalid integer and 0. > > There are a lot of cases where it is validated, for example > if you used first a regular expression to filter lines. There > are also a lot of uses where doesn't mind the difference > between 0 and no integer (for example sort -n). There > are also a lot of cases where 0 is not a valid value. > > > Generally speaking, it should never be used. > > 'should never be used' is not a very general sentence. > > But, ok. This is a personal taste (this and the other question > about strlcpy), and I don't want to begin a war.
BTW, just to clarify, strtol() and friends has its set of problems as well. It is too elaborate to check for error conditions unless you already have a wrapper to do this once. strtonum() is I think the most appropriate interface to default to and perhaps switch over to atoi() when you are aware of its limitations and they do not impact the correctness of your code.