ifcfg-rh: modify svGetString() signature to avoid copying of temporary value

Modify the signature of svGetString() to allow getting the internal
string without cloning the string after unescaping.
This commit is contained in:
Thomas Haller
2016-11-01 12:50:53 +01:00
parent 08a22f2eea
commit 82ccec22ca
3 changed files with 30 additions and 40 deletions

View File

@@ -678,7 +678,7 @@ error:
} }
static void static void
parse_dns_options (NMSettingIPConfig *ip_config, char *value) parse_dns_options (NMSettingIPConfig *ip_config, const char *value)
{ {
char **options = NULL; char **options = NULL;
@@ -969,7 +969,8 @@ make_ip4_setting (shvarFile *ifcfg,
char *value = NULL; char *value = NULL;
char *route_path = NULL; char *route_path = NULL;
char *method; char *method;
char *dns_options = NULL; gs_free char *dns_options_free = NULL;
const char *dns_options = NULL;
gs_free char *gateway = NULL; gs_free char *gateway = NULL;
gint32 i; gint32 i;
shvarFile *network_ifcfg; shvarFile *network_ifcfg;
@@ -995,7 +996,7 @@ make_ip4_setting (shvarFile *ifcfg,
/* Get the connection ifcfg device name and the global gateway device */ /* Get the connection ifcfg device name and the global gateway device */
value = svGetValueString (ifcfg, "DEVICE"); value = svGetValueString (ifcfg, "DEVICE");
gatewaydev = svGetValueString (network_ifcfg, "GATEWAYDEV"); gatewaydev = svGetValueString (network_ifcfg, "GATEWAYDEV");
dns_options = svGetValue (network_ifcfg, "RES_OPTIONS"); dns_options = svGetValue (network_ifcfg, "RES_OPTIONS", &dns_options_free);
/* If there was a global gateway device specified, then only connections /* If there was a global gateway device specified, then only connections
* for that device can be the default connection. * for that device can be the default connection.
@@ -1192,12 +1193,9 @@ make_ip4_setting (shvarFile *ifcfg,
} }
/* DNS options */ /* DNS options */
value = svGetValue (ifcfg, "RES_OPTIONS"); parse_dns_options (s_ip4, svGetValue (ifcfg, "RES_OPTIONS", &value));
parse_dns_options (s_ip4, value);
parse_dns_options (s_ip4, dns_options); parse_dns_options (s_ip4, dns_options);
g_free (value); g_free (value);
g_free (dns_options);
dns_options = NULL;
/* DNS priority */ /* DNS priority */
priority = svGetValueInt64 (ifcfg, "IPV4_DNS_PRIORITY", 10, G_MININT32, G_MAXINT32, 0); priority = svGetValueInt64 (ifcfg, "IPV4_DNS_PRIORITY", 10, G_MININT32, G_MAXINT32, 0);
@@ -1267,7 +1265,6 @@ make_ip4_setting (shvarFile *ifcfg,
return NM_SETTING (s_ip4); return NM_SETTING (s_ip4);
done: done:
g_free (dns_options);
g_free (route_path); g_free (route_path);
g_object_unref (s_ip4); g_object_unref (s_ip4);
return NULL; return NULL;
@@ -1382,7 +1379,8 @@ make_ip6_setting (shvarFile *ifcfg,
char *value = NULL; char *value = NULL;
char *str_value; char *str_value;
char *route6_path = NULL; char *route6_path = NULL;
char *dns_options = NULL; gs_free char *dns_options_free = NULL;
const char *dns_options = NULL;
gboolean ipv6init, ipv6forwarding, ipv6_autoconf, dhcp6 = FALSE; gboolean ipv6init, ipv6forwarding, ipv6_autoconf, dhcp6 = FALSE;
char *method = NM_SETTING_IP6_CONFIG_METHOD_MANUAL; char *method = NM_SETTING_IP6_CONFIG_METHOD_MANUAL;
char *ipv6addr, *ipv6addr_secondaries; char *ipv6addr, *ipv6addr_secondaries;
@@ -1418,7 +1416,7 @@ make_ip6_setting (shvarFile *ifcfg,
value = svGetValueString (ifcfg, "DEVICE"); value = svGetValueString (ifcfg, "DEVICE");
ipv6_defaultgw = svGetValueString (network_ifcfg, "IPV6_DEFAULTGW"); ipv6_defaultgw = svGetValueString (network_ifcfg, "IPV6_DEFAULTGW");
ipv6_defaultdev = svGetValueString (network_ifcfg, "IPV6_DEFAULTDEV"); ipv6_defaultdev = svGetValueString (network_ifcfg, "IPV6_DEFAULTDEV");
dns_options = svGetValue (network_ifcfg, "RES_OPTIONS"); dns_options = svGetValue (network_ifcfg, "RES_OPTIONS", &dns_options_free);
if (ipv6_defaultgw) { if (ipv6_defaultgw) {
default_dev = strchr (ipv6_defaultgw, '%'); default_dev = strchr (ipv6_defaultgw, '%');
@@ -1639,11 +1637,9 @@ make_ip6_setting (shvarFile *ifcfg,
} }
/* DNS options */ /* DNS options */
value = svGetValue (ifcfg, "RES_OPTIONS"); parse_dns_options (s_ip6, svGetValue (ifcfg, "RES_OPTIONS", &value));
parse_dns_options (s_ip6, value);
parse_dns_options (s_ip6, dns_options); parse_dns_options (s_ip6, dns_options);
g_free (value); g_free (value);
g_free (dns_options);
/* DNS priority */ /* DNS priority */
priority = svGetValueInt64 (ifcfg, "IPV6_DNS_PRIORITY", 10, G_MININT32, G_MAXINT32, 0); priority = svGetValueInt64 (ifcfg, "IPV6_DNS_PRIORITY", 10, G_MININT32, G_MAXINT32, 0);
@@ -1655,7 +1651,6 @@ make_ip6_setting (shvarFile *ifcfg,
return NM_SETTING (s_ip6); return NM_SETTING (s_ip6);
error: error:
g_free (dns_options);
g_free (route6_path); g_free (route6_path);
g_object_unref (s_ip6); g_object_unref (s_ip6);
return NULL; return NULL;
@@ -3373,6 +3368,7 @@ make_wireless_setting (shvarFile *ifcfg,
GError **error) GError **error)
{ {
NMSettingWireless *s_wireless; NMSettingWireless *s_wireless;
const char *cvalue;
char *value = NULL; char *value = NULL;
gint64 chan = 0; gint64 chan = 0;
NMSettingMacRandomization mac_randomization = NM_SETTING_MAC_RANDOMIZATION_NEVER; NMSettingMacRandomization mac_randomization = NM_SETTING_MAC_RANDOMIZATION_NEVER;
@@ -3535,19 +3531,19 @@ make_wireless_setting (shvarFile *ifcfg,
svGetValueBoolean (ifcfg, "SSID_HIDDEN", FALSE), svGetValueBoolean (ifcfg, "SSID_HIDDEN", FALSE),
NULL); NULL);
value = svGetValue (ifcfg, "POWERSAVE"); cvalue = svGetValue (ifcfg, "POWERSAVE", &value);
if (value) { if (cvalue) {
if (!strcmp (value, "default")) if (!strcmp (cvalue, "default"))
powersave = NM_SETTING_WIRELESS_POWERSAVE_DEFAULT; powersave = NM_SETTING_WIRELESS_POWERSAVE_DEFAULT;
else if (!strcmp (value, "ignore")) else if (!strcmp (cvalue, "ignore"))
powersave = NM_SETTING_WIRELESS_POWERSAVE_IGNORE; powersave = NM_SETTING_WIRELESS_POWERSAVE_IGNORE;
else if (!strcmp (value, "disable") || !strcmp (value, "no")) else if (!strcmp (cvalue, "disable") || !strcmp (cvalue, "no"))
powersave = NM_SETTING_WIRELESS_POWERSAVE_DISABLE; powersave = NM_SETTING_WIRELESS_POWERSAVE_DISABLE;
else if (!strcmp (value, "enable") || !strcmp (value, "yes")) else if (!strcmp (cvalue, "enable") || !strcmp (cvalue, "yes"))
powersave = NM_SETTING_WIRELESS_POWERSAVE_ENABLE; powersave = NM_SETTING_WIRELESS_POWERSAVE_ENABLE;
else { else {
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
"Invalid POWERSAVE value '%s'", value); "Invalid POWERSAVE value '%s'", cvalue);
g_free (value); g_free (value);
goto error; goto error;
} }
@@ -3559,17 +3555,17 @@ make_wireless_setting (shvarFile *ifcfg,
powersave, powersave,
NULL); NULL);
value = svGetValue (ifcfg, "MAC_ADDRESS_RANDOMIZATION"); cvalue = svGetValue (ifcfg, "MAC_ADDRESS_RANDOMIZATION", &value);
if (value) { if (cvalue) {
if (strcmp (value, "default") == 0) if (strcmp (cvalue, "default") == 0)
mac_randomization = NM_SETTING_MAC_RANDOMIZATION_DEFAULT; mac_randomization = NM_SETTING_MAC_RANDOMIZATION_DEFAULT;
else if (strcmp (value, "never") == 0) else if (strcmp (cvalue, "never") == 0)
mac_randomization = NM_SETTING_MAC_RANDOMIZATION_NEVER; mac_randomization = NM_SETTING_MAC_RANDOMIZATION_NEVER;
else if (strcmp (value, "always") == 0) else if (strcmp (cvalue, "always") == 0)
mac_randomization = NM_SETTING_MAC_RANDOMIZATION_ALWAYS; mac_randomization = NM_SETTING_MAC_RANDOMIZATION_ALWAYS;
else { else {
g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION, g_set_error (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_INVALID_CONNECTION,
"Invalid MAC_ADDRESS_RANDOMIZATION value '%s'", value); "Invalid MAC_ADDRESS_RANDOMIZATION value '%s'", cvalue);
g_free (value); g_free (value);
goto error; goto error;
} }
@@ -3918,8 +3914,8 @@ make_wired_setting (shvarFile *ifcfg,
g_free (value); g_free (value);
} }
value = svGetValue (ifcfg, "ETHTOOL_OPTS"); parse_ethtool_options (ifcfg, s_wired,
parse_ethtool_options (ifcfg, s_wired, value); svGetValue (ifcfg, "ETHTOOL_OPTS", &value));
g_free (value); g_free (value);
return (NMSetting *) s_wired; return (NMSetting *) s_wired;

View File

@@ -776,20 +776,14 @@ _svGetValue (shvarFile *s, const char *key, char **to_free)
return svUnescape (last_val, to_free); return svUnescape (last_val, to_free);
} }
/* svGetValue() is identical to svGetValueString() except that const char *
* svGetValueString() will never return an empty value (but %NULL instead). svGetValue (shvarFile *s, const char *key, char **to_free)
* svGetValue() will return empty values if that is the value for the @key. */
char *
svGetValue (shvarFile *s, const char *key)
{ {
const char *value;
char *to_free;
g_return_val_if_fail (s != NULL, NULL); g_return_val_if_fail (s != NULL, NULL);
g_return_val_if_fail (key != NULL, NULL); g_return_val_if_fail (key != NULL, NULL);
g_return_val_if_fail (to_free, NULL);
value = _svGetValue (s, key, &to_free); return _svGetValue (s, key, to_free);
return to_free ?: (value ? g_strdup (value) : NULL);
} }
/* Get the value associated with the key, and leave the current pointer /* Get the value associated with the key, and leave the current pointer

View File

@@ -48,8 +48,8 @@ shvarFile *svOpenFile (const char *name, GError **error);
* pointing at the line containing the value. The char* returned MUST * pointing at the line containing the value. The char* returned MUST
* be freed by the caller. * be freed by the caller.
*/ */
const char *svGetValue (shvarFile *s, const char *key, char **to_free);
char *svGetValueString (shvarFile *s, const char *key); char *svGetValueString (shvarFile *s, const char *key);
char *svGetValue (shvarFile *s, const char *key);
gint svParseBoolean (const char *value, gint def); gint svParseBoolean (const char *value, gint def);