Commit b7bb7449 authored by Thomas Haller's avatar Thomas Haller

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.
parent a3370af3
...@@ -127,8 +127,8 @@ newt_entry_numeric_validate (NmtNewtEntry *entry, ...@@ -127,8 +127,8 @@ newt_entry_numeric_validate (NmtNewtEntry *entry,
if (!*text) if (!*text)
return priv->optional ? TRUE : FALSE; return priv->optional ? TRUE : FALSE;
val = _nm_utils_ascii_str_to_int64 (text, 10, priv->min, priv->max, 0); val = _nm_utils_ascii_str_to_int64 (text, 10, priv->min, priv->max, G_MAXINT64);
return val != 0 || errno == 0; return val != G_MAXINT64 || errno == 0;
} }
static void static void
......
...@@ -175,19 +175,14 @@ nm_setting_bond_get_option (NMSettingBond *setting, ...@@ -175,19 +175,14 @@ nm_setting_bond_get_option (NMSettingBond *setting,
static gboolean static gboolean
validate_int (const char *name, const char *value, const BondDefault *def) validate_int (const char *name, const char *value, const BondDefault *def)
{ {
long num; guint64 num;
guint i;
for (i = 0; i < strlen (value); i++) { if (!NM_STRCHAR_ALL (value, ch, g_ascii_isdigit (ch)))
if (!g_ascii_isdigit (value[i]) && value[i] != '-')
return FALSE;
}
errno = 0;
num = strtol (value, NULL, 10);
if (errno)
return FALSE; 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 FALSE;
return TRUE; return TRUE;
......
...@@ -157,23 +157,13 @@ validate_type_int (const struct Opt * opt, ...@@ -157,23 +157,13 @@ validate_type_int (const struct Opt * opt,
const char * value, const char * value,
const guint32 len) const guint32 len)
{ {
long int intval; gint64 v;
g_return_val_if_fail (opt != NULL, FALSE); g_return_val_if_fail (opt != NULL, FALSE);
g_return_val_if_fail (value != NULL, FALSE); g_return_val_if_fail (value != NULL, FALSE);
errno = 0; v = _nm_utils_ascii_str_to_int64 (value, 10, opt->int_low, opt->int_high, G_MININT64);
intval = strtol (value, NULL, 10); return v != G_MININT64 || errno == 0;
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;
} }
static gboolean static gboolean
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment