aboutsummaryrefslogtreecommitdiffstats
path: root/wsutil
diff options
context:
space:
mode:
authorGuy Harris <guy@alum.mit.edu>2020-02-24 23:36:35 -0800
committerGuy Harris <guy@alum.mit.edu>2020-02-25 08:27:52 +0000
commitcd6134da9086bf66513a8fa842a4aa04e690025f (patch)
treeb2f98a43c13f75e3fdf6906d77d06c6547864722 /wsutil
parentf2a64e9559e5e4a4d5582aaef2a5c5debab2e753 (diff)
Add ws_strtoi() and ws_strtoui() routines and use them.
Those fetch gint and guint values, respectively, rather than values with specified sizes in bits. This should squelch Coverity CID 1457357. Change-Id: Ia8f100bd3fe90c266e24a4346f80b2667c653b93 Reviewed-on: https://code.wireshark.org/review/36177 Petri-Dish: Guy Harris <guy@alum.mit.edu> Tested-by: Petri Dish Buildbot Reviewed-by: Guy Harris <guy@alum.mit.edu>
Diffstat (limited to 'wsutil')
-rw-r--r--wsutil/strtoi.c84
-rw-r--r--wsutil/strtoi.h4
2 files changed, 88 insertions, 0 deletions
diff --git a/wsutil/strtoi.c b/wsutil/strtoi.c
index a70deced89..7a9a9001fe 100644
--- a/wsutil/strtoi.c
+++ b/wsutil/strtoi.c
@@ -102,6 +102,50 @@ DEFINE_WS_STRTOI_BITS(32)
DEFINE_WS_STRTOI_BITS(16)
DEFINE_WS_STRTOI_BITS(8)
+gboolean ws_strtoi(const gchar* str, const gchar** endptr, gint* cint)
+{
+ gint64 val = 0;
+ if (!ws_strtoi64(str, endptr, &val)) {
+ /*
+ * For ERANGE, return either G_MININT or
+ * G_MAXINT so our caller knows whether
+ * to report the value as "too small" or "too
+ * large".
+ *
+ * For other errors, return 0, for parallelism
+ * with ws_strtoi64().
+ */
+ if (errno == ERANGE) {
+ if (val < 0)
+ *cint = G_MININT;
+ else
+ *cint = G_MAXINT;
+ } else
+ *cint = 0;
+ return FALSE;
+ }
+ if (val < G_MININT) {
+ /*
+ * Return G_MININT so our caller knows whether to
+ * report the value as "too small" or "too large".
+ */
+ *cint = G_MININT;
+ errno = ERANGE;
+ return FALSE;
+ }
+ if (val > G_MAXINT) {
+ /*
+ * Return G_MAXINT so our caller knows whether to
+ * report the value as "too small" or "too large".
+ */
+ *cint = G_MAXINT;
+ errno = ERANGE;
+ return FALSE;
+ }
+ *cint = (gint)val;
+ return TRUE;
+}
+
gboolean ws_basestrtou64(const gchar* str, const gchar** endptr, guint64* cint, int base)
{
gchar* end;
@@ -204,6 +248,46 @@ DEFINE_WS_STRTOU_BITS(32)
DEFINE_WS_STRTOU_BITS(16)
DEFINE_WS_STRTOU_BITS(8)
+gboolean ws_basestrtou(const gchar* str, const gchar** endptr, guint* cint, int base)
+{
+ guint64 val;
+ if (!ws_basestrtou64(str, endptr, &val, base)) {
+ /*
+ * For ERANGE, return G_MAXUINT for parallelism
+ * with ws_strtoi().
+ *
+ * For other errors, return 0, for parallelism
+ * with ws_basestrtou64().
+ */
+ if (errno == ERANGE)
+ *cint = G_MAXUINT;
+ else
+ *cint = 0;
+ return FALSE;
+ }
+ if (val > G_MAXUINT) {
+ /*
+ * Return G_MAXUINT for parallelism with
+ * ws_strtoi().
+ */
+ *cint = G_MAXUINT;
+ errno = ERANGE;
+ return FALSE;
+ }
+ *cint = (guint)val;
+ return TRUE;
+}
+
+gboolean ws_strtou(const gchar* str, const gchar** endptr, guint* cint)
+{
+ return ws_basestrtou(str, endptr, cint, 10);
+}
+\
+gboolean ws_hexstrtou(const gchar* str, const gchar** endptr, guint* cint)
+{
+ return ws_basestrtou(str, endptr, cint, 16);
+}
+
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
diff --git a/wsutil/strtoi.h b/wsutil/strtoi.h
index 072f05a0c1..6e2fabed07 100644
--- a/wsutil/strtoi.h
+++ b/wsutil/strtoi.h
@@ -37,11 +37,13 @@ WS_DLL_PUBLIC gboolean ws_strtoi64(const gchar* str, const gchar** endptr, gint6
WS_DLL_PUBLIC gboolean ws_strtoi32(const gchar* str, const gchar** endptr, gint32* cint);
WS_DLL_PUBLIC gboolean ws_strtoi16(const gchar* str, const gchar** endptr, gint16* cint);
WS_DLL_PUBLIC gboolean ws_strtoi8 (const gchar* str, const gchar** endptr, gint8* cint);
+WS_DLL_PUBLIC gboolean ws_strtoi (const gchar* str, const gchar** endptr, gint* cint);
WS_DLL_PUBLIC gboolean ws_strtou64(const gchar* str, const gchar** endptr, guint64* cint);
WS_DLL_PUBLIC gboolean ws_strtou32(const gchar* str, const gchar** endptr, guint32* cint);
WS_DLL_PUBLIC gboolean ws_strtou16(const gchar* str, const gchar** endptr, guint16* cint);
WS_DLL_PUBLIC gboolean ws_strtou8 (const gchar* str, const gchar** endptr, guint8* cint);
+WS_DLL_PUBLIC gboolean ws_strtou (const gchar* str, const gchar** endptr, guint* cint);
/*
* \brief Convert a hexadecimal string to an unsigned int, with error checks.
@@ -60,6 +62,7 @@ WS_DLL_PUBLIC gboolean ws_hexstrtou64(const gchar* str, const gchar** endptr, gu
WS_DLL_PUBLIC gboolean ws_hexstrtou32(const gchar* str, const gchar** endptr, guint32* cint);
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);
+WS_DLL_PUBLIC gboolean ws_hexstrtou (const gchar* str, const gchar** endptr, guint* cint);
/*
* \brief Convert a string in the specified base to an unsigned int, with
@@ -82,6 +85,7 @@ WS_DLL_PUBLIC gboolean ws_basestrtou64(const gchar* str, const gchar** endptr, g
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);
+WS_DLL_PUBLIC gboolean ws_basestrtou (const gchar* str, const gchar** endptr, guint* cint, int base);
#ifdef __cplusplus
}