* [Qemu-devel] [PATCH 0/4] cutils: Add qemu_strto*l() wrappers
@ 2015-07-08 1:09 Carlos L. Torres
2015-07-08 1:09 ` [Qemu-devel] [PATCH 1/4] cutils: Add qemu_strtol() wrapper Carlos L. Torres
` (4 more replies)
0 siblings, 5 replies; 8+ messages in thread
From: Carlos L. Torres @ 2015-07-08 1:09 UTC (permalink / raw
To: qemu-devel; +Cc: pbonzini, stefanha
From: "Carlos L. Torres" <carlos.torres@rackspace.com>
Introduce qemu_ wrappers for strtol/strtoul/strtoll/strtoull
C functions, ensure that errno is checked, and if NULL is
passed as the endptr argument, then whole string has to be
a valid number on the given base, otherwise return appropriate
error.
Different from the C strto*l() functions, these wrappers
return through the 'result' out parameter.
Include unit-tests for each wrapper function.
Signed-off-by: Carlos L. Torres <carlos.torres@rackspace.com>
Carlos L. Torres (4):
cutils: Add qemu_strtol() wrapper
cutils: Add qemu_strtoul() wrapper
cutils: Add qemu_strtoll() wrapper
cutils: Add qemu_strtoull() wrapper
include/qemu-common.h | 8 +
tests/test-cutils.c | 1172 +++++++++++++++++++++++++++++++++++++++++++++++++
util/cutils.c | 131 ++++++
3 files changed, 1311 insertions(+)
--
1.9.1
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Qemu-devel] [PATCH 1/4] cutils: Add qemu_strtol() wrapper
2015-07-08 1:09 [Qemu-devel] [PATCH 0/4] cutils: Add qemu_strto*l() wrappers Carlos L. Torres
@ 2015-07-08 1:09 ` Carlos L. Torres
2015-07-08 12:59 ` Paolo Bonzini
2015-07-08 1:09 ` [Qemu-devel] [PATCH 2/4] cutils: Add qemu_strtoul() wrapper Carlos L. Torres
` (3 subsequent siblings)
4 siblings, 1 reply; 8+ messages in thread
From: Carlos L. Torres @ 2015-07-08 1:09 UTC (permalink / raw
To: qemu-devel; +Cc: pbonzini, stefanha
From: "Carlos L. Torres" <carlos.torres@rackspace.com>
Add wrapper for strtol() function. Include unit tests.
Signed-off-by: Carlos L. Torres <carlos.torres@rackspace.com>
---
include/qemu-common.h | 2 +
tests/test-cutils.c | 292 ++++++++++++++++++++++++++++++++++++++++++++++++++
util/cutils.c | 58 ++++++++++
3 files changed, 352 insertions(+)
diff --git a/include/qemu-common.h b/include/qemu-common.h
index 237d654..a0ce7d8 100644
--- a/include/qemu-common.h
+++ b/include/qemu-common.h
@@ -161,6 +161,8 @@ int qemu_fls(int i);
int qemu_fdatasync(int fd);
int fcntl_setfl(int fd, int flag);
int qemu_parse_fd(const char *param);
+int qemu_strtol(const char *nptr, const char **endptr, int base,
+ long *result);
int parse_uint(const char *s, unsigned long long *value, char **endptr,
int base);
diff --git a/tests/test-cutils.c b/tests/test-cutils.c
index 2a4556d..0e8fdbf 100644
--- a/tests/test-cutils.c
+++ b/tests/test-cutils.c
@@ -226,6 +226,269 @@ static void test_parse_uint_full_correct(void)
g_assert_cmpint(i, ==, 123);
}
+static void test_qemu_strtol_correct(void)
+{
+ const char *str = "12345 foo";
+ char f = 'X';
+ const char *endptr = &f;
+ long res = 999;
+ int err;
+
+ err = qemu_strtol(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 12345);
+ g_assert(endptr == str + 5);
+}
+
+static void test_qemu_strtol_null(void)
+{
+ char f = 'X';
+ const char *endptr = &f;
+ long res = 999;
+ int err;
+
+ err = qemu_strtol(NULL, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, -EINVAL);
+ g_assert(endptr == NULL);
+}
+
+static void test_qemu_strtol_empty(void)
+{
+ const char *str = "";
+ char f = 'X';
+ const char *endptr = &f;
+ long res = 999;
+ int err;
+
+ err = qemu_strtol(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0);
+}
+
+static void test_qemu_strtol_whitespace(void)
+{
+ const char *str = " \t ";
+ char f = 'X';
+ const char *endptr = &f;
+ long res = 999;
+ int err;
+
+ err = qemu_strtol(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0);
+ g_assert(endptr == str);
+}
+
+static void test_qemu_strtol_invalid(void)
+{
+ const char *str = " xxxx \t abc";
+ char f = 'X';
+ const char *endptr = &f;
+ long res = 999;
+ int err;
+
+ err = qemu_strtol(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0);
+ g_assert(endptr == str);
+}
+
+static void test_qemu_strtol_trailing(void)
+{
+ const char *str = "123xxx";
+ char f = 'X';
+ const char *endptr = &f;
+ long res = 999;
+ int err;
+
+ err = qemu_strtol(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 123);
+ g_assert(endptr == str + 3);
+}
+
+static void test_qemu_strtol_octal(void)
+{
+ const char *str = "0123";
+ char f = 'X';
+ const char *endptr = &f;
+ long res = 999;
+ int err;
+
+ err = qemu_strtol(str, &endptr, 8, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0123);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtol_decimal(void)
+{
+ const char *str = "0123";
+ char f = 'X';
+ const char *endptr = &f;
+ long res = 999;
+ int err;
+
+ err = qemu_strtol(str, &endptr, 10, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 123);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtol_hex(void)
+{
+ const char *str = "0123";
+ char f = 'X';
+ const char *endptr = &f;
+ long res = 999;
+ int err;
+
+ err = qemu_strtol(str, &endptr, 16, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0x123);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtol_max(void)
+{
+ const char *str = g_strdup_printf("%ld", LONG_MAX);
+ char f = 'X';
+ const char *endptr = &f;
+ long res = 999;
+ int err;
+
+ err = qemu_strtol(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, LONG_MAX);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtol_overflow(void)
+{
+ const char *str = "99999999999999999999999999999999999999999999";
+ char f = 'X';
+ const char *endptr = &f;
+ long res = 999;
+ int err;
+
+ err = qemu_strtol(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, -ERANGE);
+ g_assert_cmpint(res, ==, LONG_MAX);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtol_underflow(void)
+{
+ const char *str = "-99999999999999999999999999999999999999999999";
+ char f = 'X';
+ const char *endptr = &f;
+ long res = 999;
+ int err;
+
+ err = qemu_strtol(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, -ERANGE);
+ g_assert_cmpint(res, ==, LONG_MIN);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtol_negative(void)
+{
+ const char *str = " \t -321";
+ char f = 'X';
+ const char *endptr = &f;
+ long res = 999;
+ int err;
+
+ err = qemu_strtol(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, -321);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtol_full_correct(void)
+{
+ const char *str = "123";
+ long res = 999;
+ int err;
+
+ err = qemu_strtol(str, NULL, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 123);
+}
+
+static void test_qemu_strtol_full_null(void)
+{
+ char f = 'X';
+ const char *endptr = &f;
+ long res = 999;
+ int err;
+
+ err = qemu_strtol(NULL, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, -EINVAL);
+ g_assert(endptr == NULL);
+}
+
+static void test_qemu_strtol_full_empty(void)
+{
+ const char *str = "";
+ long res = 999L;
+ int err;
+
+ err = qemu_strtol(str, NULL, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0);
+}
+
+static void test_qemu_strtol_full_negative(void)
+{
+ const char *str = " \t -321";
+ long res = 999;
+ int err;
+
+ err = qemu_strtol(str, NULL, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, -321);
+}
+
+static void test_qemu_strtol_full_trailing(void)
+{
+ const char *str = "123xxx";
+ long res;
+ int err;
+
+ err = qemu_strtol(str, NULL, 0, &res);
+
+ g_assert_cmpint(err, ==, -EINVAL);
+}
+
+static void test_qemu_strtol_full_max(void)
+{
+ const char *str = g_strdup_printf("%ld", LONG_MAX);
+ long res;
+ int err;
+
+ err = qemu_strtol(str, NULL, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, LONG_MAX);
+}
int main(int argc, char **argv)
{
g_test_init(&argc, &argv, NULL);
@@ -247,5 +510,34 @@ int main(int argc, char **argv)
g_test_add_func("/cutils/parse_uint_full/correct",
test_parse_uint_full_correct);
+ /* qemu_strtol() tests */
+ g_test_add_func("/cutils/qemu_strtol/correct", test_qemu_strtol_correct);
+ g_test_add_func("/cutils/qemu_strtol/null", test_qemu_strtol_null);
+ g_test_add_func("/cutils/qemu_strtol/empty", test_qemu_strtol_empty);
+ g_test_add_func("/cutils/qemu_strtol/whitespace",
+ test_qemu_strtol_whitespace);
+ g_test_add_func("/cutils/qemu_strtol/invalid", test_qemu_strtol_invalid);
+ g_test_add_func("/cutils/qemu_strtol/trailing", test_qemu_strtol_trailing);
+ g_test_add_func("/cutils/qemu_strtol/octal", test_qemu_strtol_octal);
+ g_test_add_func("/cutils/qemu_strtol/decimal", test_qemu_strtol_decimal);
+ g_test_add_func("/cutils/qemu_strtol/hex", test_qemu_strtol_hex);
+ g_test_add_func("/cutils/qemu_strtol/max", test_qemu_strtol_max);
+ g_test_add_func("/cutils/qemu_strtol/overflow", test_qemu_strtol_overflow);
+ g_test_add_func("/cutils/qemu_strtol/underflow",
+ test_qemu_strtol_underflow);
+ g_test_add_func("/cutils/qemu_strtol/negative", test_qemu_strtol_negative);
+ g_test_add_func("/cutils/qemu_strtol_full/correct",
+ test_qemu_strtol_full_correct);
+ g_test_add_func("/cutils/qemu_strtol_full/null",
+ test_qemu_strtol_full_null);
+ g_test_add_func("/cutils/qemu_strtol_full/empty",
+ test_qemu_strtol_full_empty);
+ g_test_add_func("/cutils/qemu_strtol_full/negative",
+ test_qemu_strtol_full_negative);
+ g_test_add_func("/cutils/qemu_strtol_full/trailing",
+ test_qemu_strtol_full_trailing);
+ g_test_add_func("/cutils/qemu_strtol_full/max",
+ test_qemu_strtol_full_max);
+
return g_test_run();
}
diff --git a/util/cutils.c b/util/cutils.c
index 5d1c9eb..bfaccd3 100644
--- a/util/cutils.c
+++ b/util/cutils.c
@@ -359,6 +359,64 @@ int64_t strtosz(const char *nptr, char **end)
}
/**
+ * Helper function for qemu_strto*l() functions.
+ */
+static int check_strtox_error(const char **next, char *endptr,
+ int err)
+{
+ if (!next && *endptr) {
+ return -EINVAL;
+ }
+ if (next) {
+ *next = endptr;
+ }
+ return -err;
+}
+
+/**
+ * QEMU wrappers for strtol(), strtoll(), strtoul(), strotull() C functions.
+ *
+ * Convert ASCII string @nptr to a long integer value
+ * from the given @base. Parameters @nptr, @endptr, @base
+ * follows same semantics as strtol() C function.
+ *
+ * Different from strtol() function, if @endptr is not NULL, this
+ * function will return -EINVAL whenever it cannot fully convert
+ * the string in @nptr with given @base to a long. This function returns
+ * the result of the conversion only through the @result parameter.
+ *
+ * If NULL is passed in @endptr, then the whole string in @ntpr
+ * is a number otherwise it returns -EINVAL.
+ *
+ * RETURN VALUE
+ * Different from strtol() function, this wrapper returns either
+ * -EINVAL or the errno set by strtol() function (e.g -ERANGE).
+ * If the conversion overflows, -ERANGE is returned, and @result
+ * is set to the max value of the desired type
+ * (e.g. LONG_MAX, LLONG_MAX, ULONG_MAX, ULLONG_MAX). If the case
+ * of underflow, -ERANGE is returned, and @result is set to the min
+ * value of the desired type. For strtol(), strtoll(), @result is set to
+ * LONG_MIN, LLONG_MIN, respectively, and for strtoul(), strtoull() it
+ * is set to 0.
+ */
+int qemu_strtol(const char *nptr, const char **endptr, int base,
+ long *result)
+{
+ char *p;
+ int err = 0;
+ if (!nptr) {
+ if (endptr) {
+ *endptr = nptr;
+ }
+ err = -EINVAL;
+ } else {
+ errno = 0;
+ *result = strtol(nptr, &p, base);
+ err = check_strtox_error(endptr, p, errno);
+ }
+ return err;
+}
+/**
* parse_uint:
*
* @s: String to parse
--
1.9.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [Qemu-devel] [PATCH 2/4] cutils: Add qemu_strtoul() wrapper
2015-07-08 1:09 [Qemu-devel] [PATCH 0/4] cutils: Add qemu_strto*l() wrappers Carlos L. Torres
2015-07-08 1:09 ` [Qemu-devel] [PATCH 1/4] cutils: Add qemu_strtol() wrapper Carlos L. Torres
@ 2015-07-08 1:09 ` Carlos L. Torres
2015-07-08 13:01 ` Paolo Bonzini
2015-07-08 1:09 ` [Qemu-devel] [PATCH 3/4] cutils: Add qemu_strtoll() wrapper Carlos L. Torres
` (2 subsequent siblings)
4 siblings, 1 reply; 8+ messages in thread
From: Carlos L. Torres @ 2015-07-08 1:09 UTC (permalink / raw
To: qemu-devel; +Cc: pbonzini, stefanha
From: "Carlos L. Torres" <carlos.torres@rackspace.com>
Add wrapper for strtoul() function. Include unit tests.
Signed-off-by: Carlos L. Torres <carlos.torres@rackspace.com>
---
include/qemu-common.h | 2 +
tests/test-cutils.c | 291 ++++++++++++++++++++++++++++++++++++++++++++++++++
util/cutils.c | 27 +++++
3 files changed, 320 insertions(+)
diff --git a/include/qemu-common.h b/include/qemu-common.h
index a0ce7d8..28bd7d2 100644
--- a/include/qemu-common.h
+++ b/include/qemu-common.h
@@ -163,6 +163,8 @@ int fcntl_setfl(int fd, int flag);
int qemu_parse_fd(const char *param);
int qemu_strtol(const char *nptr, const char **endptr, int base,
long *result);
+int qemu_strtoul(const char *nptr, const char **endptr, int base,
+ unsigned long *result);
int parse_uint(const char *s, unsigned long long *value, char **endptr,
int base);
diff --git a/tests/test-cutils.c b/tests/test-cutils.c
index 0e8fdbf..3c6bd68 100644
--- a/tests/test-cutils.c
+++ b/tests/test-cutils.c
@@ -489,6 +489,265 @@ static void test_qemu_strtol_full_max(void)
g_assert_cmpint(err, ==, 0);
g_assert_cmpint(res, ==, LONG_MAX);
}
+
+static void test_qemu_strtoul_correct(void)
+{
+ const char *str = "12345 foo";
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long res = 999;
+ int err;
+
+ err = qemu_strtoul(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 12345);
+ g_assert(endptr == str + 5);
+}
+
+static void test_qemu_strtoul_null(void)
+{
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long res = 999;
+ int err;
+
+ err = qemu_strtoul(NULL, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, -EINVAL);
+ g_assert(endptr == NULL);
+}
+
+static void test_qemu_strtoul_empty(void)
+{
+ const char *str = "";
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long res = 999;
+ int err;
+
+ err = qemu_strtoul(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0);
+}
+
+static void test_qemu_strtoul_whitespace(void)
+{
+ const char *str = " \t ";
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long res = 999;
+ int err;
+
+ err = qemu_strtoul(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0);
+ g_assert(endptr == str);
+}
+
+static void test_qemu_strtoul_invalid(void)
+{
+ const char *str = " xxxx \t abc";
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long res = 999;
+ int err;
+
+ err = qemu_strtoul(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert(endptr == str);
+}
+
+static void test_qemu_strtoul_trailing(void)
+{
+ const char *str = "123xxx";
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long res = 999;
+ int err;
+
+ err = qemu_strtoul(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 123);
+ g_assert(endptr == str + 3);
+}
+
+static void test_qemu_strtoul_octal(void)
+{
+ const char *str = "0123";
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long res = 999;
+ int err;
+
+ err = qemu_strtoul(str, &endptr, 8, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0123);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtoul_decimal(void)
+{
+ const char *str = "0123";
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long res = 999;
+ int err;
+
+ err = qemu_strtoul(str, &endptr, 10, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 123);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtoul_hex(void)
+{
+ const char *str = "0123";
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long res = 999;
+ int err;
+
+ err = qemu_strtoul(str, &endptr, 16, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0x123);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtoul_max(void)
+{
+ const char *str = g_strdup_printf("%lu", ULONG_MAX);
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long res = 999;
+ int err;
+
+ err = qemu_strtoul(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, ULONG_MAX);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtoul_overflow(void)
+{
+ const char *str = "99999999999999999999999999999999999999999999";
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long res = 999;
+ int err;
+
+ err = qemu_strtoul(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, -ERANGE);
+ g_assert_cmpint(res, ==, ULONG_MAX);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtoul_underflow(void)
+{
+ const char *str = "-99999999999999999999999999999999999999999999";
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long res = 999;
+ int err;
+
+ err = qemu_strtoul(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, -ERANGE);
+ g_assert_cmpint(res, ==, -1);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtoul_negative(void)
+{
+ const char *str = " \t -321";
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long res = 999;
+ int err;
+
+ err = qemu_strtoul(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, (unsigned long) -321);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtoul_full_correct(void)
+{
+ const char *str = "123";
+ unsigned long res = 999;
+ int err;
+
+ err = qemu_strtoul(str, NULL, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 123);
+}
+
+static void test_qemu_strtoul_full_null(void)
+{
+ unsigned long res = 999;
+ int err;
+
+ err = qemu_strtoul(NULL, NULL, 0, &res);
+
+ g_assert_cmpint(err, ==, -EINVAL);
+}
+
+static void test_qemu_strtoul_full_empty(void)
+{
+ const char *str = "";
+ unsigned long res = 999;
+ int err;
+
+ err = qemu_strtoul(str, NULL, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0);
+}
+static void test_qemu_strtoul_full_negative(void)
+{
+ const char *str = " \t -321";
+ unsigned long res = 999;
+ int err;
+
+ err = qemu_strtoul(str, NULL, 0, &res);
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 18446744073709551295LU);
+}
+
+static void test_qemu_strtoul_full_trailing(void)
+{
+ const char *str = "123xxx";
+ unsigned long res;
+ int err;
+
+ err = qemu_strtoul(str, NULL, 0, &res);
+
+ g_assert_cmpint(err, ==, -EINVAL);
+}
+
+static void test_qemu_strtoul_full_max(void)
+{
+ const char *str = g_strdup_printf("%lu", ULONG_MAX);
+ unsigned long res = 999;
+ int err;
+
+ err = qemu_strtoul(str, NULL, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, ULONG_MAX);
+}
+
int main(int argc, char **argv)
{
g_test_init(&argc, &argv, NULL);
@@ -539,5 +798,37 @@ int main(int argc, char **argv)
g_test_add_func("/cutils/qemu_strtol_full/max",
test_qemu_strtol_full_max);
+ /* qemu_strtoul() tests */
+ g_test_add_func("/cutils/qemu_strtoul/correct", test_qemu_strtoul_correct);
+ g_test_add_func("/cutils/qemu_strtoul/null", test_qemu_strtoul_null);
+ g_test_add_func("/cutils/qemu_strtoul/empty", test_qemu_strtoul_empty);
+ g_test_add_func("/cutils/qemu_strtoul/whitespace",
+ test_qemu_strtoul_whitespace);
+ g_test_add_func("/cutils/qemu_strtoul/invalid", test_qemu_strtoul_invalid);
+ g_test_add_func("/cutils/qemu_strtoul/trailing",
+ test_qemu_strtoul_trailing);
+ g_test_add_func("/cutils/qemu_strtoul/octal", test_qemu_strtoul_octal);
+ g_test_add_func("/cutils/qemu_strtoul/decimal", test_qemu_strtoul_decimal);
+ g_test_add_func("/cutils/qemu_strtoul/hex", test_qemu_strtoul_hex);
+ g_test_add_func("/cutils/qemu_strtoul/max", test_qemu_strtoul_max);
+ g_test_add_func("/cutils/qemu_strtoul/overflow",
+ test_qemu_strtoul_overflow);
+ g_test_add_func("/cutils/qemu_strtoul/underflow",
+ test_qemu_strtoul_underflow);
+ g_test_add_func("/cutils/qemu_strtoul/negative",
+ test_qemu_strtoul_negative);
+ g_test_add_func("/cutils/qemu_strtoul_full/correct",
+ test_qemu_strtoul_full_correct);
+ g_test_add_func("/cutils/qemu_strtoul_full/null",
+ test_qemu_strtoul_full_null);
+ g_test_add_func("/cutils/qemu_strtoul_full/empty",
+ test_qemu_strtoul_full_empty);
+ g_test_add_func("/cutils/qemu_strtoul_full/negative",
+ test_qemu_strtoul_full_negative);
+ g_test_add_func("/cutils/qemu_strtoul_full/trailing",
+ test_qemu_strtoul_full_trailing);
+ g_test_add_func("/cutils/qemu_strtoul_full/max",
+ test_qemu_strtoul_full_max);
+
return g_test_run();
}
diff --git a/util/cutils.c b/util/cutils.c
index bfaccd3..b2656f0 100644
--- a/util/cutils.c
+++ b/util/cutils.c
@@ -416,6 +416,33 @@ int qemu_strtol(const char *nptr, const char **endptr, int base,
}
return err;
}
+
+/**
+ * Converts ASCII string to an unsigned long integer.
+ *
+ * If string contains a negative number, value will be converted to
+ * the unsigned representation of the signed value.
+ *
+ * See qemu_strtol() documentation for more info.
+ */
+int qemu_strtoul(const char *nptr, const char **endptr, int base,
+ unsigned long *result)
+{
+ char *p;
+ int err = 0;
+ if (!nptr) {
+ if (endptr) {
+ *endptr = nptr;
+ }
+ err = -EINVAL;
+ } else {
+ errno = 0;
+ *result = strtoul(nptr, &p, base);
+ err = check_strtox_error(endptr, p, errno);
+ }
+ return err;
+}
+
/**
* parse_uint:
*
--
1.9.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [Qemu-devel] [PATCH 3/4] cutils: Add qemu_strtoll() wrapper
2015-07-08 1:09 [Qemu-devel] [PATCH 0/4] cutils: Add qemu_strto*l() wrappers Carlos L. Torres
2015-07-08 1:09 ` [Qemu-devel] [PATCH 1/4] cutils: Add qemu_strtol() wrapper Carlos L. Torres
2015-07-08 1:09 ` [Qemu-devel] [PATCH 2/4] cutils: Add qemu_strtoul() wrapper Carlos L. Torres
@ 2015-07-08 1:09 ` Carlos L. Torres
2015-07-08 1:09 ` [Qemu-devel] [PATCH 4/4] cutils: Add qemu_strtoull() wrapper Carlos L. Torres
2015-07-08 13:02 ` [Qemu-devel] [PATCH 0/4] cutils: Add qemu_strto*l() wrappers Paolo Bonzini
4 siblings, 0 replies; 8+ messages in thread
From: Carlos L. Torres @ 2015-07-08 1:09 UTC (permalink / raw
To: qemu-devel; +Cc: pbonzini, stefanha
From: "Carlos L. Torres" <carlos.torres@rackspace.com>
Add wrapper for strtoll() function. Include unit tests.
Signed-off-by: Carlos L. Torres <carlos.torres@rackspace.com>
---
include/qemu-common.h | 2 +
tests/test-cutils.c | 293 ++++++++++++++++++++++++++++++++++++++++++++++++++
util/cutils.c | 23 ++++
3 files changed, 318 insertions(+)
diff --git a/include/qemu-common.h b/include/qemu-common.h
index 28bd7d2..6da87f7 100644
--- a/include/qemu-common.h
+++ b/include/qemu-common.h
@@ -165,6 +165,8 @@ int qemu_strtol(const char *nptr, const char **endptr, int base,
long *result);
int qemu_strtoul(const char *nptr, const char **endptr, int base,
unsigned long *result);
+int qemu_strtoll(const char *nptr, const char **endptr, int base,
+ long long *result);
int parse_uint(const char *s, unsigned long long *value, char **endptr,
int base);
diff --git a/tests/test-cutils.c b/tests/test-cutils.c
index 3c6bd68..c3bab43 100644
--- a/tests/test-cutils.c
+++ b/tests/test-cutils.c
@@ -748,6 +748,267 @@ static void test_qemu_strtoul_full_max(void)
g_assert_cmpint(res, ==, ULONG_MAX);
}
+static void test_qemu_strtoll_correct(void)
+{
+ const char *str = "12345 foo";
+ char f = 'X';
+ const char *endptr = &f;
+ long long res = 999;
+ int err;
+
+ err = qemu_strtoll(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 12345);
+ g_assert(endptr == str + 5);
+}
+
+static void test_qemu_strtoll_null(void)
+{
+ char f = 'X';
+ const char *endptr = &f;
+ long long res = 999;
+ int err;
+
+ err = qemu_strtoll(NULL, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, -EINVAL);
+ g_assert(endptr == NULL);
+}
+
+static void test_qemu_strtoll_empty(void)
+{
+ const char *str = "";
+ char f = 'X';
+ const char *endptr = &f;
+ long long res = 999;
+ int err;
+
+ err = qemu_strtoll(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0);
+}
+
+static void test_qemu_strtoll_whitespace(void)
+{
+ const char *str = " \t ";
+ char f = 'X';
+ const char *endptr = &f;
+ long long res = 999;
+ int err;
+
+ err = qemu_strtoll(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0);
+ g_assert(endptr == str);
+}
+
+static void test_qemu_strtoll_invalid(void)
+{
+ const char *str = " xxxx \t abc";
+ char f = 'X';
+ const char *endptr = &f;
+ long long res = 999;
+ int err;
+
+ err = qemu_strtoll(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert(endptr == str);
+}
+
+static void test_qemu_strtoll_trailing(void)
+{
+ const char *str = "123xxx";
+ char f = 'X';
+ const char *endptr = &f;
+ long long res = 999;
+ int err;
+
+ err = qemu_strtoll(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 123);
+ g_assert(endptr == str + 3);
+}
+
+static void test_qemu_strtoll_octal(void)
+{
+ const char *str = "0123";
+ char f = 'X';
+ const char *endptr = &f;
+ long long res = 999;
+ int err;
+
+ err = qemu_strtoll(str, &endptr, 8, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0123);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtoll_decimal(void)
+{
+ const char *str = "0123";
+ char f = 'X';
+ const char *endptr = &f;
+ long long res = 999;
+ int err;
+
+ err = qemu_strtoll(str, &endptr, 10, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 123);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtoll_hex(void)
+{
+ const char *str = "0123";
+ char f = 'X';
+ const char *endptr = &f;
+ long long res = 999;
+ int err;
+
+ err = qemu_strtoll(str, &endptr, 16, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0x123);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtoll_max(void)
+{
+ const char *str = g_strdup_printf("%lld", LLONG_MAX);
+ char f = 'X';
+ const char *endptr = &f;
+ long long res = 999;
+ int err;
+
+ err = qemu_strtoll(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, LLONG_MAX);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtoll_overflow(void)
+{
+ const char *str = "99999999999999999999999999999999999999999999";
+ char f = 'X';
+ const char *endptr = &f;
+ long long res = 999;
+ int err;
+
+ err = qemu_strtoll(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, -ERANGE);
+ g_assert_cmpint(res, ==, LLONG_MAX);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtoll_underflow(void)
+{
+ const char *str = "-99999999999999999999999999999999999999999999";
+ char f = 'X';
+ const char *endptr = &f;
+ long long res = 999;
+ int err;
+
+ err = qemu_strtoll(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, -ERANGE);
+ g_assert_cmpint(res, ==, LLONG_MIN);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtoll_negative(void)
+{
+ const char *str = " \t -321";
+ char f = 'X';
+ const char *endptr = &f;
+ long long res = 999;
+ int err;
+
+ err = qemu_strtoll(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, -321);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtoll_full_correct(void)
+{
+ const char *str = "123";
+ long long res = 999;
+ int err;
+
+ err = qemu_strtoll(str, NULL, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 123);
+}
+
+static void test_qemu_strtoll_full_null(void)
+{
+ long long res = 999;
+ int err;
+
+ err = qemu_strtoll(NULL, NULL, 0, &res);
+
+ g_assert_cmpint(err, ==, -EINVAL);
+}
+
+static void test_qemu_strtoll_full_empty(void)
+{
+ const char *str = "";
+ long long res = 999;
+ int err;
+
+ err = qemu_strtoll(str, NULL, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0);
+}
+
+static void test_qemu_strtoll_full_negative(void)
+{
+ const char *str = " \t -321";
+ long long res = 999;
+ int err;
+
+ err = qemu_strtoll(str, NULL, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, -321);
+}
+
+static void test_qemu_strtoll_full_trailing(void)
+{
+ const char *str = "123xxx";
+ long long res = 999;
+ int err;
+
+ err = qemu_strtoll(str, NULL, 0, &res);
+
+ g_assert_cmpint(err, ==, -EINVAL);
+}
+
+static void test_qemu_strtoll_full_max(void)
+{
+
+ const char *str = g_strdup_printf("%lld", LLONG_MAX);
+ long long res;
+ int err;
+
+ err = qemu_strtoll(str, NULL, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, LLONG_MAX);
+}
+
int main(int argc, char **argv)
{
g_test_init(&argc, &argv, NULL);
@@ -830,5 +1091,37 @@ int main(int argc, char **argv)
g_test_add_func("/cutils/qemu_strtoul_full/max",
test_qemu_strtoul_full_max);
+ /* qemu_strtoll() tests */
+ g_test_add_func("/cutils/qemu_strtoll/correct", test_qemu_strtoll_correct);
+ g_test_add_func("/cutils/qemu_strtoll/null", test_qemu_strtoll_null);
+ g_test_add_func("/cutils/qemu_strtoll/empty", test_qemu_strtoll_empty);
+ g_test_add_func("/cutils/qemu_strtoll/whitespace",
+ test_qemu_strtoll_whitespace);
+ g_test_add_func("/cutils/qemu_strtoll/invalid", test_qemu_strtoll_invalid);
+ g_test_add_func("/cutils/qemu_strtoll/trailing",
+ test_qemu_strtoll_trailing);
+ g_test_add_func("/cutils/qemu_strtoll/octal", test_qemu_strtoll_octal);
+ g_test_add_func("/cutils/qemu_strtoll/decimal", test_qemu_strtoll_decimal);
+ g_test_add_func("/cutils/qemu_strtoll/hex", test_qemu_strtoll_hex);
+ g_test_add_func("/cutils/qemu_strtoll/max", test_qemu_strtoll_max);
+ g_test_add_func("/cutils/qemu_strtoll/overflow",
+ test_qemu_strtoll_overflow);
+ g_test_add_func("/cutils/qemu_strtoll/underflow",
+ test_qemu_strtoll_underflow);
+ g_test_add_func("/cutils/qemu_strtoll/negative",
+ test_qemu_strtoll_negative);
+ g_test_add_func("/cutils/qemu_strtoll_full/correct",
+ test_qemu_strtoll_full_correct);
+ g_test_add_func("/cutils/qemu_strtoll_full/null",
+ test_qemu_strtoll_full_null);
+ g_test_add_func("/cutils/qemu_strtoll_full/empty",
+ test_qemu_strtoll_full_empty);
+ g_test_add_func("/cutils/qemu_strtoll_full/negative",
+ test_qemu_strtoll_full_negative);
+ g_test_add_func("/cutils/qemu_strtoll_full/trailing",
+ test_qemu_strtoll_full_trailing);
+ g_test_add_func("/cutils/qemu_strtoll_full/max",
+ test_qemu_strtoll_full_max);
+
return g_test_run();
}
diff --git a/util/cutils.c b/util/cutils.c
index b2656f0..2d9f5a9 100644
--- a/util/cutils.c
+++ b/util/cutils.c
@@ -444,6 +444,29 @@ int qemu_strtoul(const char *nptr, const char **endptr, int base,
}
/**
+ * Converts ASCII string to a long long integer.
+ *
+ * See qemu_strtol() documentation for more info.
+ */
+int qemu_strtoll(const char *nptr, const char **endptr, int base,
+ long long *result)
+{
+ char *p;
+ int err = 0;
+ if (!nptr) {
+ if (endptr) {
+ *endptr = nptr;
+ }
+ err = -EINVAL;
+ } else {
+ errno = 0;
+ *result = strtoll(nptr, &p, base);
+ err = check_strtox_error(endptr, p, errno);
+ }
+ return err;
+}
+
+/**
* parse_uint:
*
* @s: String to parse
--
1.9.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [Qemu-devel] [PATCH 4/4] cutils: Add qemu_strtoull() wrapper
2015-07-08 1:09 [Qemu-devel] [PATCH 0/4] cutils: Add qemu_strto*l() wrappers Carlos L. Torres
` (2 preceding siblings ...)
2015-07-08 1:09 ` [Qemu-devel] [PATCH 3/4] cutils: Add qemu_strtoll() wrapper Carlos L. Torres
@ 2015-07-08 1:09 ` Carlos L. Torres
2015-07-08 13:02 ` [Qemu-devel] [PATCH 0/4] cutils: Add qemu_strto*l() wrappers Paolo Bonzini
4 siblings, 0 replies; 8+ messages in thread
From: Carlos L. Torres @ 2015-07-08 1:09 UTC (permalink / raw
To: qemu-devel; +Cc: pbonzini, stefanha
From: "Carlos L. Torres" <carlos.torres@rackspace.com>
Add wrapper for strtoull() function. Include unit tests.
Signed-off-by: Carlos L. Torres <carlos.torres@rackspace.com>
---
include/qemu-common.h | 2 +
tests/test-cutils.c | 296 ++++++++++++++++++++++++++++++++++++++++++++++++++
util/cutils.c | 23 ++++
3 files changed, 321 insertions(+)
diff --git a/include/qemu-common.h b/include/qemu-common.h
index 6da87f7..421a47b 100644
--- a/include/qemu-common.h
+++ b/include/qemu-common.h
@@ -167,6 +167,8 @@ int qemu_strtoul(const char *nptr, const char **endptr, int base,
unsigned long *result);
int qemu_strtoll(const char *nptr, const char **endptr, int base,
long long *result);
+int qemu_strtoull(const char *nptr, const char **endptr, int base,
+ unsigned long long *result);
int parse_uint(const char *s, unsigned long long *value, char **endptr,
int base);
diff --git a/tests/test-cutils.c b/tests/test-cutils.c
index c3bab43..30c173b 100644
--- a/tests/test-cutils.c
+++ b/tests/test-cutils.c
@@ -1009,6 +1009,266 @@ static void test_qemu_strtoll_full_max(void)
g_assert_cmpint(res, ==, LLONG_MAX);
}
+static void test_qemu_strtoull_correct(void)
+{
+ const char *str = "12345 foo";
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long long res = 999;
+ int err;
+
+ err = qemu_strtoull(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 12345);
+ g_assert(endptr == str + 5);
+}
+
+static void test_qemu_strtoull_null(void)
+{
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long long res = 999;
+ int err;
+
+ err = qemu_strtoull(NULL, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, -EINVAL);
+ g_assert(endptr == NULL);
+}
+
+static void test_qemu_strtoull_empty(void)
+{
+ const char *str = "";
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long long res = 999;
+ int err;
+
+ err = qemu_strtoull(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0);
+}
+
+static void test_qemu_strtoull_whitespace(void)
+{
+ const char *str = " \t ";
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long long res = 999;
+ int err;
+
+ err = qemu_strtoull(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0);
+ g_assert(endptr == str);
+}
+
+static void test_qemu_strtoull_invalid(void)
+{
+ const char *str = " xxxx \t abc";
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long long res = 999;
+ int err;
+
+ err = qemu_strtoull(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert(endptr == str);
+}
+
+static void test_qemu_strtoull_trailing(void)
+{
+ const char *str = "123xxx";
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long long res = 999;
+ int err;
+
+ err = qemu_strtoull(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 123);
+ g_assert(endptr == str + 3);
+}
+
+static void test_qemu_strtoull_octal(void)
+{
+ const char *str = "0123";
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long long res = 999;
+ int err;
+
+ err = qemu_strtoull(str, &endptr, 8, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0123);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtoull_decimal(void)
+{
+ const char *str = "0123";
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long long res = 999;
+ int err;
+
+ err = qemu_strtoull(str, &endptr, 10, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 123);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtoull_hex(void)
+{
+ const char *str = "0123";
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long long res = 999;
+ int err;
+
+ err = qemu_strtoull(str, &endptr, 16, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0x123);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtoull_max(void)
+{
+ const char *str = g_strdup_printf("%llu", ULLONG_MAX);
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long long res = 999;
+ int err;
+
+ err = qemu_strtoull(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, ULLONG_MAX);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtoull_overflow(void)
+{
+ const char *str = "99999999999999999999999999999999999999999999";
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long long res = 999;
+ int err;
+
+ err = qemu_strtoull(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, -ERANGE);
+ g_assert_cmpint(res, ==, ULLONG_MAX);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtoull_underflow(void)
+{
+ const char *str = "-99999999999999999999999999999999999999999999";
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long long res = 999;
+ int err;
+
+ err = qemu_strtoull(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, -ERANGE);
+ g_assert_cmpint(res, ==, -1);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtoull_negative(void)
+{
+ const char *str = " \t -321";
+ char f = 'X';
+ const char *endptr = &f;
+ unsigned long long res = 999;
+ int err;
+
+ err = qemu_strtoull(str, &endptr, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, -321);
+ g_assert(endptr == str + strlen(str));
+}
+
+static void test_qemu_strtoull_full_correct(void)
+{
+ const char *str = "18446744073709551614";
+ unsigned long long res = 999;
+ int err;
+
+ err = qemu_strtoull(str, NULL, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 18446744073709551614LLU);
+}
+
+static void test_qemu_strtoull_full_null(void)
+{
+ unsigned long long res = 999;
+ int err;
+
+ err = qemu_strtoull(NULL, NULL, 0, &res);
+
+ g_assert_cmpint(err, ==, -EINVAL);
+}
+
+static void test_qemu_strtoull_full_empty(void)
+{
+ const char *str = "";
+ unsigned long long res = 999;
+ int err;
+
+ err = qemu_strtoull(str, NULL, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 0);
+}
+
+static void test_qemu_strtoull_full_negative(void)
+{
+ const char *str = " \t -321";
+ unsigned long long res = 999;
+ int err;
+
+ err = qemu_strtoull(str, NULL, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, 18446744073709551295LLU);
+}
+
+static void test_qemu_strtoull_full_trailing(void)
+{
+ const char *str = "18446744073709551614xxxxxx";
+ unsigned long long res = 999;
+ int err;
+
+ err = qemu_strtoull(str, NULL, 0, &res);
+
+ g_assert_cmpint(err, ==, -EINVAL);
+}
+
+static void test_qemu_strtoull_full_max(void)
+{
+ const char *str = g_strdup_printf("%lld", ULLONG_MAX);
+ unsigned long long res = 999;
+ int err;
+
+ err = qemu_strtoull(str, NULL, 0, &res);
+
+ g_assert_cmpint(err, ==, 0);
+ g_assert_cmpint(res, ==, ULLONG_MAX);
+}
+
int main(int argc, char **argv)
{
g_test_init(&argc, &argv, NULL);
@@ -1123,5 +1383,41 @@ int main(int argc, char **argv)
g_test_add_func("/cutils/qemu_strtoll_full/max",
test_qemu_strtoll_full_max);
+ /* qemu_strtoull() tests */
+ g_test_add_func("/cutils/qemu_strtoull/correct",
+ test_qemu_strtoull_correct);
+ g_test_add_func("/cutils/qemu_strtoull/null",
+ test_qemu_strtoull_null);
+ g_test_add_func("/cutils/qemu_strtoull/empty", test_qemu_strtoull_empty);
+ g_test_add_func("/cutils/qemu_strtoull/whitespace",
+ test_qemu_strtoull_whitespace);
+ g_test_add_func("/cutils/qemu_strtoull/invalid",
+ test_qemu_strtoull_invalid);
+ g_test_add_func("/cutils/qemu_strtoull/trailing",
+ test_qemu_strtoull_trailing);
+ g_test_add_func("/cutils/qemu_strtoull/octal", test_qemu_strtoull_octal);
+ g_test_add_func("/cutils/qemu_strtoull/decimal",
+ test_qemu_strtoull_decimal);
+ g_test_add_func("/cutils/qemu_strtoull/hex", test_qemu_strtoull_hex);
+ g_test_add_func("/cutils/qemu_strtoull/max", test_qemu_strtoull_max);
+ g_test_add_func("/cutils/qemu_strtoull/overflow",
+ test_qemu_strtoull_overflow);
+ g_test_add_func("/cutils/qemu_strtoull/underflow",
+ test_qemu_strtoull_underflow);
+ g_test_add_func("/cutils/qemu_strtoull/negative",
+ test_qemu_strtoull_negative);
+ g_test_add_func("/cutils/qemu_strtoull_full/correct",
+ test_qemu_strtoull_full_correct);
+ g_test_add_func("/cutils/qemu_strtoull_full/null",
+ test_qemu_strtoull_full_null);
+ g_test_add_func("/cutils/qemu_strtoull_full/empty",
+ test_qemu_strtoull_full_empty);
+ g_test_add_func("/cutils/qemu_strtoull_full/negative",
+ test_qemu_strtoull_full_negative);
+ g_test_add_func("/cutils/qemu_strtoull_full/trailing",
+ test_qemu_strtoull_full_trailing);
+ g_test_add_func("/cutils/qemu_strtoull_full/max",
+ test_qemu_strtoull_full_max);
+
return g_test_run();
}
diff --git a/util/cutils.c b/util/cutils.c
index 2d9f5a9..65aa524 100644
--- a/util/cutils.c
+++ b/util/cutils.c
@@ -467,6 +467,29 @@ int qemu_strtoll(const char *nptr, const char **endptr, int base,
}
/**
+ * Converts ASCII string to an unsigned long long integer.
+ *
+ * See qemu_strtol() documentation for more info.
+ */
+int qemu_strtoull(const char *nptr, const char **endptr, int base,
+ unsigned long long *result)
+{
+ char *p;
+ int err = 0;
+ if (!nptr) {
+ if (endptr) {
+ *endptr = nptr;
+ }
+ err = -EINVAL;
+ } else {
+ errno = 0;
+ *result = strtoull(nptr, &p, base);
+ err = check_strtox_error(endptr, p, errno);
+ }
+ return err;
+}
+
+/**
* parse_uint:
*
* @s: String to parse
--
1.9.1
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [Qemu-devel] [PATCH 1/4] cutils: Add qemu_strtol() wrapper
2015-07-08 1:09 ` [Qemu-devel] [PATCH 1/4] cutils: Add qemu_strtol() wrapper Carlos L. Torres
@ 2015-07-08 12:59 ` Paolo Bonzini
0 siblings, 0 replies; 8+ messages in thread
From: Paolo Bonzini @ 2015-07-08 12:59 UTC (permalink / raw
To: Carlos L. Torres, qemu-devel; +Cc: stefanha
On 08/07/2015 03:09, Carlos L. Torres wrote:
> From: "Carlos L. Torres" <carlos.torres@rackspace.com>
>
> Add wrapper for strtol() function. Include unit tests.
>
> Signed-off-by: Carlos L. Torres <carlos.torres@rackspace.com>
Very thorough work, just a couple comments below.
> ---
> include/qemu-common.h | 2 +
> tests/test-cutils.c | 292 ++++++++++++++++++++++++++++++++++++++++++++++++++
> util/cutils.c | 58 ++++++++++
> 3 files changed, 352 insertions(+)
>
> diff --git a/include/qemu-common.h b/include/qemu-common.h
> index 237d654..a0ce7d8 100644
> --- a/include/qemu-common.h
> +++ b/include/qemu-common.h
> @@ -161,6 +161,8 @@ int qemu_fls(int i);
> int qemu_fdatasync(int fd);
> int fcntl_setfl(int fd, int flag);
> int qemu_parse_fd(const char *param);
> +int qemu_strtol(const char *nptr, const char **endptr, int base,
> + long *result);
>
> int parse_uint(const char *s, unsigned long long *value, char **endptr,
> int base);
> diff --git a/tests/test-cutils.c b/tests/test-cutils.c
> index 2a4556d..0e8fdbf 100644
> --- a/tests/test-cutils.c
> +++ b/tests/test-cutils.c
> @@ -226,6 +226,269 @@ static void test_parse_uint_full_correct(void)
> g_assert_cmpint(i, ==, 123);
> }
>
> +static void test_qemu_strtol_correct(void)
> +{
> + const char *str = "12345 foo";
> + char f = 'X';
> + const char *endptr = &f;
> + long res = 999;
> + int err;
> +
> + err = qemu_strtol(str, &endptr, 0, &res);
> +
> + g_assert_cmpint(err, ==, 0);
> + g_assert_cmpint(res, ==, 12345);
> + g_assert(endptr == str + 5);
> +}
> +
> +static void test_qemu_strtol_null(void)
> +{
> + char f = 'X';
> + const char *endptr = &f;
> + long res = 999;
> + int err;
> +
> + err = qemu_strtol(NULL, &endptr, 0, &res);
> +
> + g_assert_cmpint(err, ==, -EINVAL);
> + g_assert(endptr == NULL);
> +}
> +
> +static void test_qemu_strtol_empty(void)
> +{
> + const char *str = "";
> + char f = 'X';
> + const char *endptr = &f;
> + long res = 999;
> + int err;
> +
> + err = qemu_strtol(str, &endptr, 0, &res);
> +
> + g_assert_cmpint(err, ==, 0);
> + g_assert_cmpint(res, ==, 0);
Please assert that endptr == str.
> +}
> +
> +static void test_qemu_strtol_whitespace(void)
> +{
> + const char *str = " \t ";
> + char f = 'X';
> + const char *endptr = &f;
> + long res = 999;
> + int err;
> +
> + err = qemu_strtol(str, &endptr, 0, &res);
> +
> + g_assert_cmpint(err, ==, 0);
> + g_assert_cmpint(res, ==, 0);
> + g_assert(endptr == str);
> +}
> +
> +static void test_qemu_strtol_invalid(void)
> +{
> + const char *str = " xxxx \t abc";
> + char f = 'X';
> + const char *endptr = &f;
> + long res = 999;
> + int err;
> +
> + err = qemu_strtol(str, &endptr, 0, &res);
> +
> + g_assert_cmpint(err, ==, 0);
> + g_assert_cmpint(res, ==, 0);
> + g_assert(endptr == str);
> +}
> +
> +static void test_qemu_strtol_trailing(void)
> +{
> + const char *str = "123xxx";
> + char f = 'X';
> + const char *endptr = &f;
> + long res = 999;
> + int err;
> +
> + err = qemu_strtol(str, &endptr, 0, &res);
> +
> + g_assert_cmpint(err, ==, 0);
> + g_assert_cmpint(res, ==, 123);
> + g_assert(endptr == str + 3);
> +}
> +
> +static void test_qemu_strtol_octal(void)
> +{
> + const char *str = "0123";
> + char f = 'X';
> + const char *endptr = &f;
> + long res = 999;
> + int err;
> +
> + err = qemu_strtol(str, &endptr, 8, &res);
Add a testcase for str = "0123" and base = 0?
> +
> + g_assert_cmpint(err, ==, 0);
> + g_assert_cmpint(res, ==, 0123);
> + g_assert(endptr == str + strlen(str));
> +}
> +
> +static void test_qemu_strtol_decimal(void)
> +{
> + const char *str = "0123";
> + char f = 'X';
> + const char *endptr = &f;
> + long res = 999;
> + int err;
> +
> + err = qemu_strtol(str, &endptr, 10, &res);
> +
> + g_assert_cmpint(err, ==, 0);
> + g_assert_cmpint(res, ==, 123);
> + g_assert(endptr == str + strlen(str));
> +}
> +
> +static void test_qemu_strtol_hex(void)
> +{
> + const char *str = "0123";
> + char f = 'X';
> + const char *endptr = &f;
> + long res = 999;
> + int err;
> +
> + err = qemu_strtol(str, &endptr, 16, &res);
> +
> + g_assert_cmpint(err, ==, 0);
> + g_assert_cmpint(res, ==, 0x123);
> + g_assert(endptr == str + strlen(str));
> +}
> +
> +static void test_qemu_strtol_max(void)
> +{
> + const char *str = g_strdup_printf("%ld", LONG_MAX);
> + char f = 'X';
> + const char *endptr = &f;
> + long res = 999;
> + int err;
> +
> + err = qemu_strtol(str, &endptr, 0, &res);
> +
> + g_assert_cmpint(err, ==, 0);
> + g_assert_cmpint(res, ==, LONG_MAX);
> + g_assert(endptr == str + strlen(str));
> +}
> +
> +static void test_qemu_strtol_overflow(void)
> +{
> + const char *str = "99999999999999999999999999999999999999999999";
> + char f = 'X';
> + const char *endptr = &f;
> + long res = 999;
> + int err;
> +
> + err = qemu_strtol(str, &endptr, 0, &res);
> +
> + g_assert_cmpint(err, ==, -ERANGE);
> + g_assert_cmpint(res, ==, LONG_MAX);
> + g_assert(endptr == str + strlen(str));
> +}
> +
> +static void test_qemu_strtol_underflow(void)
> +{
> + const char *str = "-99999999999999999999999999999999999999999999";
> + char f = 'X';
> + const char *endptr = &f;
> + long res = 999;
> + int err;
> +
> + err = qemu_strtol(str, &endptr, 0, &res);
> +
> + g_assert_cmpint(err, ==, -ERANGE);
> + g_assert_cmpint(res, ==, LONG_MIN);
> + g_assert(endptr == str + strlen(str));
> +}
> +
> +static void test_qemu_strtol_negative(void)
> +{
> + const char *str = " \t -321";
> + char f = 'X';
> + const char *endptr = &f;
> + long res = 999;
> + int err;
> +
> + err = qemu_strtol(str, &endptr, 0, &res);
> +
> + g_assert_cmpint(err, ==, 0);
> + g_assert_cmpint(res, ==, -321);
> + g_assert(endptr == str + strlen(str));
> +}
> +
> +static void test_qemu_strtol_full_correct(void)
> +{
> + const char *str = "123";
> + long res = 999;
> + int err;
> +
> + err = qemu_strtol(str, NULL, 0, &res);
> +
> + g_assert_cmpint(err, ==, 0);
> + g_assert_cmpint(res, ==, 123);
> +}
> +
> +static void test_qemu_strtol_full_null(void)
> +{
> + char f = 'X';
> + const char *endptr = &f;
> + long res = 999;
> + int err;
> +
> + err = qemu_strtol(NULL, &endptr, 0, &res);
> +
> + g_assert_cmpint(err, ==, -EINVAL);
> + g_assert(endptr == NULL);
> +}
> +
> +static void test_qemu_strtol_full_empty(void)
> +{
> + const char *str = "";
> + long res = 999L;
> + int err;
> +
> + err = qemu_strtol(str, NULL, 0, &res);
> +
> + g_assert_cmpint(err, ==, 0);
> + g_assert_cmpint(res, ==, 0);
> +}
> +
> +static void test_qemu_strtol_full_negative(void)
> +{
> + const char *str = " \t -321";
> + long res = 999;
> + int err;
> +
> + err = qemu_strtol(str, NULL, 0, &res);
> +
> + g_assert_cmpint(err, ==, 0);
> + g_assert_cmpint(res, ==, -321);
> +}
> +
> +static void test_qemu_strtol_full_trailing(void)
> +{
> + const char *str = "123xxx";
> + long res;
> + int err;
> +
> + err = qemu_strtol(str, NULL, 0, &res);
> +
> + g_assert_cmpint(err, ==, -EINVAL);
> +}
> +
> +static void test_qemu_strtol_full_max(void)
> +{
> + const char *str = g_strdup_printf("%ld", LONG_MAX);
> + long res;
> + int err;
> +
> + err = qemu_strtol(str, NULL, 0, &res);
> +
> + g_assert_cmpint(err, ==, 0);
> + g_assert_cmpint(res, ==, LONG_MAX);
> +}
> int main(int argc, char **argv)
> {
> g_test_init(&argc, &argv, NULL);
> @@ -247,5 +510,34 @@ int main(int argc, char **argv)
> g_test_add_func("/cutils/parse_uint_full/correct",
> test_parse_uint_full_correct);
>
> + /* qemu_strtol() tests */
> + g_test_add_func("/cutils/qemu_strtol/correct", test_qemu_strtol_correct);
> + g_test_add_func("/cutils/qemu_strtol/null", test_qemu_strtol_null);
> + g_test_add_func("/cutils/qemu_strtol/empty", test_qemu_strtol_empty);
> + g_test_add_func("/cutils/qemu_strtol/whitespace",
> + test_qemu_strtol_whitespace);
> + g_test_add_func("/cutils/qemu_strtol/invalid", test_qemu_strtol_invalid);
> + g_test_add_func("/cutils/qemu_strtol/trailing", test_qemu_strtol_trailing);
> + g_test_add_func("/cutils/qemu_strtol/octal", test_qemu_strtol_octal);
> + g_test_add_func("/cutils/qemu_strtol/decimal", test_qemu_strtol_decimal);
> + g_test_add_func("/cutils/qemu_strtol/hex", test_qemu_strtol_hex);
> + g_test_add_func("/cutils/qemu_strtol/max", test_qemu_strtol_max);
> + g_test_add_func("/cutils/qemu_strtol/overflow", test_qemu_strtol_overflow);
> + g_test_add_func("/cutils/qemu_strtol/underflow",
> + test_qemu_strtol_underflow);
> + g_test_add_func("/cutils/qemu_strtol/negative", test_qemu_strtol_negative);
> + g_test_add_func("/cutils/qemu_strtol_full/correct",
> + test_qemu_strtol_full_correct);
> + g_test_add_func("/cutils/qemu_strtol_full/null",
> + test_qemu_strtol_full_null);
> + g_test_add_func("/cutils/qemu_strtol_full/empty",
> + test_qemu_strtol_full_empty);
> + g_test_add_func("/cutils/qemu_strtol_full/negative",
> + test_qemu_strtol_full_negative);
> + g_test_add_func("/cutils/qemu_strtol_full/trailing",
> + test_qemu_strtol_full_trailing);
> + g_test_add_func("/cutils/qemu_strtol_full/max",
> + test_qemu_strtol_full_max);
> +
> return g_test_run();
> }
> diff --git a/util/cutils.c b/util/cutils.c
> index 5d1c9eb..bfaccd3 100644
> --- a/util/cutils.c
> +++ b/util/cutils.c
> @@ -359,6 +359,64 @@ int64_t strtosz(const char *nptr, char **end)
> }
>
> /**
> + * Helper function for qemu_strto*l() functions.
> + */
> +static int check_strtox_error(const char **next, char *endptr,
> + int err)
> +{
> + if (!next && *endptr) {
> + return -EINVAL;
> + }
> + if (next) {
> + *next = endptr;
> + }
> + return -err;
> +}
> +
> +/**
> + * QEMU wrappers for strtol(), strtoll(), strtoul(), strotull() C functions.
> + *
> + * Convert ASCII string @nptr to a long integer value
> + * from the given @base. Parameters @nptr, @endptr, @base
> + * follows same semantics as strtol() C function.
> + *
> + * Different from strtol() function, if @endptr is not NULL, this
"Unlike the strtol() function," ...
> + * function will return -EINVAL whenever it cannot fully convert
> + * the string in @nptr with given @base to a long. This function returns
> + * the result of the conversion only through the @result parameter.
> + *
> + * If NULL is passed in @endptr, then the whole string in @ntpr
> + * is a number otherwise it returns -EINVAL.
> + *
> + * RETURN VALUE
> + * Different from strtol() function, this wrapper returns either
Same here.
> + * -EINVAL or the errno set by strtol() function (e.g -ERANGE).
> + * If the conversion overflows, -ERANGE is returned, and @result
> + * is set to the max value of the desired type
> + * (e.g. LONG_MAX, LLONG_MAX, ULONG_MAX, ULLONG_MAX). If the case
> + * of underflow, -ERANGE is returned, and @result is set to the min
> + * value of the desired type. For strtol(), strtoll(), @result is set to
> + * LONG_MIN, LLONG_MIN, respectively, and for strtoul(), strtoull() it
> + * is set to 0.
> + */
> +int qemu_strtol(const char *nptr, const char **endptr, int base,
> + long *result)
> +{
> + char *p;
> + int err = 0;
> + if (!nptr) {
> + if (endptr) {
> + *endptr = nptr;
> + }
> + err = -EINVAL;
> + } else {
> + errno = 0;
> + *result = strtol(nptr, &p, base);
> + err = check_strtox_error(endptr, p, errno);
> + }
> + return err;
> +}
> +/**
> * parse_uint:
> *
> * @s: String to parse
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Qemu-devel] [PATCH 2/4] cutils: Add qemu_strtoul() wrapper
2015-07-08 1:09 ` [Qemu-devel] [PATCH 2/4] cutils: Add qemu_strtoul() wrapper Carlos L. Torres
@ 2015-07-08 13:01 ` Paolo Bonzini
0 siblings, 0 replies; 8+ messages in thread
From: Paolo Bonzini @ 2015-07-08 13:01 UTC (permalink / raw
To: Carlos L. Torres, qemu-devel; +Cc: stefanha
On 08/07/2015 03:09, Carlos L. Torres wrote:
> +
> +/**
> + * Converts ASCII string to an unsigned long integer.
> + *
> + * If string contains a negative number, value will be converted to
> + * the unsigned representation of the signed value.
Please document overflow here, as it is slightly different between
strtol and strtoul.
Paolo
> + * See qemu_strtol() documentation for more info.
> + */
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Qemu-devel] [PATCH 0/4] cutils: Add qemu_strto*l() wrappers
2015-07-08 1:09 [Qemu-devel] [PATCH 0/4] cutils: Add qemu_strto*l() wrappers Carlos L. Torres
` (3 preceding siblings ...)
2015-07-08 1:09 ` [Qemu-devel] [PATCH 4/4] cutils: Add qemu_strtoull() wrapper Carlos L. Torres
@ 2015-07-08 13:02 ` Paolo Bonzini
4 siblings, 0 replies; 8+ messages in thread
From: Paolo Bonzini @ 2015-07-08 13:02 UTC (permalink / raw
To: Carlos L. Torres, qemu-devel; +Cc: stefanha
On 08/07/2015 03:09, Carlos L. Torres wrote:
> From: "Carlos L. Torres" <carlos.torres@rackspace.com>
>
> Introduce qemu_ wrappers for strtol/strtoul/strtoll/strtoull
> C functions, ensure that errno is checked, and if NULL is
> passed as the endptr argument, then whole string has to be
> a valid number on the given base, otherwise return appropriate
> error.
>
> Different from the C strto*l() functions, these wrappers
> return through the 'result' out parameter.
>
> Include unit-tests for each wrapper function.
>
> Signed-off-by: Carlos L. Torres <carlos.torres@rackspace.com>
>
>
> Carlos L. Torres (4):
> cutils: Add qemu_strtol() wrapper
> cutils: Add qemu_strtoul() wrapper
> cutils: Add qemu_strtoll() wrapper
> cutils: Add qemu_strtoull() wrapper
>
> include/qemu-common.h | 8 +
> tests/test-cutils.c | 1172 +++++++++++++++++++++++++++++++++++++++++++++++++
> util/cutils.c | 131 ++++++
> 3 files changed, 1311 insertions(+)
>
This looks pretty much okay. Of course, you would include at least one
conversion from strto* to qemu_strto* (not necessarily one per function,
though one signed case and one unsigned case would be great).
Paolo
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2015-07-08 13:03 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-07-08 1:09 [Qemu-devel] [PATCH 0/4] cutils: Add qemu_strto*l() wrappers Carlos L. Torres
2015-07-08 1:09 ` [Qemu-devel] [PATCH 1/4] cutils: Add qemu_strtol() wrapper Carlos L. Torres
2015-07-08 12:59 ` Paolo Bonzini
2015-07-08 1:09 ` [Qemu-devel] [PATCH 2/4] cutils: Add qemu_strtoul() wrapper Carlos L. Torres
2015-07-08 13:01 ` Paolo Bonzini
2015-07-08 1:09 ` [Qemu-devel] [PATCH 3/4] cutils: Add qemu_strtoll() wrapper Carlos L. Torres
2015-07-08 1:09 ` [Qemu-devel] [PATCH 4/4] cutils: Add qemu_strtoull() wrapper Carlos L. Torres
2015-07-08 13:02 ` [Qemu-devel] [PATCH 0/4] cutils: Add qemu_strto*l() wrappers Paolo Bonzini
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.