From cea1737bd218aa84bf41637deee25bd199fb128d Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Mon, 12 Sep 2016 11:22:16 +0200 Subject: Let strtoi with NULL endptr require no invalid characters If the caller is not interested in checking its end, then it probably wants a valid number only if the string contains a valid number. Add a shortcut for this. Change-Id: I39701bd445e29fb2606720b18ca3764c74a7255b Reviewed-on: https://code.wireshark.org/review/17658 Petri-Dish: Dario Lombardo Tested-by: Petri Dish Buildbot Reviewed-by: Guy Harris Reviewed-by: Dario Lombardo --- wsutil/clopts_common.c | 10 ++++------ wsutil/strtoi.c | 4 ++-- wsutil/strtoi.h | 8 +++++--- 3 files changed, 11 insertions(+), 11 deletions(-) (limited to 'wsutil') 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. */ -- cgit v1.2.3