libnm: avoid heap allocation for checking valid routes in nm_ip_route_attribute_validate()

This commit is contained in:
Thomas Haller 2019-08-03 08:01:18 +02:00
parent cc7b2cde95
commit 539db43619

View file

@ -1312,8 +1312,6 @@ nm_ip_route_attribute_validate (const char *name,
if (g_variant_type_equal (spec->type, G_VARIANT_TYPE_STRING)) { if (g_variant_type_equal (spec->type, G_VARIANT_TYPE_STRING)) {
const char *string = g_variant_get_string (value, NULL); const char *string = g_variant_get_string (value, NULL);
gs_free char *string_free = NULL;
char *sep;
switch (spec->str_type) { switch (spec->str_type) {
case 'a': /* IP address */ case 'a': /* IP address */
@ -1328,30 +1326,35 @@ nm_ip_route_attribute_validate (const char *name,
return FALSE; return FALSE;
} }
break; break;
case 'p': /* IP address + optional prefix */ case 'p': /* IP address + optional prefix */ {
string_free = g_strdup (string); gs_free char *addr_free = NULL;
sep = strchr (string_free, '/'); const char *addr = string;
if (sep) { const char *str;
*sep = 0;
if (_nm_utils_ascii_str_to_int64 (sep + 1, 10, 1, family == AF_INET ? 32 : 128, -1) < 0) { str = strchr (addr, '/');
if (str) {
addr = nm_strndup_a (200, addr, str - addr, &addr_free);
str++;
if (_nm_utils_ascii_str_to_int64 (str, 10, 1, family == AF_INET ? 32 : 128, -1) < 0) {
g_set_error (error, g_set_error (error,
NM_CONNECTION_ERROR, NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_FAILED, NM_CONNECTION_ERROR_FAILED,
_("invalid prefix %s"), sep + 1); _("invalid prefix %s"), str);
return FALSE; return FALSE;
} }
} }
if (!nm_utils_ipaddr_valid (family, string_free)) { if (!nm_utils_ipaddr_valid (family, addr)) {
g_set_error (error, g_set_error (error,
NM_CONNECTION_ERROR, NM_CONNECTION_ERROR,
NM_CONNECTION_ERROR_FAILED, NM_CONNECTION_ERROR_FAILED,
family == AF_INET ? family == AF_INET ?
_("'%s' is not a valid IPv4 address") : _("'%s' is not a valid IPv4 address") :
_("'%s' is not a valid IPv6 address"), _("'%s' is not a valid IPv6 address"),
string_free); string);
return FALSE; return FALSE;
} }
break; break;
}
default: default:
break; break;
} }