From owner-svn-src-all@freebsd.org Thu Jul 30 06:14:49 2015 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 54FE19ADE22; Thu, 30 Jul 2015 06:14:49 +0000 (UTC) (envelope-from bapt@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 2BEB81D4; Thu, 30 Jul 2015 06:14:49 +0000 (UTC) (envelope-from bapt@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.70]) by repo.freebsd.org (8.14.9/8.14.9) with ESMTP id t6U6EnQE065981; Thu, 30 Jul 2015 06:14:49 GMT (envelope-from bapt@FreeBSD.org) Received: (from bapt@localhost) by repo.freebsd.org (8.14.9/8.14.9/Submit) id t6U6EmJR065979; Thu, 30 Jul 2015 06:14:48 GMT (envelope-from bapt@FreeBSD.org) Message-Id: <201507300614.t6U6EmJR065979@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: bapt set sender to bapt@FreeBSD.org using -f From: Baptiste Daroussin Date: Thu, 30 Jul 2015 06:14:48 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r286066 - head/usr.sbin/pw X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 30 Jul 2015 06:14:49 -0000 Author: bapt Date: Thu Jul 30 06:14:47 2015 New Revision: 286066 URL: https://svnweb.freebsd.org/changeset/base/286066 Log: Improve strtounum Fix many style bugs Better variable naming Use C99 'restrict' were apropriate Fix potential errno race Submitted by: bde Modified: head/usr.sbin/pw/pw.h head/usr.sbin/pw/strtounum.c Modified: head/usr.sbin/pw/pw.h ============================================================================== --- head/usr.sbin/pw/pw.h Thu Jul 30 05:13:12 2015 (r286065) +++ head/usr.sbin/pw/pw.h Thu Jul 30 06:14:47 2015 (r286066) @@ -103,5 +103,5 @@ char *pw_pwcrypt(char *password); extern const char *Modes[]; extern const char *Which[]; -uintmax_t strtounum(const char *numstr, uintmax_t minval, uintmax_t maxval, - const char **errmsg); +uintmax_t strtounum(const char * __restrict, uintmax_t, uintmax_t, + const char ** __restrict); Modified: head/usr.sbin/pw/strtounum.c ============================================================================== --- head/usr.sbin/pw/strtounum.c Thu Jul 30 05:13:12 2015 (r286065) +++ head/usr.sbin/pw/strtounum.c Thu Jul 30 06:14:47 2015 (r286066) @@ -34,41 +34,38 @@ __FBSDID("$FreeBSD$"); #include "pw.h" -#define INVALID "invalid" -#define TOOSMALL "too small" -#define TOOLARGE "too large" - uintmax_t -strtounum(const char *numstr, uintmax_t minval, uintmax_t maxval, - const char **errstrp) +strtounum(const char * __restrict np, uintmax_t minval, uintmax_t maxval, + const char ** __restrict errpp) { - uintmax_t ret = 0; - char *ep; + char *endp; + uintmax_t ret; if (minval > maxval) { errno = EINVAL; - if (errstrp != NULL) - *errstrp = INVALID; + if (errpp != NULL) + *errpp = "invalid"; return (0); } - - ret = strtoumax(numstr, &ep, 10); - if (errno == EINVAL || numstr == ep || *ep != '\0') { + errno = 0; + ret = strtoumax(np, &endp, 10); + if (endp == np || *endp != '\0') { errno = EINVAL; - if (errstrp != NULL) - *errstrp = INVALID; + if (errpp != NULL) + *errpp = "invalid"; return (0); - } else if ((ret == 0 && errno == ERANGE) || ret < minval) { + } + if (ret < minval) { errno = ERANGE; - if (errstrp != NULL) - *errstrp = TOOSMALL; + if (errpp != NULL) + *errpp = "too small"; return (0); - } else if ((ret == UINTMAX_MAX && errno == ERANGE) || ret > maxval) { + } + if (errno == ERANGE || ret > maxval) { errno = ERANGE; - if (errstrp != NULL) - *errstrp = TOOLARGE; + if (errpp != NULL) + *errpp = "too large"; return (0); } - return (ret); }