From nobody Tue Jun 13 04:08:47 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4QgFRM3Shxz4cmS4; Tue, 13 Jun 2023 04:08:47 +0000 (UTC) (envelope-from git@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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4QgFRM31vYz3C2G; Tue, 13 Jun 2023 04:08:47 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1686629327; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=b/wx0hl8hMVn/UQSM05wYS1Pi6TRHwLjY4aXtQlxcM0=; b=CvCrj5AjABJCtf1DeTC89bG8CCpiKkQv2M/Q7NXQN+JsS+PvuS/fLx/Y00S2zSwit3MViB 2GhcuszqHYnCoPuzGjsmwRM7eN6Ru+aYoEnoz8BNrm9MVgcrXaZKB6aKTsDyXuiLSetVUA 2N3Ce70wkrnoEBILx1A3idIQ5p0q0G+UhzmdJk9xkB352fh6+CwNdcOkOtnUFdeRUXffQk Gvta9CjWOzdLjZTDcHUrdG+nuwKUQbJwSegaJSpqAxe+RAsNNtomlVGs3YMbIbm3TE+Gke ml0oobtxR9xoy1PG0DBjuQfqGeTx2HE778l8/14U5egwBlUOalREuzz/o7j/OA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1686629327; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=b/wx0hl8hMVn/UQSM05wYS1Pi6TRHwLjY4aXtQlxcM0=; b=teVJW8fD1x919ts8vDHf13RPbaTUbLTHpwMVLuZEHqMu1MKxahWR2cFyDm8bz5NCapF9S2 NQPK16fYpCbfD3hx8XPW2uRwksvrGmIUKTuIAYWo16piCfUkcPYTGthgYluOY6VtJlZd7e zABLdw9vFq6zDuEaZuBO1JYckP2h8he/nIRZWghgj5hTHkXzHC9AMSsKrfMJbiMsgE1aR1 YSoPHPSS5etRF7rMVkxbrC3zRyWyMdPs2NZkWHg+HQ1tQ3QF6Y7Azst+LddQZB+z1Gmyag GAt5LNSlO3BGnhd39S1mjV0gbQD7GxF48X8VxMWE7vePxcNtFYX6/6OT3f6O/A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1686629327; a=rsa-sha256; cv=none; b=if+MifqSJU7fYo8Wa/bvLycLlW2zgxTQ7PmMwTKnZdm4eJMOFoWmqm4CMageEmArPAuXVV 7GEMmTch+LTMhtqzMUA4+YltwLuc42TLbKZ2cIPuqeIUx6/dFqfI4geU/uK9Hf6JJ/KelH xfq4ohtx7z/zQooD49/WHOLJspQC7FVddoQ1LacoPNsImcAnTqVE3Jvs5F5qSuaWYcjm8x adNI1aoGbP4nk/Z7zyIUTs+tkAxfh10v0k+8kT15zTEmxb3QPLkbuhX3AMiMW8HWbZbqaI kPF44s3f8WOiYgX4ejARLtvzDZgcHQYW7zY46TVgxaySd0ioy2ZS5XCy1kWYfA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4QgFRM22fQz171X; Tue, 13 Jun 2023 04:08:47 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 35D48lh9000798; Tue, 13 Jun 2023 04:08:47 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 35D48l57000797; Tue, 13 Jun 2023 04:08:47 GMT (envelope-from git) Date: Tue, 13 Jun 2023 04:08:47 GMT Message-Id: <202306130408.35D48l57000797@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Xin LI Subject: git: 08300d849485 - main - expand_number: Tighten check of unit. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: delphij X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 08300d849485bd6231d3a18cb430cab4279edef3 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by delphij: URL: https://cgit.FreeBSD.org/src/commit/?id=08300d849485bd6231d3a18cb430cab4279edef3 commit 08300d849485bd6231d3a18cb430cab4279edef3 Author: Xin LI AuthorDate: 2023-06-13 04:08:32 +0000 Commit: Xin LI CommitDate: 2023-06-13 04:08:32 +0000 expand_number: Tighten check of unit. The current code silently ignores characters after the unit as long the unit themselves were recognized. This commit makes expand_number(3) to fail with EINVAL if buf did not terminate after the unit character. Historically, the function accepts and ignores "B" as a SI unit, this behavior is preserved and e.g. KB, MB are still accepted as aliases of K and M, document this behavior in the manual page. While I am there, also write a few test cases to validate the behavior. Reviewed-by: emaste MFC-after: 2 weeks Differential Revision: https://reviews.freebsd.org/D40482 --- lib/libutil/expand_number.3 | 11 ++++- lib/libutil/expand_number.c | 14 ++++++ lib/libutil/tests/Makefile | 1 + lib/libutil/tests/expand_number_test.c | 87 ++++++++++++++++++++++++++++++++++ 4 files changed, 112 insertions(+), 1 deletion(-) diff --git a/lib/libutil/expand_number.3 b/lib/libutil/expand_number.3 index 7fd9eb2db934..ed7ef487f08b 100644 --- a/lib/libutil/expand_number.3 +++ b/lib/libutil/expand_number.3 @@ -25,7 +25,7 @@ .\" .\" $FreeBSD$ .\" -.Dd July 20, 2019 +.Dd June 13, 2023 .Dt EXPAND_NUMBER 3 .Os .Sh NAME @@ -63,6 +63,15 @@ The suffixes are: .It Li P Ta No peta Ta 1125899906842624 .It Li E Ta No exa Ta 1152921504606846976 .El +.Pp +For historical reasons, the +.Fn expand_number +function accepts and ignores a single +.Dq B +suffix at the end of the +.Fa buf +string. +However, the usage of this suffix is discouraged. .Sh RETURN VALUES .Rv -std .Sh ERRORS diff --git a/lib/libutil/expand_number.c b/lib/libutil/expand_number.c index f34db013ff18..61b73039f3bd 100644 --- a/lib/libutil/expand_number.c +++ b/lib/libutil/expand_number.c @@ -76,6 +76,8 @@ expand_number(const char *buf, uint64_t *num) shift = 10; break; case 'b': + shift = 0; + break; case '\0': /* No unit. */ *num = number; return (0); @@ -85,6 +87,18 @@ expand_number(const char *buf, uint64_t *num) return (-1); } + /* + * Treat 'b' as an ignored suffix for all unit except 'b', + * otherwise there should be no remaining character(s). + */ + endptr++; + if (shift != 0 && tolower((unsigned char)*endptr) == 'b') + endptr++; + if (*endptr != '\0') { + errno = EINVAL; + return (-1); + } + if ((number << shift) >> shift != number) { /* Overflow */ errno = ERANGE; diff --git a/lib/libutil/tests/Makefile b/lib/libutil/tests/Makefile index fc9a871dd8bc..9816988b74be 100644 --- a/lib/libutil/tests/Makefile +++ b/lib/libutil/tests/Makefile @@ -7,6 +7,7 @@ TAP_TESTS_C+= pidfile_test TAP_TESTS_C+= trimdomain_test TAP_TESTS_C+= trimdomain-nodomain_test ATF_TESTS_C+= cpuset_test +ATF_TESTS_C+= expand_number_test WARNS?= 2 LIBADD+= util diff --git a/lib/libutil/tests/expand_number_test.c b/lib/libutil/tests/expand_number_test.c new file mode 100644 index 000000000000..319df26c9621 --- /dev/null +++ b/lib/libutil/tests/expand_number_test.c @@ -0,0 +1,87 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause + * + * Copyright (c) 2023 Google LLC + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include + +#include + +ATF_TC_WITHOUT_HEAD(positivetests); +ATF_TC_BODY(positivetests, tc) +{ + int retval; + uint64_t num; + +#define positive_tc(string, value) \ + do { \ + ATF_CHECK_ERRNO(0, (retval = expand_number((string), &num)) == 0); \ + ATF_CHECK_EQ(retval, 0); \ + ATF_CHECK_EQ(num, (value)); \ + } while (0) + + positive_tc("123456", 123456); + positive_tc("123456b", 123456); + positive_tc("1k", 1024); + positive_tc("1kb", 1024); + positive_tc("1K", 1024); + positive_tc("1KB", 1024); + positive_tc("1m", 1048576); + positive_tc("1M", 1048576); + positive_tc("1g", 1073741824); + positive_tc("1G", 1073741824); + positive_tc("1t", 1099511627776); + positive_tc("1T", 1099511627776); + positive_tc("1p", 1125899906842624); + positive_tc("1P", 1125899906842624); + positive_tc("1e", 1152921504606846976); + positive_tc("1E", 1152921504606846976); + positive_tc("15E", 17293822569102704640ULL); +} + +ATF_TC_WITHOUT_HEAD(negativetests); +ATF_TC_BODY(negativetests, tc) +{ + int retval; + uint64_t num; + + ATF_CHECK_ERRNO(EINVAL, retval = expand_number("", &num)); + ATF_CHECK_ERRNO(EINVAL, retval = expand_number("x", &num)); + ATF_CHECK_ERRNO(EINVAL, retval = expand_number("1bb", &num)); + ATF_CHECK_ERRNO(EINVAL, retval = expand_number("1x", &num)); + ATF_CHECK_ERRNO(EINVAL, retval = expand_number("1kx", &num)); + ATF_CHECK_ERRNO(ERANGE, retval = expand_number("16E", &num)); +} + +ATF_TP_ADD_TCS(tp) +{ + ATF_TP_ADD_TC(tp, positivetests); + ATF_TP_ADD_TC(tp, negativetests); + return (atf_no_error()); +}