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.
This commit is contained in:
Thomas Haller 2019-01-31 13:40:53 +01:00
parent a3370af3a8
commit b7bb744973
3 changed files with 11 additions and 26 deletions

View file

@ -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

View file

@ -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;

View file

@ -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