From owner-svn-src-head@FreeBSD.ORG Fri Aug 30 11:21:52 2013 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTP id E3E82DA8; Fri, 30 Aug 2013 11:21:52 +0000 (UTC) (envelope-from pluknet@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id B662F2BF7; Fri, 30 Aug 2013 11:21:52 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.7/8.14.7) with ESMTP id r7UBLqcv077494; Fri, 30 Aug 2013 11:21:52 GMT (envelope-from pluknet@svn.freebsd.org) Received: (from pluknet@localhost) by svn.freebsd.org (8.14.7/8.14.5/Submit) id r7UBLqdQ077492; Fri, 30 Aug 2013 11:21:52 GMT (envelope-from pluknet@svn.freebsd.org) Message-Id: <201308301121.r7UBLqdQ077492@svn.freebsd.org> From: Sergey Kandaurov Date: Fri, 30 Aug 2013 11:21:52 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r255069 - head/lib/libutil X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 30 Aug 2013 11:21:53 -0000 Author: pluknet Date: Fri Aug 30 11:21:52 2013 New Revision: 255069 URL: http://svnweb.freebsd.org/changeset/base/255069 Log: The round of expand_number() cleanups. o Fix range error checking to detect overflow when uint64_t < uintmax_t. o Remove a non-functional check for no valid digits as pointed out by Bruce. o Remove a rather pointless comment describing what the function does. o Clean up a bunch of style bugs. Brucified by: bde Modified: head/lib/libutil/expand_number.c Modified: head/lib/libutil/expand_number.c ============================================================================== --- head/lib/libutil/expand_number.c Fri Aug 30 10:45:02 2013 (r255068) +++ head/lib/libutil/expand_number.c Fri Aug 30 11:21:52 2013 (r255069) @@ -35,42 +35,24 @@ __FBSDID("$FreeBSD$"); #include #include -/* - * Convert an expression of the following forms to a uint64_t. - * 1) A positive decimal number. - * 2) A positive decimal number followed by a 'b' or 'B' (mult by 1). - * 3) A positive decimal number followed by a 'k' or 'K' (mult by 1 << 10). - * 4) A positive decimal number followed by a 'm' or 'M' (mult by 1 << 20). - * 5) A positive decimal number followed by a 'g' or 'G' (mult by 1 << 30). - * 6) A positive decimal number followed by a 't' or 'T' (mult by 1 << 40). - * 7) A positive decimal number followed by a 'p' or 'P' (mult by 1 << 50). - * 8) A positive decimal number followed by a 'e' or 'E' (mult by 1 << 60). - */ int expand_number(const char *buf, uint64_t *num) { + char *endptr; + uintmax_t umaxval; uint64_t number; - int saved_errno; unsigned shift; - char *endptr; + int serrno; - saved_errno = errno; + serrno = errno; errno = 0; - - number = strtoumax(buf, &endptr, 0); - - if (number == UINTMAX_MAX && errno == ERANGE) { - return (-1); - } - - if (errno == 0) - errno = saved_errno; - - if (endptr == buf) { - /* No valid digits. */ - errno = EINVAL; + umaxval = strtoumax(buf, &endptr, 0); + if (umaxval > UINT64_MAX) + errno = ERANGE; + if (errno != 0) return (-1); - } + errno = serrno; + number = umaxval; switch (tolower((unsigned char)*endptr)) { case 'e': @@ -106,7 +88,6 @@ expand_number(const char *buf, uint64_t errno = ERANGE; return (-1); } - *num = number << shift; return (0); }