Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F144783584
D51723.1776481147.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
4 KB
Referenced Files
None
Subscribers
None
D51723.1776481147.diff
View Options
diff --git a/lib/libutil/Symbol.map b/lib/libutil/Symbol.map
--- a/lib/libutil/Symbol.map
+++ b/lib/libutil/Symbol.map
@@ -13,6 +13,7 @@
cpuset_parselist;
domainset_parselist;
expand_number;
+ expand_unsigned;
flopen;
flopenat;
forkpty;
diff --git a/lib/libutil/expand_number.c b/lib/libutil/expand_number.c
--- a/lib/libutil/expand_number.c
+++ b/lib/libutil/expand_number.c
@@ -37,13 +37,12 @@
#include <stdbool.h>
#include <stdint.h>
-int
-expand_number(const char *buf, int64_t *num)
+static int
+expand_impl(const char *buf, uint64_t *num, bool *neg)
{
char *endptr;
uintmax_t number;
unsigned int shift;
- bool neg;
int serrno;
/*
@@ -52,10 +51,10 @@
while (isspace((unsigned char)*buf))
buf++;
if (*buf == '-') {
- neg = true;
+ *neg = true;
buf++;
} else {
- neg = false;
+ *neg = false;
if (*buf == '+')
buf++;
}
@@ -127,6 +126,22 @@
}
number <<= shift;
+ *num = number;
+ return (0);
+}
+
+int
+(expand_number)(const char *buf, int64_t *num)
+{
+ uint64_t number;
+ bool neg;
+
+ /*
+ * Parse the number.
+ */
+ if (expand_impl(buf, &number, &neg) != 0)
+ return (-1);
+
/*
* Apply the sign and check for overflow.
*/
@@ -146,3 +161,27 @@
return (0);
}
+
+int
+expand_unsigned(const char *buf, uint64_t *num)
+{
+ uint64_t number;
+ bool neg;
+
+ /*
+ * Parse the number.
+ */
+ if (expand_impl(buf, &number, &neg) != 0)
+ return (-1);
+
+ /*
+ * Negative numbers are out of range.
+ */
+ if (neg && number > 0) {
+ errno = ERANGE;
+ return (-1);
+ }
+
+ *num = number;
+ return (0);
+}
diff --git a/lib/libutil/libutil.h b/lib/libutil/libutil.h
--- a/lib/libutil/libutil.h
+++ b/lib/libutil/libutil.h
@@ -88,7 +88,15 @@
__BEGIN_DECLS
void clean_environment(const char * const *_white,
const char * const *_more_white);
+int expand_unsigned(const char *_buf, uint64_t *_num);
int expand_number(const char *_buf, int64_t *_num);
+#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
+ __has_extension(c_generic_selections)
+#define expand_number(_buf, _num) \
+ _Generic((_num), \
+ uint64_t *: expand_unsigned, \
+ default: expand_number)((_buf), (_num))
+#endif
int extattr_namespace_to_string(int _attrnamespace, char **_string);
int extattr_string_to_namespace(const char *_string, int *_attrnamespace);
int flopen(const char *_path, int _flags, ...);
diff --git a/lib/libutil/tests/expand_number_test.c b/lib/libutil/tests/expand_number_test.c
--- a/lib/libutil/tests/expand_number_test.c
+++ b/lib/libutil/tests/expand_number_test.c
@@ -206,10 +206,85 @@
require_error(" + 1", EINVAL);
}
+ATF_TC_WITHOUT_HEAD(expand_unsigned);
+ATF_TC_BODY(expand_unsigned, tp)
+{
+ static struct tc {
+ const char *str;
+ uint64_t num;
+ int error;
+ } tcs[] = {
+ { "0", 0, 0 },
+ { "+0", 0, 0 },
+ { "-0", 0, 0 },
+ { "1", 1, 0 },
+ { "+1", 1, 0 },
+ { "-1", 0, ERANGE },
+ { "18446744073709551615", UINT64_MAX, 0 },
+ { "+18446744073709551615", UINT64_MAX, 0 },
+ { "-18446744073709551615", 0, ERANGE },
+ { 0 },
+ };
+ struct tc *tc;
+ uint64_t num;
+ int error, ret;
+
+ for (tc = tcs; tc->str != NULL; tc++) {
+ ret = expand_number(tc->str, &num);
+ error = errno;
+ if (tc->error == 0) {
+ ATF_REQUIRE_EQ_MSG(0, ret,
+ "%s ret = %d", tc->str, ret);
+ ATF_REQUIRE_EQ_MSG(tc->num, num,
+ "%s num = %ju", tc->str, (uintmax_t)num);
+ } else {
+ ATF_REQUIRE_EQ_MSG(-1, ret,
+ "%s ret = %d", tc->str, ret);
+ ATF_REQUIRE_EQ_MSG(tc->error, error,
+ "%s errno = %d", tc->str, error);
+ }
+ }
+}
+
+ATF_TC_WITHOUT_HEAD(expand_generic);
+ATF_TC_BODY(expand_generic, tp)
+{
+ uint64_t uint64;
+ int64_t int64;
+ size_t size;
+ off_t off;
+
+ ATF_REQUIRE_EQ(0, expand_number("18446744073709551615", &uint64));
+ ATF_REQUIRE_EQ(UINT64_MAX, uint64);
+ ATF_REQUIRE_EQ(-1, expand_number("-1", &uint64));
+ ATF_REQUIRE_EQ(ERANGE, errno);
+
+ ATF_REQUIRE_EQ(0, expand_number("9223372036854775807", &int64));
+ ATF_REQUIRE_EQ(INT64_MAX, int64);
+ ATF_REQUIRE_EQ(-1, expand_number("9223372036854775808", &int64));
+ ATF_REQUIRE_EQ(ERANGE, errno);
+ ATF_REQUIRE_EQ(0, expand_number("-9223372036854775808", &int64));
+ ATF_REQUIRE_EQ(INT64_MIN, int64);
+
+ ATF_REQUIRE_EQ(0, expand_number("18446744073709551615", &size));
+ ATF_REQUIRE_EQ(UINT64_MAX, size);
+ ATF_REQUIRE_EQ(-1, expand_number("-1", &size));
+ ATF_REQUIRE_EQ(ERANGE, errno);
+
+ ATF_REQUIRE_EQ(0, expand_number("9223372036854775807", &off));
+ ATF_REQUIRE_EQ(INT64_MAX, off);
+ ATF_REQUIRE_EQ(-1, expand_number("9223372036854775808", &off));
+ ATF_REQUIRE_EQ(ERANGE, errno);
+ ATF_REQUIRE_EQ(0, expand_number("-9223372036854775808", &off));
+ ATF_REQUIRE_EQ(INT64_MIN, off);
+}
+
ATF_TP_ADD_TCS(tp)
{
ATF_TP_ADD_TC(tp, expand_number__ok);
ATF_TP_ADD_TC(tp, expand_number__bad);
+ ATF_TP_ADD_TC(tp, expand_unsigned);
+ ATF_TP_ADD_TC(tp, expand_generic);
return (atf_no_error());
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Apr 18, 2:59 AM (3 m, 12 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
28372464
Default Alt Text
D51723.1776481147.diff (4 KB)
Attached To
Mode
D51723: libutil: Backward compatibility for expand_number()
Attached
Detach File
Event Timeline
Log In to Comment