cutils: Add qemu_strto*() wrappers
v2 cover cutils: Add qemu_strto*() wrappers
From: "Carlos L. Torres" <[email protected]>
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.
Carlos L. Torres (5):
cutils: Add qemu_strtol() wrapper
cutils: Add qemu_strtoul() wrapper
cutils: Add qemu_strtoll() wrapper
cutils: Add qemu_strtoull() wrapper
qmp: Add example usage of strto*l() qemu wrapper
include/qemu-common.h | 8 + qmp.c | 21 +- tests/test-cutils.c | 1280 +++++++++++++++++++++++++++++++++++++++++++++++++ util/cutils.c | 136 ++++++ 4 files changed, 1441 insertions(+), 4 deletions(-)
Signature · 1 line
1.9.1
PATCH v2 1/5 cutils: Add qemu_strtol() wrapper
From: "Carlos L. Torres" <[email protected]>
Add wrapper for strtol() function. Include unit tests.
--- include/qemu-common.h | 2 + tests/test-cutils.c | 319 ++++++++++++++++++++++++++++++++++++++++++++++++++ util/cutils.c | 58 +++++++++ 3 files changed, 379 insertions(+)
include/qemu-common.h +2 -0
@@ -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);
tests/test-cutils.c +319 -0
330 lines not on this page · raw message
util/cutils.c +58 -0
64 lines not on this page · raw message
Signature · 1 line
1.9.1
PATCH v2 2/5 cutils: Add qemu_strtoul() wrapper
In reply to Carlos L. TorresFrom: "Carlos L. Torres" <[email protected]>
Add wrapper for strtoul() function. Include unit tests.
--- include/qemu-common.h | 2 + tests/test-cutils.c | 318 ++++++++++++++++++++++++++++++++++++++++++++++++++ util/cutils.c | 32 +++++ 3 files changed, 352 insertions(+)
include/qemu-common.h +2 -0
@@ -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);
tests/test-cutils.c +318 -0
329 lines not on this page · raw message
util/cutils.c +32 -0
@@ -416,6 +416,38 @@ 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, unless the original +* (nonnegated) value would overflow, in this case, it will set @result +* to ULONG_MAX, and return ERANGE. +* +* The same behavior holds, for qemu_strtoull() but sets @result to +* ULLONG_MAX instead of ULONG_MAX. +* +* 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: *
Signature · 1 line
1.9.1
PATCH v2 3/5 cutils: Add qemu_strtoll() wrapper
In reply to Carlos L. TorresFrom: "Carlos L. Torres" <[email protected]>
Add wrapper for strtoll() function. Include unit tests.
--- include/qemu-common.h | 2 + tests/test-cutils.c | 320 ++++++++++++++++++++++++++++++++++++++++++++++++++ util/cutils.c | 23 ++++ 3 files changed, 345 insertions(+)
include/qemu-common.h +2 -0
@@ -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);
tests/test-cutils.c +320 -0
331 lines not on this page · raw message
util/cutils.c +23 -0
@@ -449,6 +449,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
Signature · 1 line
1.9.1
PATCH v2 4/5 cutils: Add qemu_strtoull() wrapper
In reply to Carlos L. TorresFrom: "Carlos L. Torres" <[email protected]>
Add wrapper for strtoull() function. Include unit tests.
--- include/qemu-common.h | 2 + tests/test-cutils.c | 323 ++++++++++++++++++++++++++++++++++++++++++++++++++ util/cutils.c | 23 ++++ 3 files changed, 348 insertions(+)
include/qemu-common.h +2 -0
@@ -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);
tests/test-cutils.c +323 -0
334 lines not on this page · raw message
util/cutils.c +23 -0
@@ -472,6 +472,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
Signature · 1 line
1.9.1
PATCH v2 5/5 qmp: Add example usage of strto*l() qemu wrapper
In reply to Carlos L. TorresFrom: "Carlos L. Torres" <[email protected]>
--- qmp.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-)
qmp.c +17 -4
@@ -49,14 +49,27 @@ VersionInfo *qmp_query_version(Error **errp) {VersionInfo *info =g_new0 (VersionInfo,1 );const char *version =QEMU_VERSION ; -char *tmp; +const char *tmp; +int err; info->qemu =g_new0 (VersionTriple,1 ); - info->qemu->major =strtol (version, &tmp,10 ); + err =qemu_strtol (version, &tmp,10 , &(info->qemu->major)); +if (err) { +error_setg (errp,"There was a problem retrieving QEMU major version." ); + } tmp++; - info->qemu->minor =strtol (tmp, &tmp,10 ); + + err =qemu_strtol (tmp, &tmp,10 , &(info->qemu->minor)); +if (err) { +error_setg (errp,"There was a problem retrieving QEMU minor version." ); + } tmp++; - info->qemu->micro =strtol (tmp, &tmp,10 ); + + err =qemu_strtol (tmp, &tmp,10 , &(info->qemu->micro)); +if (err) { +error_setg (errp,"There was a problem retrieving QEMU micro version." ); + } + info->package =g_strdup (QEMU_PKGVERSION );return info;
Signature · 1 line
1.9.1
Re: [Qemu-devel] [PATCH v2 5/5] qmp: Add example usage of strto*l() qemu wrapper
qmp.c VersionInfo *qmp_query_version(Error **errp) · line 56
Quoted 5 lines · Carlos L. Torres
qmp.c +2
-1+int err; info->qemu =g_new0 (VersionTriple,1 ); - info->qemu->major =strtol (version, &tmp,10 ); + err =qemu_strtol (version, &tmp,10 , &(info->qemu->major));
There are usually no parentheses around the argument of the & operator.
qmp.c VersionInfo *qmp_query_version(Error **errp) · line 59
Quoted 3 lines
qmp.c +3
-0+if (err) { +error_setg (errp,"There was a problem retrieving QEMU major version." ); + }
I think it's okay to just assert that err is zero. Otherwise, this simple example is okay. Thanks!
Paolo
Quoted 14 lines
tmp++;qmp.c +10
-2- info->qemu->minor =strtol (tmp, &tmp,10 ); + + err =qemu_strtol (tmp, &tmp,10 , &(info->qemu->minor)); +if (err) { +error_setg (errp,"There was a problem retrieving QEMU minor version." ); + } tmp++; - info->qemu->micro =strtol (tmp, &tmp,10 ); + + err =qemu_strtol (tmp, &tmp,10 , &(info->qemu->micro)); +if (err) { +error_setg (errp,"There was a problem retrieving QEMU micro version." ); + }
Re: [Qemu-devel] [PATCH v2 5/5] qmp: Add example usage of strto*l() qemu wrapper
Quoted 20 lines · Paolo Bonzini
qmp.c VersionInfo *qmp_query_version(Error **errp) · line 56
Quoted 5 lines · Carlos L. Torres
qmp.c +2
-1+int err; info->qemu =g_new0 (VersionTriple,1 ); - info->qemu->major =strtol (version, &tmp,10 ); + err =qemu_strtol (version, &tmp,10 , &(info->qemu->major));There are usually no parentheses around the argument of the & operator.
qmp.c VersionInfo *qmp_query_version(Error **errp) · line 59
Quoted 3 lines
qmp.c +3
-0+if (err) { +error_setg (errp,"There was a problem retrieving QEMU major version." ); + }I think it's okay to just assert that err is zero. Otherwise, this simple example is okay. Thanks!
Paolo
Quoted 14 lines
Quoted 4 lines
tmp++;qmp.c +2
-1- info->qemu->minor =strtol (tmp, &tmp,10 ); + + err =qemu_strtol (tmp, &tmp,10 , &(info->qemu->minor));qmp.c VersionInfo *qmp_query_version(Error **errp) · line 71
Quoted 10 lines
qmp.c +8
-1+if (err) { +error_setg (errp,"There was a problem retrieving QEMU minor version." ); + } tmp++; - info->qemu->micro =strtol (tmp, &tmp,10 ); + + err =qemu_strtol (tmp, &tmp,10 , &(info->qemu->micro)); +if (err) { +error_setg (errp,"There was a problem retrieving QEMU micro version." ); + }
Hi Paolo,
Thanks for taking the time to review the patch.
I'll make those small changes, and post v3 version of the patch.
Thanks,
-- Carlos Torres