From owner-freebsd-current@FreeBSD.ORG Thu Apr 14 02:39:28 2005 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 4971316A4CE for ; Thu, 14 Apr 2005 02:39:28 +0000 (GMT) Received: from cvs.openbsd.org (cvs.openbsd.org [199.185.137.3]) by mx1.FreeBSD.org (Postfix) with ESMTP id D577143D49 for ; Thu, 14 Apr 2005 02:39:27 +0000 (GMT) (envelope-from deraadt@cvs.openbsd.org) Received: from cvs.openbsd.org (localhost [127.0.0.1]) by cvs.openbsd.org (8.13.3/8.12.1) with ESMTP id j3E2dRkG018431; Wed, 13 Apr 2005 20:39:27 -0600 (MDT) Message-Id: <200504140239.j3E2dRkG018431@cvs.openbsd.org> To: Ted Unangst In-reply-to: Your message of "Wed, 13 Apr 2005 22:23:42 EDT." Date: Wed, 13 Apr 2005 20:39:27 -0600 From: Theo de Raadt X-Mailman-Approved-At: Thu, 14 Apr 2005 12:07:06 +0000 cc: tech@openbsd.org cc: freebsd-current@freebsd.org Subject: Re: strtonum(3) in FreeBSD? X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 14 Apr 2005 02:39:28 -0000 > 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.