diff options
Diffstat (limited to 'wsutil')
-rw-r--r-- | wsutil/clopts_common.c | 10 | ||||
-rw-r--r-- | wsutil/strtoi.c | 4 | ||||
-rw-r--r-- | wsutil/strtoi.h | 8 |
3 files changed, 11 insertions, 11 deletions
diff --git a/wsutil/clopts_common.c b/wsutil/clopts_common.c index 78763328d5..95d2850cba 100644 --- a/wsutil/clopts_common.c +++ b/wsutil/clopts_common.c @@ -33,11 +33,10 @@ int get_natural_int(const char *string, const char *name) { - const char *end; gint32 number; - if (!ws_strtoi32(string, &end, &number)) { - if (errno == EINVAL || *end != '\0') { + if (!ws_strtoi32(string, NULL, &number)) { + if (errno == EINVAL) { cmdarg_err("The specified %s \"%s\" isn't a decimal number", name, string); exit(1); } @@ -74,11 +73,10 @@ get_positive_int(const char *string, const char *name) guint32 get_guint32(const char *string, const char *name) { - const char *end; guint32 number; - if (!ws_strtou32(string, &end, &number)) { - if (errno == EINVAL || *end != '\0') { + if (!ws_strtou32(string, NULL, &number)) { + if (errno == EINVAL) { cmdarg_err("The specified %s \"%s\" isn't a decimal number", name, string); exit(1); } diff --git a/wsutil/strtoi.c b/wsutil/strtoi.c index 26e756d444..75d0cf3ff4 100644 --- a/wsutil/strtoi.c +++ b/wsutil/strtoi.c @@ -34,7 +34,7 @@ gboolean ws_strtoi64(const gchar* str, const gchar** endptr, gint64* cint) errno = 0; val = g_ascii_strtoll(str, &end, 10); - if (val == 0 && end == str) { + if ((val == 0 && end == str) || (endptr == NULL && *end != '\0')) { *cint = 0; if (endptr != NULL) *endptr = end; @@ -75,7 +75,7 @@ gboolean ws_strtou64(const gchar* str, const gchar** endptr, guint64* cint) } errno = 0; val = g_ascii_strtoull(str, &end, 10); - if (val == 0 && end == str) { + if ((val == 0 && end == str) || (endptr == NULL && *end != '\0')) { *cint = 0; if (endptr != NULL) *endptr = end; diff --git a/wsutil/strtoi.h b/wsutil/strtoi.h index ed48b52aac..3361da8fc3 100644 --- a/wsutil/strtoi.h +++ b/wsutil/strtoi.h @@ -32,10 +32,12 @@ /* * \brief Convert a string to a signed/unsigned int, with error checks. * \param str The string to convert - * \param endptr NULL or pointer to a pointer set to point to the - * character after the last character of the number + * \param endptr A pointer that will store a pointer to the first invalid + * character in str, allowing a number to be parsed even if there is trailing + * whitespace. If NULL, then the string is assumed to contain only valid + * characters (or it will error out). * \param cint The converted integer - * \return TRUE if the conversion suceeds, FALSE otherwise. + * \return TRUE if the conversion succeeds, FALSE otherwise. * On error, errno is set to EINVAL for unrecognized input and ERANGE * if the resulting number does not fit in the type. */ |