From owner-svn-src-all@freebsd.org Tue Jun 16 19:21:28 2020 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id E87613308A7; Tue, 16 Jun 2020 19:21:28 +0000 (UTC) (envelope-from jmallett@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 49mdNS5v4Wz4Ypn; Tue, 16 Jun 2020 19:21:28 +0000 (UTC) (envelope-from jmallett@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id C5C65141FF; Tue, 16 Jun 2020 19:21:28 +0000 (UTC) (envelope-from jmallett@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 05GJLSb5010971; Tue, 16 Jun 2020 19:21:28 GMT (envelope-from jmallett@FreeBSD.org) Received: (from jmallett@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 05GJLSpa010970; Tue, 16 Jun 2020 19:21:28 GMT (envelope-from jmallett@FreeBSD.org) Message-Id: <202006161921.05GJLSpa010970@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: jmallett set sender to jmallett@FreeBSD.org using -f From: Juli Mallett Date: Tue, 16 Jun 2020 19:21:28 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r362236 - head/usr.sbin/mpsutil X-SVN-Group: head X-SVN-Commit-Author: jmallett X-SVN-Commit-Paths: head/usr.sbin/mpsutil X-SVN-Commit-Revision: 362236 X-SVN-Commit-Repository: base 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.33 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: Tue, 16 Jun 2020 19:21:29 -0000 Author: jmallett Date: Tue Jun 16 19:21:28 2020 New Revision: 362236 URL: https://svnweb.freebsd.org/changeset/base/362236 Log: Improve unit parsing of mpsutil. Previously, it used atoi(3) to parse the unit parameter, which would silently yield a unit of 0 in the presence of an invalid unit number. As most users of mpsutil(8) are likely to have at least a unit 0, this is could have confusing results. This behaviour was particularly unintuitive if one incorrectly passed an adapter device name, or a device path, instead of a unit number. In addition to using strtoumax(3) instead of atoi(3) to parse unit numbers, support stripping a device name (e.g. mps1) or path (e.g. /dev/mps2) to just its unit number. Reviewed by: scottl (earlier version) Modified: head/usr.sbin/mpsutil/mpsutil.c Modified: head/usr.sbin/mpsutil/mpsutil.c ============================================================================== --- head/usr.sbin/mpsutil/mpsutil.c Tue Jun 16 18:39:56 2020 (r362235) +++ head/usr.sbin/mpsutil/mpsutil.c Tue Jun 16 19:21:28 2020 (r362236) @@ -37,6 +37,8 @@ __RCSID("$FreeBSD$"); #include #include #include +#include +#include #include #include #include @@ -91,6 +93,8 @@ int main(int ac, char **av) { struct mpsutil_command **cmd; + uintmax_t unit; + char *end; int ch; is_mps = !strcmp(getprogname(), "mpsutil"); @@ -98,7 +102,17 @@ main(int ac, char **av) while ((ch = getopt(ac, av, "u:h?")) != -1) { switch (ch) { case 'u': - mps_unit = atoi(optarg); + if (strncmp(optarg, _PATH_DEV, strlen(_PATH_DEV)) == 0) { + optarg += strlen(_PATH_DEV); + if (strncmp(optarg, is_mps ? "mps" : "mpr", 3) != 0) + errx(1, "Invalid device: %s", optarg); + } + if (strncmp(optarg, is_mps ? "mps" : "mpr", 3) == 0) + optarg += 3; + unit = strtoumax(optarg, &end, 10); + if (*end != '\0' || unit == UINTMAX_MAX || unit > INT_MAX) + errx(1, "Invalid unit: %s", optarg); + mps_unit = unit; break; case 'h': case '?':