Skip to content

Commit

Permalink
parse: fix redefinition of gateway(4|6)
Browse files Browse the repository at this point in the history
Options gateway4 and gateway6 were not being properly redefined when a
merge happens. Redefinition was causing a crash due to an assert in the
set_str_if_null macro.

While this assertion helped identifying the merge issue, I believe it's
not the best place to keep it so I'm removing it from the macro and
making the macro do only what it name suggests it does.

This macro is used in other places but this problem seems to not happen
there as far as I can see.

Add a couple of unit test to check the merge is working.
  • Loading branch information
daniloegea committed Apr 19, 2024
1 parent 3ab206a commit a670781
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@
#define vxlan_offset(field) GUINT_TO_POINTER(offsetof(NetplanVxlan, field))

/* convenience macro to avoid strdup'ing a string into a field if it's already set. */
#define set_str_if_null(dst, src) { if (dst) {\
g_assert_cmpstr(src, ==, dst); \
} else { \
#define set_str_if_null(dst, src) { if (dst == NULL) {\
dst = g_strdup(src); \
} }

Expand Down Expand Up @@ -1455,6 +1453,10 @@ handle_gateway4(NetplanParser* npp, yaml_node_t* node, __unused const void* _, G
{
if (!is_ip4_address(scalar(node)))
return yaml_error(npp, node, error, "invalid IPv4 address '%s'", scalar(node));
if (npp->current.netdef->gateway4) {
g_free(npp->current.netdef->gateway4);
npp->current.netdef->gateway4 = NULL;
}
set_str_if_null(npp->current.netdef->gateway4, scalar(node));
mark_data_as_dirty(npp, &npp->current.netdef->gateway4);
g_warning("`gateway4` has been deprecated, use default routes instead.\n"
Expand All @@ -1467,6 +1469,10 @@ handle_gateway6(NetplanParser* npp, yaml_node_t* node, __unused const void* _, G
{
if (!is_ip6_address(scalar(node)))
return yaml_error(npp, node, error, "invalid IPv6 address '%s'", scalar(node));
if (npp->current.netdef->gateway6) {
g_free(npp->current.netdef->gateway6);
npp->current.netdef->gateway6 = NULL;
}
set_str_if_null(npp->current.netdef->gateway6, scalar(node));
mark_data_as_dirty(npp, &npp->current.netdef->gateway6);
g_warning("`gateway6` has been deprecated, use default routes instead.\n"
Expand Down
40 changes: 40 additions & 0 deletions tests/generator/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -1825,3 +1825,43 @@ def test_wifi_access_points_overwriting(self):
psk="bbbbbbbb"
}
""")

def test_gateway6_redefinition_works(self):
self.generate('''network:
version: 2
ethernets:
engreen:
addresses: [2001:FFfe::1/62]
gateway6: 2001:FFfe::2''', confs={'b': '''network:
ethernets:
engreen:
gateway6: 2001:FFfe::34'''}, expect_fail=False)

self.assert_networkd({'engreen.network': '''[Match]
Name=engreen
[Network]
LinkLocalAddressing=ipv6
Address=2001:FFfe::1/62
Gateway=2001:FFfe::34
'''})

def test_gateway4_redefinition_works(self):
self.generate('''network:
version: 2
ethernets:
engreen:
addresses: [192.168.0.1/24]
gateway4: 192.168.0.123''', confs={'b': '''network:
ethernets:
engreen:
gateway4: 192.168.0.254'''}, expect_fail=False)

self.assert_networkd({'engreen.network': '''[Match]
Name=engreen
[Network]
LinkLocalAddressing=ipv6
Address=192.168.0.1/24
Gateway=192.168.0.254
'''})

0 comments on commit a670781

Please sign in to comment.