Skip to content

Commit

Permalink
tree-wide: refactoring to avoid Conditionals with Omitted Operands an…
Browse files Browse the repository at this point in the history
…d use proper Boolean conditions
  • Loading branch information
slyon authored and daniloegea committed Aug 23, 2024
1 parent 366b476 commit a21ed4f
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 93 deletions.
2 changes: 1 addition & 1 deletion src/generate.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ int main(int argc, char** argv)
start_unit_jit("systemd-networkd.service");
}
g_autofree char* glob_run = g_build_path(G_DIR_SEPARATOR_S,
rootdir ?: G_DIR_SEPARATOR_S,
rootdir != NULL ? rootdir : G_DIR_SEPARATOR_S,
"run/systemd/system/netplan-*.service",
NULL);
if (!glob(glob_run, 0, NULL, &gl)) {
Expand Down
11 changes: 6 additions & 5 deletions src/netplan.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ STATIC gboolean
write_vxlan(yaml_event_t* event, yaml_emitter_t* emitter, const NetplanNetDefinition* def)
{
if (def->type == NETPLAN_DEF_TYPE_TUNNEL && def->tunnel.mode == NETPLAN_TUNNEL_MODE_VXLAN) {
g_assert(def->vxlan);
g_assert(def->vxlan != NULL);
YAML_UINT_0(def, event, emitter, "id", def->vxlan->vni);
if (def->vxlan->link)
YAML_STRING(def, event, emitter, "link", def->vxlan->link->id);
Expand Down Expand Up @@ -987,7 +987,8 @@ netplan_netdef_write_yaml(
filename = g_strconcat("90-NM-", netdef->backend_settings.uuid, ".yaml", NULL);
else
filename = g_strconcat("10-netplan-", netdef->id, ".yaml", NULL);
path = g_build_path(G_DIR_SEPARATOR_S, rootdir ?: G_DIR_SEPARATOR_S, "etc", "netplan", filename, NULL);
path = g_build_path(G_DIR_SEPARATOR_S, rootdir != NULL ? rootdir : G_DIR_SEPARATOR_S,
"etc", "netplan", filename, NULL);

/* Start rendering YAML output */
yaml_emitter_t emitter_data;
Expand Down Expand Up @@ -1149,7 +1150,7 @@ netplan_state_write_yaml_file(const NetplanState* np_state, const char* filename
GList* to_write = NULL;
int out_fd;

path = g_build_path(G_DIR_SEPARATOR_S, rootdir ?: G_DIR_SEPARATOR_S, "etc", "netplan", filename, NULL);
path = g_build_path(G_DIR_SEPARATOR_S, rootdir != NULL ? rootdir : G_DIR_SEPARATOR_S, "etc", "netplan", filename, NULL);

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

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

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

/* Dump global conf to the default path */
Expand Down
25 changes: 14 additions & 11 deletions src/nm.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ type_str(const NetplanNetDefinition* def)
return "ip-tunnel";
case NETPLAN_DEF_TYPE_NM:
/* needs to be overriden by passthrough "connection.type" setting */
g_assert(def->backend_settings.passthrough);
g_assert(def->backend_settings.passthrough != NULL);
GData *passthrough = def->backend_settings.passthrough;
return g_datalist_get_data(&passthrough, "connection.type");
// LCOV_EXCL_START
Expand Down Expand Up @@ -359,7 +359,7 @@ write_nm_wireguard_params(const NetplanNetDefinition* def, GKeyFile *kf, GError*
if (def->wireguard_peers) {
for (guint i = 0; i < def->wireguard_peers->len; i++) {
NetplanWireguardPeer *peer = g_array_index (def->wireguard_peers, NetplanWireguardPeer*, i);
g_assert(peer->public_key);
g_assert(peer->public_key != NULL);
g_autofree gchar* tmp_group = g_strdup_printf("wireguard-peer.%s", peer->public_key);

if (peer->keepalive)
Expand Down Expand Up @@ -510,7 +510,7 @@ maybe_generate_uuid(const NetplanNetDefinition* def)
STATIC void
write_nm_vxlan_parameters(const NetplanNetDefinition* def, GKeyFile* kf)
{
g_assert(def->vxlan);
g_assert(def->vxlan != NULL);
char uuidstr[37];
if (def->vxlan->ageing)
g_key_file_set_uint64(kf, "vxlan", "ageing", def->vxlan->ageing);
Expand Down Expand Up @@ -604,7 +604,7 @@ write_fallback_key_value(GQuark key_id, gpointer value, gpointer user_data)
} else if (!has_key) {
g_debug("NetworkManager: passing through fallback key: %s.%s=%s", group, k, val);
g_key_file_set_comment(kf, group, k, "Netplan: passthrough setting", NULL);
} else if (!!g_strcmp0(val, old_key)) {
} else if (g_strcmp0(val, old_key) != 0) {
g_debug("NetworkManager: fallback override: %s.%s=%s", group, k, val);
g_key_file_set_comment(kf, group, k, "Netplan: passthrough override", NULL);
}
Expand Down Expand Up @@ -637,7 +637,7 @@ write_nm_conf_access_point(const NetplanNetDefinition* def, const char* rootdir,
const char *match_interface_name = NULL;

if (def->type == NETPLAN_DEF_TYPE_WIFI)
g_assert(ap);
g_assert(ap != NULL);
else
g_assert(ap == NULL);

Expand Down Expand Up @@ -680,7 +680,7 @@ write_nm_conf_access_point(const NetplanNetDefinition* def, const char* rootdir,
if (def->activation_mode) {
/* XXX: For now NetworkManager only supports the "manual" activation
* mode */
if (!!g_strcmp0(def->activation_mode, "manual")) {
if (g_strcmp0(def->activation_mode, "manual") != 0) {
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);
return FALSE;
}
Expand Down Expand Up @@ -983,11 +983,12 @@ write_nm_conf_access_point(const NetplanNetDefinition* def, const char* rootdir,
/* Create /run/NetworkManager/ with 755 permissions if the folder is missing.
* Letting the next invokation of _netplan_safe_mkdir_p_dir do it would
* result in more restrictive access because of the call to umask. */
nm_run_path = g_strjoin(G_DIR_SEPARATOR_S, rootdir ?: "", "run/NetworkManager/", NULL);
nm_run_path = g_strjoin(G_DIR_SEPARATOR_S, rootdir != NULL ? rootdir : "",
"run/NetworkManager/", NULL);
if (!g_file_test(nm_run_path, G_FILE_TEST_EXISTS))
_netplan_safe_mkdir_p_dir(nm_run_path);

full_path = g_strjoin(G_DIR_SEPARATOR_S, rootdir ?: "", conf_path, NULL);
full_path = g_strjoin(G_DIR_SEPARATOR_S, rootdir != NULL ? rootdir : "", conf_path, NULL);

/* NM connection files might contain secrets, and NM insists on tight permissions */
orig_umask = umask(077);
Expand Down Expand Up @@ -1092,7 +1093,7 @@ netplan_state_finish_nm_write(
/* Special case: manage or ignore any device of given type on empty "match: {}" stanza */
if (nd->has_match && !nd->match.driver && !nd->match.mac && !nd->match.original_name) {
nm_type = type_str(nd);
g_assert(nm_type);
g_assert(nm_type != NULL);
g_string_append_printf(nm_conf, "[device-netplan.%s.%s]\nmatch-device=type:%s\n"
"managed=%d\n\n", netplan_def_type_name(nd->type),
netdef_id, nm_type, !unmanaged);
Expand Down Expand Up @@ -1180,8 +1181,10 @@ netplan_state_finish_nm_write(
gboolean
_netplan_nm_cleanup(const char* rootdir)
{
g_autofree char* confpath = g_strjoin(NULL, rootdir ?: "", "/run/NetworkManager/conf.d/netplan.conf", NULL);
g_autofree char* global_manage_path = g_strjoin(NULL, rootdir ?: "", "/run/NetworkManager/conf.d/10-globally-managed-devices.conf", NULL);
g_autofree char* confpath = g_strjoin(NULL, rootdir != NULL ? rootdir : "",
"/run/NetworkManager/conf.d/netplan.conf", NULL);
g_autofree char* global_manage_path = g_strjoin(NULL, rootdir != NULL ? rootdir : "",
"/run/NetworkManager/conf.d/10-globally-managed-devices.conf", NULL);
unlink(confpath);
unlink(global_manage_path);
_netplan_unlink_glob(rootdir, "/run/NetworkManager/system-connections/netplan-*");
Expand Down
13 changes: 7 additions & 6 deletions src/openvswitch.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ STATIC gboolean
write_ovs_systemd_unit(const char* id, const GString* cmds, const char* rootdir, gboolean physical, gboolean cleanup, const char* dependency, GError** error)
{
g_autofree char* escaped_netdef_id = g_uri_escape_string(id, NULL, TRUE);
g_autofree char* link = g_strjoin(NULL, rootdir ?: "", "/run/systemd/system/systemd-networkd.service.wants/netplan-ovs-", escaped_netdef_id, ".service", NULL);
g_autofree char* link = g_strjoin(NULL, rootdir != NULL ? rootdir : "",
"/run/systemd/system/systemd-networkd.service.wants/netplan-ovs-", escaped_netdef_id, ".service", NULL);
g_autofree char* path = g_strjoin(NULL, "/run/systemd/system/netplan-ovs-", escaped_netdef_id, ".service", NULL);

GString* s = g_string_new("[Unit]\n");
Expand Down Expand Up @@ -118,8 +119,8 @@ netplan_type_is_physical(const NetplanDefType type)
STATIC void
write_ovs_tag_setting(const gchar* id, const char* type, const char* col, const char* key, const char* value, GString* cmds)
{
g_assert(col);
g_assert(value);
g_assert(col != NULL);
g_assert(value != NULL);
g_autofree char *clean_value = g_strdup(value);
/* Replace " " -> "," if value contains spaces */
if (strchr(value, ' ')) {
Expand Down Expand Up @@ -260,7 +261,7 @@ write_ovs_bridge_interfaces(const NetplanState* np_state, const NetplanNetDefini
STATIC void
write_ovs_protocols(const NetplanOVSSettings* ovs_settings, const gchar* bridge, GString* cmds)
{
g_assert(bridge);
g_assert(bridge != NULL);
GString* s = g_string_new(g_array_index(ovs_settings->protocols, char*, 0));

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

case NETPLAN_DEF_TYPE_PORT:
g_assert(def->peer);
g_assert(def->peer != NULL);
dependency = def->bridge?: def->bond;
if (!dependency) {
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);
Expand All @@ -405,7 +406,7 @@ _netplan_netdef_write_ovs(const NetplanState* np_state, const NetplanNetDefiniti
break;

case NETPLAN_DEF_TYPE_VLAN:
g_assert(def->vlan_link);
g_assert(def->vlan_link != NULL);
dependency = def->vlan_link->id;
/* Create a fake VLAN bridge */
append_systemd_cmd(cmds, OPENVSWITCH_OVS_VSCTL " --may-exist add-br %s %s %i", def->id, def->vlan_link->id, def->vlan_id)
Expand Down
24 changes: 12 additions & 12 deletions src/parse-nm.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ kf_matches(GKeyFile* kf, const gchar* group, const gchar* key, const gchar* matc
STATIC void
set_true_on_match(GKeyFile* kf, const gchar* group, const gchar* key, const gchar* match, const void* dataptr)
{
g_assert(dataptr);
g_assert(dataptr != NULL);
if (kf_matches(kf, group, key, match)) {
*((gboolean*) dataptr) = TRUE;
_kf_clear_key(kf, group, key);
Expand All @@ -118,16 +118,16 @@ set_true_on_match(GKeyFile* kf, const gchar* group, const gchar* key, const gcha
STATIC void
keyfile_handle_generic_bool(GKeyFile* kf, const gchar* group, const gchar* key, gboolean* dataptr)
{
g_assert(dataptr);
g_assert(dataptr != NULL);
*dataptr = g_key_file_get_boolean(kf, group, key, NULL);
_kf_clear_key(kf, group, key);
}

STATIC void
keyfile_handle_generic_str(GKeyFile* kf, const gchar* group, const gchar* key, char** dataptr)
{
g_assert(dataptr);
g_assert(!*dataptr);
g_assert(dataptr != NULL);
g_assert(*dataptr == NULL);
*dataptr = g_key_file_get_string(kf, group, key, NULL);
if (*dataptr)
_kf_clear_key(kf, group, key);
Expand All @@ -136,7 +136,7 @@ keyfile_handle_generic_str(GKeyFile* kf, const gchar* group, const gchar* key, c
STATIC void
keyfile_handle_generic_uint(GKeyFile* kf, const gchar* group, const gchar* key, guint* dataptr, guint default_value)
{
g_assert(dataptr);
g_assert(dataptr != NULL);
if (g_key_file_has_key(kf, group, key, NULL)) {
guint data = g_key_file_get_uint64(kf, group, key, NULL);
if (data != default_value)
Expand Down Expand Up @@ -176,7 +176,7 @@ keyfile_handle_cloned_mac_address(GKeyFile *kf, NetplanNetDefinition* nd, const
STATIC void
parse_addresses(GKeyFile* kf, const gchar* group, GArray** ip_arr)
{
g_assert(ip_arr);
g_assert(ip_arr != NULL);
if (kf_matches(kf, group, "method", "manual")) {
gboolean unhandled_data = FALSE;
gchar *key = NULL;
Expand Down Expand Up @@ -216,7 +216,7 @@ parse_addresses(GKeyFile* kf, const gchar* group, GArray** ip_arr)
STATIC void
parse_routes(GKeyFile* kf, const gchar* group, GArray** routes_arr)
{
g_assert(routes_arr);
g_assert(routes_arr != NULL);
NetplanIPRoute *route = NULL;
gchar **split = NULL;
for (unsigned i = 1;; ++i) {
Expand Down Expand Up @@ -306,7 +306,7 @@ parse_routes(GKeyFile* kf, const gchar* group, GArray** routes_arr)
STATIC void
parse_dhcp_overrides(GKeyFile* kf, const gchar* group, NetplanDHCPOverrides* dataptr)
{
g_assert(dataptr);
g_assert(dataptr != NULL);
if ( g_key_file_get_boolean(kf, group, "ignore-auto-routes", NULL)
&& g_key_file_get_boolean(kf, group, "never-default", NULL)) {
(*dataptr).use_routes = FALSE;
Expand All @@ -322,7 +322,7 @@ parse_search_domains(GKeyFile* kf, const gchar* group, GArray** domains_arr)
{
// Keep "dns-search" as fallback/passthrough, as netplan cannot
// differentiate between ipv4.dns-search and ipv6.dns-search
g_assert(domains_arr);
g_assert(domains_arr != NULL);
gsize len = 0;
gchar **split = g_key_file_get_string_list(kf, group, "dns-search", &len, NULL);
if (split) {
Expand All @@ -347,7 +347,7 @@ parse_search_domains(GKeyFile* kf, const gchar* group, GArray** domains_arr)
STATIC void
parse_nameservers(GKeyFile* kf, const gchar* group, GArray** nameserver_arr)
{
g_assert(nameserver_arr);
g_assert(nameserver_arr != NULL);
gchar **split = g_key_file_get_string_list(kf, group, "dns", NULL, NULL);
if (split) {

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

if (method && g_strcmp0(method, "") != 0) {
Expand Down Expand Up @@ -431,7 +431,7 @@ parse_dot1x_auth(GKeyFile* kf, NetplanAuthenticationSettings* auth)
STATIC void
parse_bond_arp_ip_targets(GKeyFile* kf, GArray **targets_arr)
{
g_assert(targets_arr);
g_assert(targets_arr != NULL);
g_autofree gchar *v = g_key_file_get_string(kf, "bond", "arp_ip_target", NULL);
if (v) {
gchar** split = g_strsplit(v, ",", -1);
Expand Down
Loading

0 comments on commit a21ed4f

Please sign in to comment.