diff options
author | Thomas Haller <thaller@redhat.com> | 2019-01-31 13:40:53 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2019-02-12 08:50:28 +0100 |
commit | b7bb7449738b8fd3eee545af7fa5efd004718431 (patch) | |
tree | cc66a22028e2f4c8877281444867cb832ede4c22 | |
parent | a3370af3a8a965e41950c5022182730d79614df2 (diff) | |
download | NetworkManager-b7bb7449738b8fd3eee545af7fa5efd004718431.tar.gz |
libnm,core: use _nm_utils_ascii_str_to_uint64() instead of strtol()
Using strtol() correctly proves to be hard.
Usually, we want to also check that the end pointer is points to the end
of the string. Othewise, we silently accept trailing garbage.
-rw-r--r-- | clients/tui/newt/nmt-newt-entry-numeric.c | 4 | ||||
-rw-r--r-- | libnm-core/nm-setting-bond.c | 17 | ||||
-rw-r--r-- | src/supplicant/nm-supplicant-settings-verify.c | 16 |
3 files changed, 11 insertions, 26 deletions
diff --git a/clients/tui/newt/nmt-newt-entry-numeric.c b/clients/tui/newt/nmt-newt-entry-numeric.c index a52fe8689f..92855fcb2c 100644 --- a/clients/tui/newt/nmt-newt-entry-numeric.c +++ b/clients/tui/newt/nmt-newt-entry-numeric.c @@ -127,8 +127,8 @@ newt_entry_numeric_validate (NmtNewtEntry *entry, if (!*text) return priv->optional ? TRUE : FALSE; - val = _nm_utils_ascii_str_to_int64 (text, 10, priv->min, priv->max, 0); - return val != 0 || errno == 0; + val = _nm_utils_ascii_str_to_int64 (text, 10, priv->min, priv->max, G_MAXINT64); + return val != G_MAXINT64 || errno == 0; } static void diff --git a/libnm-core/nm-setting-bond.c b/libnm-core/nm-setting-bond.c index b36ed20851..51ce2debbc 100644 --- a/libnm-core/nm-setting-bond.c +++ b/libnm-core/nm-setting-bond.c @@ -175,19 +175,14 @@ nm_setting_bond_get_option (NMSettingBond *setting, static gboolean validate_int (const char *name, const char *value, const BondDefault *def) { - long num; - guint i; + guint64 num; - for (i = 0; i < strlen (value); i++) { - if (!g_ascii_isdigit (value[i]) && value[i] != '-') - return FALSE; - } - - errno = 0; - num = strtol (value, NULL, 10); - if (errno) + if (!NM_STRCHAR_ALL (value, ch, g_ascii_isdigit (ch))) return FALSE; - if (num < def->min || num > def->max) + + num = _nm_utils_ascii_str_to_uint64 (value, 10, def->min, def->max, G_MAXUINT64); + if ( num == G_MAXUINT64 + && errno != 0) return FALSE; return TRUE; diff --git a/src/supplicant/nm-supplicant-settings-verify.c b/src/supplicant/nm-supplicant-settings-verify.c index 2ed918b602..f10bbb0431 100644 --- a/src/supplicant/nm-supplicant-settings-verify.c +++ b/src/supplicant/nm-supplicant-settings-verify.c @@ -157,23 +157,13 @@ validate_type_int (const struct Opt * opt, const char * value, const guint32 len) { - long int intval; + gint64 v; g_return_val_if_fail (opt != NULL, FALSE); g_return_val_if_fail (value != NULL, FALSE); - errno = 0; - intval = strtol (value, NULL, 10); - if (errno != 0) - return FALSE; - - /* strtol returns a long, but we are dealing with ints */ - if (intval > INT_MAX || intval < INT_MIN) - return FALSE; - if (intval > opt->int_high || intval < opt->int_low) - return FALSE; - - return TRUE; + v = _nm_utils_ascii_str_to_int64 (value, 10, opt->int_low, opt->int_high, G_MININT64); + return v != G_MININT64 || errno == 0; } static gboolean |