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 @@ -1,86 +1,95 @@ .\" Copyright (c) 2007 Eric Anderson .\" Copyright (c) 2007 Pawel Jakub Dawidek .\" All rights reserved. .\" .\" 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 AUTHORS 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 AUTHORS 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. .\" .\" $FreeBSD$ .\" -.Dd July 20, 2019 +.Dd June 13, 2023 .Dt EXPAND_NUMBER 3 .Os .Sh NAME .Nm expand_number .Nd format a number from human readable form .Sh LIBRARY .Lb libutil .Sh SYNOPSIS .In libutil.h .Ft int .Fo expand_number .Fa "const char *buf" "uint64_t *num" .Fc .Sh DESCRIPTION The .Fn expand_number function parses the .Fa buf string and stores a unsigned 64-bit quantity at .Fa *num . .Pp The .Fn expand_number function is case-insensitive and follows the SI power of two convention. .Pp The suffixes are: .Bl -column "Suffix" "Description" "1000000000000000000" -offset indent .It Sy "Suffix" Ta Sy "Description" Ta Sy "Multiplier" .It Li K Ta No kilo Ta 1024 .It Li M Ta No mega Ta 1048576 .It Li G Ta No giga Ta 1073741824 .It Li T Ta No tera Ta 1099511627776 .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 The .Fn expand_number function will fail if: .Bl -tag -width Er .It Bq Er EINVAL The given string contains no digits. .It Bq Er EINVAL An unrecognized suffix was given. .It Bq Er ERANGE Result doesn't fit into 64 bits. .El .Sh SEE ALSO .Xr humanize_number 3 .Sh HISTORY The .Fn expand_number function first appeared in .Fx 6.3 . 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 @@ -1,95 +1,109 @@ /*- * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2007 Eric Anderson * Copyright (c) 2007 Pawel Jakub Dawidek * All rights reserved. * * 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 AUTHORS 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 AUTHORS 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 #include #include #include int expand_number(const char *buf, uint64_t *num) { char *endptr; uintmax_t umaxval; uint64_t number; unsigned shift; int serrno; serrno = errno; errno = 0; 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': shift = 60; break; case 'p': shift = 50; break; case 't': shift = 40; break; case 'g': shift = 30; break; case 'm': shift = 20; break; case 'k': shift = 10; break; case 'b': + shift = 0; + break; case '\0': /* No unit. */ *num = number; return (0); default: /* Unrecognized unit. */ errno = EINVAL; 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; return (-1); } *num = number << shift; return (0); } 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 @@ -1,14 +1,15 @@ # $FreeBSD$ TAP_TESTS_C+= flopen_test TAP_TESTS_C+= grp_test TAP_TESTS_C+= humanize_number_test 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 .include 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()); +}