diff --git a/epan/range.c b/epan/range.c index 7bbf4f8c70..0dce529554 100644 --- a/epan/range.c +++ b/epan/range.c @@ -13,6 +13,7 @@ #include "config.h" +#include #include #include #include @@ -22,7 +23,8 @@ #include #include -#include + +#include /* * Size of the header of a range_t. @@ -84,11 +86,11 @@ range_convert_str_work(wmem_allocator_t *scope, range_t **rangep, const gchar *e range_t *range; guint nranges; const gchar *p; - char *endp; + const char *endp; gchar c; guint i; guint32 tmp; - unsigned long val; + guint32 val; if ( (rangep == NULL) || (es == NULL) ) return CVT_SYNTAX_ERROR; @@ -135,8 +137,8 @@ range_convert_str_work(wmem_allocator_t *scope, range_t **rangep, const gchar *e } else if (g_ascii_isdigit(c)) { /* Subrange starts with the specified number */ errno = 0; - val = strtoul(p, &endp, 0); - if (p == endp) { + ws_basestrtou32(p, &endp, &val, 0); + if (errno == EINVAL) { /* That wasn't a valid number. */ wmem_free(scope, range); return CVT_SYNTAX_ERROR; @@ -181,8 +183,8 @@ range_convert_str_work(wmem_allocator_t *scope, range_t **rangep, const gchar *e } else if (g_ascii_isdigit(c)) { /* Subrange ends with the specified number. */ errno = 0; - val = strtoul(p, &endp, 0); - if (p == endp) { + ws_basestrtou32(p, &endp, &val, 0); + if (errno == EINVAL) { /* That wasn't a valid number. */ wmem_free(scope, range); return CVT_SYNTAX_ERROR; diff --git a/wsutil/strtoi.c b/wsutil/strtoi.c index ac97efe163..a70deced89 100644 --- a/wsutil/strtoi.c +++ b/wsutil/strtoi.c @@ -102,7 +102,7 @@ DEFINE_WS_STRTOI_BITS(32) DEFINE_WS_STRTOI_BITS(16) DEFINE_WS_STRTOI_BITS(8) -static gboolean ws_basestrtou64(const gchar* str, const gchar** endptr, guint64* cint, int base) +gboolean ws_basestrtou64(const gchar* str, const gchar** endptr, guint64* cint, int base) { gchar* end; guint64 val; @@ -160,7 +160,7 @@ gboolean ws_hexstrtou64(const gchar* str, const gchar** endptr, guint64* cint) } #define DEFINE_WS_STRTOU_BITS(bits) \ -static gboolean ws_basestrtou##bits(const gchar* str, const gchar** endptr, guint##bits* cint, int base) \ +gboolean ws_basestrtou##bits(const gchar* str, const gchar** endptr, guint##bits* cint, int base) \ { \ guint64 val; \ if (!ws_basestrtou64(str, endptr, &val, base)) { \ diff --git a/wsutil/strtoi.h b/wsutil/strtoi.h index 1e3fa6acd8..072f05a0c1 100644 --- a/wsutil/strtoi.h +++ b/wsutil/strtoi.h @@ -61,6 +61,28 @@ WS_DLL_PUBLIC gboolean ws_hexstrtou32(const gchar* str, const gchar** endptr, gu WS_DLL_PUBLIC gboolean ws_hexstrtou16(const gchar* str, const gchar** endptr, guint16* cint); WS_DLL_PUBLIC gboolean ws_hexstrtou8 (const gchar* str, const gchar** endptr, guint8* cint); +/* + * \brief Convert a string in the specified base to an unsigned int, with + * error checks. + * \param str The string to convert + * \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 + * \param base The base for the integer; 0 means "if it begins with 0x, + * it's hex, otherwise if it begins with 0, it's octal, otherwise it's + * decimal". + * \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. + */ + +WS_DLL_PUBLIC gboolean ws_basestrtou64(const gchar* str, const gchar** endptr, guint64* cint, int base); +WS_DLL_PUBLIC gboolean ws_basestrtou32(const gchar* str, const gchar** endptr, guint32* cint, int base); +WS_DLL_PUBLIC gboolean ws_basestrtou16(const gchar* str, const gchar** endptr, guint16* cint, int base); +WS_DLL_PUBLIC gboolean ws_basestrtou8 (const gchar* str, const gchar** endptr, guint8* cint, int base); + #ifdef __cplusplus } #endif /* __cplusplus */