summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2018-10-16 14:06:38 +0200
committerThomas Haller <thaller@redhat.com>2018-10-17 13:06:56 +0200
commitfa2d6792f7335b4249e24a2d06f5c1fa56e48dfe (patch)
tree9dd4401e22a32256b751408d4bf29b4c49cdcfdf
parentfd7115eeed527c92925b3b176add81d9a3605455 (diff)
downloadNetworkManager-fa2d6792f7335b4249e24a2d06f5c1fa56e48dfe.tar.gz
shared: add _nm_utils_ascii_str_to_uint64() helper
-rw-r--r--shared/nm-utils/nm-shared-utils.c44
-rw-r--r--shared/nm-utils/nm-shared-utils.h3
2 files changed, 46 insertions, 1 deletions
diff --git a/shared/nm-utils/nm-shared-utils.c b/shared/nm-utils/nm-shared-utils.c
index c41b5e68c2..0c7f708cb3 100644
--- a/shared/nm-utils/nm-shared-utils.c
+++ b/shared/nm-utils/nm-shared-utils.c
@@ -608,6 +608,50 @@ _nm_utils_ascii_str_to_int64 (const char *str, guint base, gint64 min, gint64 ma
return v;
}
+guint64
+_nm_utils_ascii_str_to_uint64 (const char *str, guint base, guint64 min, guint64 max, guint64 fallback)
+{
+ guint64 v;
+ const char *s = NULL;
+
+ if (str) {
+ while (g_ascii_isspace (str[0]))
+ str++;
+ }
+ if (!str || !str[0]) {
+ errno = EINVAL;
+ return fallback;
+ }
+
+ errno = 0;
+ v = g_ascii_strtoull (str, (char **) &s, base);
+
+ if (errno != 0)
+ return fallback;
+ if (s[0] != '\0') {
+ while (g_ascii_isspace (s[0]))
+ s++;
+ if (s[0] != '\0') {
+ errno = EINVAL;
+ return fallback;
+ }
+ }
+ if (v > max || v < min) {
+ errno = ERANGE;
+ return fallback;
+ }
+
+ if ( v != 0
+ && str[0] == '-') {
+ /* I don't know why, but g_ascii_strtoull() accepts minus signs ("-2" gives 18446744073709551614).
+ * For "-0" that is OK, but otherwise not. */
+ return errno = ERANGE;
+ return fallback;
+ }
+
+ return v;
+}
+
/*****************************************************************************/
/* like nm_strcmp_p(), suitable for g_ptr_array_sort_with_data().
diff --git a/shared/nm-utils/nm-shared-utils.h b/shared/nm-utils/nm-shared-utils.h
index 7ee76799bd..bfa51def49 100644
--- a/shared/nm-utils/nm-shared-utils.h
+++ b/shared/nm-utils/nm-shared-utils.h
@@ -408,7 +408,8 @@ gboolean nm_utils_parse_inaddr_prefix (int addr_family,
char **out_addr,
int *out_prefix);
-gint64 _nm_utils_ascii_str_to_int64 (const char *str, guint base, gint64 min, gint64 max, gint64 fallback);
+gint64 _nm_utils_ascii_str_to_int64 (const char *str, guint base, gint64 min, gint64 max, gint64 fallback);
+guint64 _nm_utils_ascii_str_to_uint64 (const char *str, guint base, guint64 min, guint64 max, guint64 fallback);
int _nm_utils_ascii_str_to_bool (const char *str,
int default_value);