network: fixes related to NetworkConfigSection

- Do not allocate NetworkConfigSection when filename == NULL
- set .network element before calling hashmap_put()
- Always free NetworkConfigSection in each object.
This commit is contained in:
Yu Watanabe 2018-11-12 14:57:04 +09:00
parent 17f9c355d5
commit 0f7f27694e
6 changed files with 53 additions and 58 deletions

View File

@ -11,18 +11,6 @@
#include "parse-util.h"
#include "socket-util.h"
int address_label_new(AddressLabel **ret) {
_cleanup_(address_label_freep) AddressLabel *addrlabel = NULL;
addrlabel = new0(AddressLabel, 1);
if (!addrlabel)
return -ENOMEM;
*ret = TAKE_PTR(addrlabel);
return 0;
}
void address_label_free(AddressLabel *label) {
if (!label)
return;
@ -50,31 +38,38 @@ static int address_label_new_static(Network *network, const char *filename, unsi
assert(ret);
assert(!!filename == (section_line > 0));
r = network_config_section_new(filename, section_line, &n);
if (r < 0)
return r;
if (filename) {
r = network_config_section_new(filename, section_line, &n);
if (r < 0)
return r;
label = hashmap_get(network->address_labels_by_section, n);
if (label) {
*ret = TAKE_PTR(label);
label = hashmap_get(network->address_labels_by_section, n);
if (label) {
*ret = TAKE_PTR(label);
return 0;
return 0;
}
}
r = address_label_new(&label);
if (r < 0)
return r;
label = new(AddressLabel, 1);
if (!label)
return -ENOMEM;
label->section = TAKE_PTR(n);
*label = (AddressLabel) {
.network = network,
};
r = hashmap_put(network->address_labels_by_section, label->section, label);
if (r < 0)
return r;
label->network = network;
LIST_APPEND(labels, network->address_labels, label);
network->n_address_labels++;
if (filename) {
label->section = TAKE_PTR(n);
r = hashmap_put(network->address_labels_by_section, label->section, label);
if (r < 0)
return r;
}
*ret = TAKE_PTR(label);
return 0;

View File

@ -28,7 +28,6 @@ struct AddressLabel {
LIST_FIELDS(AddressLabel, labels);
};
int address_label_new(AddressLabel **ret);
void address_label_free(AddressLabel *label);
DEFINE_TRIVIAL_CLEANUP_FUNC(AddressLabel*, address_label_free);

View File

@ -67,6 +67,10 @@ int address_new_static(Network *network, const char *filename, unsigned section_
if (r < 0)
return r;
address->network = network;
LIST_APPEND(addresses, network->static_addresses, address);
network->n_static_addresses++;
if (filename) {
address->section = TAKE_PTR(n);
@ -75,10 +79,6 @@ int address_new_static(Network *network, const char *filename, unsigned section_
return r;
}
address->network = network;
LIST_APPEND(addresses, network->static_addresses, address);
network->n_static_addresses++;
*ret = TAKE_PTR(address);
return 0;

View File

@ -153,6 +153,10 @@ int prefix_new_static(Network *network, const char *filename,
if (r < 0)
return r;
prefix->network = network;
LIST_APPEND(prefixes, network->static_prefixes, prefix);
network->n_static_prefixes++;
if (filename) {
prefix->section = TAKE_PTR(n);
@ -162,10 +166,6 @@ int prefix_new_static(Network *network, const char *filename,
return r;
}
prefix->network = network;
LIST_APPEND(prefixes, network->static_prefixes, prefix);
network->n_static_prefixes++;
*ret = TAKE_PTR(prefix);
return 0;

View File

@ -95,6 +95,9 @@ int route_new_static(Network *network, const char *filename, unsigned section_li
return r;
route->protocol = RTPROT_STATIC;
route->network = network;
LIST_PREPEND(routes, network->static_routes, route);
network->n_static_routes++;
if (filename) {
route->section = TAKE_PTR(n);
@ -104,10 +107,6 @@ int route_new_static(Network *network, const char *filename, unsigned section_li
return r;
}
route->network = network;
LIST_PREPEND(routes, network->static_routes, route);
network->n_static_routes++;
*ret = TAKE_PTR(route);
return 0;

View File

@ -40,11 +40,8 @@ void routing_policy_rule_free(RoutingPolicyRule *rule) {
assert(rule->network->n_rules > 0);
rule->network->n_rules--;
if (rule->section) {
if (rule->section)
hashmap_remove(rule->network->rules_by_section, rule->section);
network_config_section_free(rule->section);
}
}
if (rule->manager) {
@ -52,6 +49,7 @@ void routing_policy_rule_free(RoutingPolicyRule *rule) {
set_remove(rule->manager->rules_foreign, rule);
}
network_config_section_free(rule->section);
free(rule->iif);
free(rule->oif);
free(rule);
@ -390,31 +388,35 @@ static int routing_policy_rule_new_static(Network *network, const char *filename
assert(ret);
assert(!!filename == (section_line > 0));
r = network_config_section_new(filename, section_line, &n);
if (r < 0)
return r;
if (filename) {
r = network_config_section_new(filename, section_line, &n);
if (r < 0)
return r;
rule = hashmap_get(network->rules_by_section, n);
if (rule) {
*ret = TAKE_PTR(rule);
rule = hashmap_get(network->rules_by_section, n);
if (rule) {
*ret = TAKE_PTR(rule);
return 0;
return 0;
}
}
r = routing_policy_rule_new(&rule);
if (r < 0)
return r;
rule->section = TAKE_PTR(n);
rule->network = network;
r = hashmap_put(network->rules_by_section, rule->section, rule);
if (r < 0)
return r;
LIST_APPEND(rules, network->rules, rule);
network->n_rules++;
if (filename) {
rule->section = TAKE_PTR(n);
r = hashmap_put(network->rules_by_section, rule->section, rule);
if (r < 0)
return r;
}
*ret = TAKE_PTR(rule);
return 0;