cutils: Add qemu_strto*l() wrappers
v3 cover cutils: Add qemu_strto*l() 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 | 14 +- tests/test-cutils.c | 1280 +++++++++++++++++++++++++++++++++++++++++++++++++ util/cutils.c | 136 ++++++ 4 files changed, 1434 insertions(+), 4 deletions(-)
Signature · 1 line
1.9.1
PATCH v3 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 v3 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 v3 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 v3 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 v3 5/5 qmp: Add example usage of strto*l() qemu wrapper
In reply to Carlos L. TorresFrom: "Carlos L. Torres" <[email protected]>
--- qmp.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
qmp.c +10 -4
@@ -49,14 +49,20 @@ 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); +assert (err ==0 ); tmp++; - info->qemu->minor =strtol (tmp, &tmp,10 ); + + err =qemu_strtol (tmp, &tmp,10 , &info->qemu->minor); +assert (err ==0 ); tmp++; - info->qemu->micro =strtol (tmp, &tmp,10 ); + + err =qemu_strtol (tmp, &tmp,10 , &info->qemu->micro); +assert (err ==0 ); info->package =g_strdup (QEMU_PKGVERSION );return info;
Signature · 1 line
1.9.1
Quoted 8 lines · Carlos L. Torres
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.
Quoted 4 lines
Different from the C strto*l() functions, these wrappers return through the 'result' out parameter.
Include unit-tests for each wrapper function.
Can you also patch scripts/check-patch.pl to forcefully warn developers when they are introducing bogus uses of strto*l() and should be using the sane wrappers instead?
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org