Skip to content

Commit d4e5955

Browse files
committed
tree-wide: refactoring to avoid Conditionals with Omitted Operands and use proper Boolean conditions
1 parent 4121cd5 commit d4e5955

File tree

9 files changed

+103
-93
lines changed

9 files changed

+103
-93
lines changed

src/generate.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ int main(int argc, char** argv)
340340
start_unit_jit("systemd-networkd.service");
341341
}
342342
g_autofree char* glob_run = g_build_path(G_DIR_SEPARATOR_S,
343-
rootdir ?: G_DIR_SEPARATOR_S,
343+
rootdir != NULL ? rootdir : G_DIR_SEPARATOR_S,
344344
"run/systemd/system/netplan-*.service",
345345
NULL);
346346
if (!glob(glob_run, 0, NULL, &gl)) {

src/netplan.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ STATIC gboolean
209209
write_vxlan(yaml_event_t* event, yaml_emitter_t* emitter, const NetplanNetDefinition* def)
210210
{
211211
if (def->type == NETPLAN_DEF_TYPE_TUNNEL && def->tunnel.mode == NETPLAN_TUNNEL_MODE_VXLAN) {
212-
g_assert(def->vxlan);
212+
g_assert(def->vxlan != NULL);
213213
YAML_UINT_0(def, event, emitter, "id", def->vxlan->vni);
214214
if (def->vxlan->link)
215215
YAML_STRING(def, event, emitter, "link", def->vxlan->link->id);
@@ -987,7 +987,8 @@ netplan_netdef_write_yaml(
987987
filename = g_strconcat("90-NM-", netdef->backend_settings.uuid, ".yaml", NULL);
988988
else
989989
filename = g_strconcat("10-netplan-", netdef->id, ".yaml", NULL);
990-
path = g_build_path(G_DIR_SEPARATOR_S, rootdir ?: G_DIR_SEPARATOR_S, "etc", "netplan", filename, NULL);
990+
path = g_build_path(G_DIR_SEPARATOR_S, rootdir != NULL ? rootdir : G_DIR_SEPARATOR_S,
991+
"etc", "netplan", filename, NULL);
991992

992993
/* Start rendering YAML output */
993994
yaml_emitter_t emitter_data;
@@ -1149,7 +1150,7 @@ netplan_state_write_yaml_file(const NetplanState* np_state, const char* filename
11491150
GList* to_write = NULL;
11501151
int out_fd;
11511152

1152-
path = g_build_path(G_DIR_SEPARATOR_S, rootdir ?: G_DIR_SEPARATOR_S, "etc", "netplan", filename, NULL);
1153+
path = g_build_path(G_DIR_SEPARATOR_S, rootdir != NULL ? rootdir : G_DIR_SEPARATOR_S, "etc", "netplan", filename, NULL);
11531154

11541155
while (iter) {
11551156
NetplanNetDefinition* netdef = iter->data;
@@ -1160,7 +1161,7 @@ netplan_state_write_yaml_file(const NetplanState* np_state, const char* filename
11601161
}
11611162

11621163
/* Remove any existing file if there is no data to write */
1163-
gboolean write_globals = !!np_state->global_renderer;
1164+
gboolean write_globals = np_state->global_renderer != NULL;
11641165
if (to_write == NULL && !write_globals) {
11651166
if (unlink(path) && errno != ENOENT) {
11661167
g_set_error(error, NETPLAN_FILE_ERROR, errno, "%m");
@@ -1218,7 +1219,7 @@ netplan_state_update_yaml_hierarchy(const NetplanState* np_state, const char* de
12181219
g_assert(default_filename != NULL && *default_filename != '\0');
12191220

12201221
perfile_netdefs = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify)g_list_free);
1221-
default_path = g_build_path(G_DIR_SEPARATOR_S, rootdir ?: G_DIR_SEPARATOR_S, "etc", "netplan", default_filename, NULL);
1222+
default_path = g_build_path(G_DIR_SEPARATOR_S, rootdir != NULL ? rootdir : G_DIR_SEPARATOR_S, "etc", "netplan", default_filename, NULL);
12221223
int out_fd = -1;
12231224

12241225
/* Dump global conf to the default path */

src/nm.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ type_str(const NetplanNetDefinition* def)
9797
return "ip-tunnel";
9898
case NETPLAN_DEF_TYPE_NM:
9999
/* needs to be overriden by passthrough "connection.type" setting */
100-
g_assert(def->backend_settings.passthrough);
100+
g_assert(def->backend_settings.passthrough != NULL);
101101
GData *passthrough = def->backend_settings.passthrough;
102102
return g_datalist_get_data(&passthrough, "connection.type");
103103
// LCOV_EXCL_START
@@ -359,7 +359,7 @@ write_nm_wireguard_params(const NetplanNetDefinition* def, GKeyFile *kf, GError*
359359
if (def->wireguard_peers) {
360360
for (guint i = 0; i < def->wireguard_peers->len; i++) {
361361
NetplanWireguardPeer *peer = g_array_index (def->wireguard_peers, NetplanWireguardPeer*, i);
362-
g_assert(peer->public_key);
362+
g_assert(peer->public_key != NULL);
363363
g_autofree gchar* tmp_group = g_strdup_printf("wireguard-peer.%s", peer->public_key);
364364

365365
if (peer->keepalive)
@@ -510,7 +510,7 @@ maybe_generate_uuid(const NetplanNetDefinition* def)
510510
STATIC void
511511
write_nm_vxlan_parameters(const NetplanNetDefinition* def, GKeyFile* kf)
512512
{
513-
g_assert(def->vxlan);
513+
g_assert(def->vxlan != NULL);
514514
char uuidstr[37];
515515
if (def->vxlan->ageing)
516516
g_key_file_set_uint64(kf, "vxlan", "ageing", def->vxlan->ageing);
@@ -604,7 +604,7 @@ write_fallback_key_value(GQuark key_id, gpointer value, gpointer user_data)
604604
} else if (!has_key) {
605605
g_debug("NetworkManager: passing through fallback key: %s.%s=%s", group, k, val);
606606
g_key_file_set_comment(kf, group, k, "Netplan: passthrough setting", NULL);
607-
} else if (!!g_strcmp0(val, old_key)) {
607+
} else if (g_strcmp0(val, old_key) != 0) {
608608
g_debug("NetworkManager: fallback override: %s.%s=%s", group, k, val);
609609
g_key_file_set_comment(kf, group, k, "Netplan: passthrough override", NULL);
610610
}
@@ -637,7 +637,7 @@ write_nm_conf_access_point(const NetplanNetDefinition* def, const char* rootdir,
637637
const char *match_interface_name = NULL;
638638

639639
if (def->type == NETPLAN_DEF_TYPE_WIFI)
640-
g_assert(ap);
640+
g_assert(ap != NULL);
641641
else
642642
g_assert(ap == NULL);
643643

@@ -680,7 +680,7 @@ write_nm_conf_access_point(const NetplanNetDefinition* def, const char* rootdir,
680680
if (def->activation_mode) {
681681
/* XXX: For now NetworkManager only supports the "manual" activation
682682
* mode */
683-
if (!!g_strcmp0(def->activation_mode, "manual")) {
683+
if (g_strcmp0(def->activation_mode, "manual") != 0) {
684684
g_set_error(error, NETPLAN_BACKEND_ERROR, NETPLAN_ERROR_UNSUPPORTED, "ERROR: %s: NetworkManager definitions do not support activation-mode %s\n", def->id, def->activation_mode);
685685
return FALSE;
686686
}
@@ -983,11 +983,12 @@ write_nm_conf_access_point(const NetplanNetDefinition* def, const char* rootdir,
983983
/* Create /run/NetworkManager/ with 755 permissions if the folder is missing.
984984
* Letting the next invokation of _netplan_safe_mkdir_p_dir do it would
985985
* result in more restrictive access because of the call to umask. */
986-
nm_run_path = g_strjoin(G_DIR_SEPARATOR_S, rootdir ?: "", "run/NetworkManager/", NULL);
986+
nm_run_path = g_strjoin(G_DIR_SEPARATOR_S, rootdir != NULL ? rootdir : "",
987+
"run/NetworkManager/", NULL);
987988
if (!g_file_test(nm_run_path, G_FILE_TEST_EXISTS))
988989
_netplan_safe_mkdir_p_dir(nm_run_path);
989990

990-
full_path = g_strjoin(G_DIR_SEPARATOR_S, rootdir ?: "", conf_path, NULL);
991+
full_path = g_strjoin(G_DIR_SEPARATOR_S, rootdir != NULL ? rootdir : "", conf_path, NULL);
991992

992993
/* NM connection files might contain secrets, and NM insists on tight permissions */
993994
orig_umask = umask(077);
@@ -1092,7 +1093,7 @@ netplan_state_finish_nm_write(
10921093
/* Special case: manage or ignore any device of given type on empty "match: {}" stanza */
10931094
if (nd->has_match && !nd->match.driver && !nd->match.mac && !nd->match.original_name) {
10941095
nm_type = type_str(nd);
1095-
g_assert(nm_type);
1096+
g_assert(nm_type != NULL);
10961097
g_string_append_printf(nm_conf, "[device-netplan.%s.%s]\nmatch-device=type:%s\n"
10971098
"managed=%d\n\n", netplan_def_type_name(nd->type),
10981099
netdef_id, nm_type, !unmanaged);
@@ -1180,8 +1181,10 @@ netplan_state_finish_nm_write(
11801181
gboolean
11811182
_netplan_nm_cleanup(const char* rootdir)
11821183
{
1183-
g_autofree char* confpath = g_strjoin(NULL, rootdir ?: "", "/run/NetworkManager/conf.d/netplan.conf", NULL);
1184-
g_autofree char* global_manage_path = g_strjoin(NULL, rootdir ?: "", "/run/NetworkManager/conf.d/10-globally-managed-devices.conf", NULL);
1184+
g_autofree char* confpath = g_strjoin(NULL, rootdir != NULL ? rootdir : "",
1185+
"/run/NetworkManager/conf.d/netplan.conf", NULL);
1186+
g_autofree char* global_manage_path = g_strjoin(NULL, rootdir != NULL ? rootdir : "",
1187+
"/run/NetworkManager/conf.d/10-globally-managed-devices.conf", NULL);
11851188
unlink(confpath);
11861189
unlink(global_manage_path);
11871190
_netplan_unlink_glob(rootdir, "/run/NetworkManager/system-connections/netplan-*");

src/openvswitch.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ STATIC gboolean
3232
write_ovs_systemd_unit(const char* id, const GString* cmds, const char* rootdir, gboolean physical, gboolean cleanup, const char* dependency, GError** error)
3333
{
3434
g_autofree char* escaped_netdef_id = g_uri_escape_string(id, NULL, TRUE);
35-
g_autofree char* link = g_strjoin(NULL, rootdir ?: "", "/run/systemd/system/systemd-networkd.service.wants/netplan-ovs-", escaped_netdef_id, ".service", NULL);
35+
g_autofree char* link = g_strjoin(NULL, rootdir != NULL ? rootdir : "",
36+
"/run/systemd/system/systemd-networkd.service.wants/netplan-ovs-", escaped_netdef_id, ".service", NULL);
3637
g_autofree char* path = g_strjoin(NULL, "/run/systemd/system/netplan-ovs-", escaped_netdef_id, ".service", NULL);
3738

3839
GString* s = g_string_new("[Unit]\n");
@@ -118,8 +119,8 @@ netplan_type_is_physical(const NetplanDefType type)
118119
STATIC void
119120
write_ovs_tag_setting(const gchar* id, const char* type, const char* col, const char* key, const char* value, GString* cmds)
120121
{
121-
g_assert(col);
122-
g_assert(value);
122+
g_assert(col != NULL);
123+
g_assert(value != NULL);
123124
g_autofree char *clean_value = g_strdup(value);
124125
/* Replace " " -> "," if value contains spaces */
125126
if (strchr(value, ' ')) {
@@ -260,7 +261,7 @@ write_ovs_bridge_interfaces(const NetplanState* np_state, const NetplanNetDefini
260261
STATIC void
261262
write_ovs_protocols(const NetplanOVSSettings* ovs_settings, const gchar* bridge, GString* cmds)
262263
{
263-
g_assert(bridge);
264+
g_assert(bridge != NULL);
264265
GString* s = g_string_new(g_array_index(ovs_settings->protocols, char*, 0));
265266

266267
for (unsigned i = 1; i < ovs_settings->protocols->len; ++i)
@@ -389,7 +390,7 @@ _netplan_netdef_write_ovs(const NetplanState* np_state, const NetplanNetDefiniti
389390
break;
390391

391392
case NETPLAN_DEF_TYPE_PORT:
392-
g_assert(def->peer);
393+
g_assert(def->peer != NULL);
393394
dependency = def->bridge?: def->bond;
394395
if (!dependency) {
395396
g_set_error(error, NETPLAN_BACKEND_ERROR, NETPLAN_ERROR_VALIDATION, "%s: OpenVSwitch patch port needs to be assigned to a bridge/bond\n", def->id);
@@ -405,7 +406,7 @@ _netplan_netdef_write_ovs(const NetplanState* np_state, const NetplanNetDefiniti
405406
break;
406407

407408
case NETPLAN_DEF_TYPE_VLAN:
408-
g_assert(def->vlan_link);
409+
g_assert(def->vlan_link != NULL);
409410
dependency = def->vlan_link->id;
410411
/* Create a fake VLAN bridge */
411412
append_systemd_cmd(cmds, OPENVSWITCH_OVS_VSCTL " --may-exist add-br %s %s %i", def->id, def->vlan_link->id, def->vlan_id)

src/parse-nm.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ kf_matches(GKeyFile* kf, const gchar* group, const gchar* key, const gchar* matc
108108
STATIC void
109109
set_true_on_match(GKeyFile* kf, const gchar* group, const gchar* key, const gchar* match, const void* dataptr)
110110
{
111-
g_assert(dataptr);
111+
g_assert(dataptr != NULL);
112112
if (kf_matches(kf, group, key, match)) {
113113
*((gboolean*) dataptr) = TRUE;
114114
_kf_clear_key(kf, group, key);
@@ -118,16 +118,16 @@ set_true_on_match(GKeyFile* kf, const gchar* group, const gchar* key, const gcha
118118
STATIC void
119119
keyfile_handle_generic_bool(GKeyFile* kf, const gchar* group, const gchar* key, gboolean* dataptr)
120120
{
121-
g_assert(dataptr);
121+
g_assert(dataptr != NULL);
122122
*dataptr = g_key_file_get_boolean(kf, group, key, NULL);
123123
_kf_clear_key(kf, group, key);
124124
}
125125

126126
STATIC void
127127
keyfile_handle_generic_str(GKeyFile* kf, const gchar* group, const gchar* key, char** dataptr)
128128
{
129-
g_assert(dataptr);
130-
g_assert(!*dataptr);
129+
g_assert(dataptr != NULL);
130+
g_assert(*dataptr == NULL);
131131
*dataptr = g_key_file_get_string(kf, group, key, NULL);
132132
if (*dataptr)
133133
_kf_clear_key(kf, group, key);
@@ -136,7 +136,7 @@ keyfile_handle_generic_str(GKeyFile* kf, const gchar* group, const gchar* key, c
136136
STATIC void
137137
keyfile_handle_generic_uint(GKeyFile* kf, const gchar* group, const gchar* key, guint* dataptr, guint default_value)
138138
{
139-
g_assert(dataptr);
139+
g_assert(dataptr != NULL);
140140
if (g_key_file_has_key(kf, group, key, NULL)) {
141141
guint data = g_key_file_get_uint64(kf, group, key, NULL);
142142
if (data != default_value)
@@ -176,7 +176,7 @@ keyfile_handle_cloned_mac_address(GKeyFile *kf, NetplanNetDefinition* nd, const
176176
STATIC void
177177
parse_addresses(GKeyFile* kf, const gchar* group, GArray** ip_arr)
178178
{
179-
g_assert(ip_arr);
179+
g_assert(ip_arr != NULL);
180180
if (kf_matches(kf, group, "method", "manual")) {
181181
gboolean unhandled_data = FALSE;
182182
gchar *key = NULL;
@@ -216,7 +216,7 @@ parse_addresses(GKeyFile* kf, const gchar* group, GArray** ip_arr)
216216
STATIC void
217217
parse_routes(GKeyFile* kf, const gchar* group, GArray** routes_arr)
218218
{
219-
g_assert(routes_arr);
219+
g_assert(routes_arr != NULL);
220220
NetplanIPRoute *route = NULL;
221221
gchar **split = NULL;
222222
for (unsigned i = 1;; ++i) {
@@ -306,7 +306,7 @@ parse_routes(GKeyFile* kf, const gchar* group, GArray** routes_arr)
306306
STATIC void
307307
parse_dhcp_overrides(GKeyFile* kf, const gchar* group, NetplanDHCPOverrides* dataptr)
308308
{
309-
g_assert(dataptr);
309+
g_assert(dataptr != NULL);
310310
if ( g_key_file_get_boolean(kf, group, "ignore-auto-routes", NULL)
311311
&& g_key_file_get_boolean(kf, group, "never-default", NULL)) {
312312
(*dataptr).use_routes = FALSE;
@@ -322,7 +322,7 @@ parse_search_domains(GKeyFile* kf, const gchar* group, GArray** domains_arr)
322322
{
323323
// Keep "dns-search" as fallback/passthrough, as netplan cannot
324324
// differentiate between ipv4.dns-search and ipv6.dns-search
325-
g_assert(domains_arr);
325+
g_assert(domains_arr != NULL);
326326
gsize len = 0;
327327
gchar **split = g_key_file_get_string_list(kf, group, "dns-search", &len, NULL);
328328
if (split) {
@@ -347,7 +347,7 @@ parse_search_domains(GKeyFile* kf, const gchar* group, GArray** domains_arr)
347347
STATIC void
348348
parse_nameservers(GKeyFile* kf, const gchar* group, GArray** nameserver_arr)
349349
{
350-
g_assert(nameserver_arr);
350+
g_assert(nameserver_arr != NULL);
351351
gchar **split = g_key_file_get_string_list(kf, group, "dns", NULL, NULL);
352352
if (split) {
353353

@@ -381,7 +381,7 @@ parse_nameservers(GKeyFile* kf, const gchar* group, GArray** nameserver_arr)
381381
STATIC void
382382
parse_dot1x_auth(GKeyFile* kf, NetplanAuthenticationSettings* auth)
383383
{
384-
g_assert(auth);
384+
g_assert(auth != NULL);
385385
g_autofree gchar* method = g_key_file_get_string(kf, "802-1x", "eap", NULL);
386386

387387
if (method && g_strcmp0(method, "") != 0) {
@@ -431,7 +431,7 @@ parse_dot1x_auth(GKeyFile* kf, NetplanAuthenticationSettings* auth)
431431
STATIC void
432432
parse_bond_arp_ip_targets(GKeyFile* kf, GArray **targets_arr)
433433
{
434-
g_assert(targets_arr);
434+
g_assert(targets_arr != NULL);
435435
g_autofree gchar *v = g_key_file_get_string(kf, "bond", "arp_ip_target", NULL);
436436
if (v) {
437437
gchar** split = g_strsplit(v, ",", -1);

0 commit comments

Comments
 (0)