From 3bda4f7d2eee54c16bf0666a595b02f7755a78d9 Mon Sep 17 00:00:00 2001 From: Danilo Egea Gondolfo Date: Tue, 6 Aug 2024 10:10:02 +0100 Subject: [PATCH 01/12] meson: enable -Wconversion by default This flag will make the compiler to issue a warning for all the dangerous implicit type conversions. --- meson.build | 1 + 1 file changed, 1 insertion(+) diff --git a/meson.build b/meson.build index 0da0d7ff5..68c740f96 100644 --- a/meson.build +++ b/meson.build @@ -33,6 +33,7 @@ find = find_program('find') add_project_arguments( '-DSBINDIR="' + join_paths(get_option('prefix'), get_option('sbindir')) + '"', '-D_GNU_SOURCE', + '-Wconversion', language: 'c') inc = include_directories('include') From 1c51cc3374473a6209f12fa1a8b3ad705b4e842f Mon Sep 17 00:00:00 2001 From: Danilo Egea Gondolfo Date: Tue, 6 Aug 2024 10:12:07 +0100 Subject: [PATCH 02/12] error.c: fix implicit conversions --- src/error.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/error.c b/src/error.c index c1bc27b0b..2d2b12bbe 100644 --- a/src/error.c +++ b/src/error.c @@ -32,18 +32,16 @@ ****************************************************/ STATIC void -write_error_marker(GString *message, int column) +write_error_marker(GString *message, size_t column) { - int i; - - for (i = 0; (column > 0 && i < column); i++) + for (size_t i = 0; (column > 0 && i < column); i++) g_string_append_printf(message, " "); g_string_append_printf(message, "^"); } STATIC char * -get_syntax_error_context(const NetplanParser* npp, const int line_num, const int column, GError **error) +get_syntax_error_context(const NetplanParser* npp, const size_t line_num, const size_t column, GError **error) { GString *message = NULL; GFile *cur_file = g_file_new_for_path(npp->current.filepath); @@ -57,7 +55,7 @@ get_syntax_error_context(const NetplanParser* npp, const int line_num, const int stream = g_data_input_stream_new (G_INPUT_STREAM(file_stream)); g_object_unref(file_stream); - for (int i = 0; i < line_num + 1; i++) { + for (size_t i = 0; i < line_num + 1; i++) { g_free(line); line = g_data_input_stream_read_line(stream, &len, NULL, error); } From 16e1c10fe81d088e5ebb9c4df32f64fa53c82a24 Mon Sep 17 00:00:00 2001 From: Danilo Egea Gondolfo Date: Tue, 6 Aug 2024 10:22:16 +0100 Subject: [PATCH 03/12] netplan.c: fix implicit conversions --- src/netplan.c | 2 +- src/yaml-helpers.h | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/netplan.c b/src/netplan.c index 6cf739708..d6ab84902 100644 --- a/src/netplan.c +++ b/src/netplan.c @@ -553,7 +553,7 @@ write_tunnel_settings(yaml_event_t* event, yaml_emitter_t* emitter, const Netpla YAML_SCALAR_PLAIN(event, emitter, "private-key-flags"); YAML_SEQUENCE_OPEN(event, emitter); - for(int i = 1; i < NETPLAN_KEY_FLAG_MAX_; i <<= 1) { + for(guint i = 1; i < NETPLAN_KEY_FLAG_MAX_; i <<= 1) { if (def->tunnel_private_key_flags & i) YAML_SCALAR_PLAIN(event, emitter, netplan_key_flags_name(i)); } diff --git a/src/yaml-helpers.h b/src/yaml-helpers.h index 466c41b57..e3cd76687 100644 --- a/src/yaml-helpers.h +++ b/src/yaml-helpers.h @@ -17,6 +17,7 @@ #pragma once +#include #include #define YAML_MAPPING_OPEN(event_ptr, emitter_ptr) \ @@ -41,7 +42,9 @@ } #define YAML_SCALAR_PLAIN(event_ptr, emitter_ptr, scalar) \ { \ - yaml_scalar_event_initialize(event_ptr, NULL, (yaml_char_t *)YAML_STR_TAG, (yaml_char_t *)scalar, strlen(scalar), 1, 0, YAML_PLAIN_SCALAR_STYLE); \ + size_t _length = strlen(scalar); \ + g_assert(_length < G_MAXINT); \ + yaml_scalar_event_initialize(event_ptr, NULL, (yaml_char_t *)YAML_STR_TAG, (yaml_char_t *)scalar, (int)_length, 1, 0, YAML_PLAIN_SCALAR_STYLE); \ if (!yaml_emitter_emit(emitter_ptr, event_ptr)) goto err_path; \ } @@ -50,13 +53,15 @@ YAML_SCALAR_PLAIN(event_ptr, emitter_ptr, flags_func(flag)); #define YAML_NULL_PLAIN(event_ptr, emitter_ptr) \ - yaml_scalar_event_initialize(event_ptr, NULL, (yaml_char_t*)YAML_NULL_TAG, (yaml_char_t*)"null", strlen("null"), 1, 0, YAML_PLAIN_SCALAR_STYLE); \ + yaml_scalar_event_initialize(event_ptr, NULL, (yaml_char_t*)YAML_NULL_TAG, (yaml_char_t*)"null", (int)strlen("null"), 1, 0, YAML_PLAIN_SCALAR_STYLE); \ if (!yaml_emitter_emit(emitter_ptr, event_ptr)) goto err_path; \ /* Implicit plain and quoted tags, double quoted style */ #define YAML_SCALAR_QUOTED(event_ptr, emitter_ptr, scalar) \ { \ - yaml_scalar_event_initialize(event_ptr, NULL, (yaml_char_t *)YAML_STR_TAG, (yaml_char_t *)scalar, strlen(scalar), 1, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE); \ + size_t _length = strlen(scalar); \ + g_assert(_length < G_MAXINT); \ + yaml_scalar_event_initialize(event_ptr, NULL, (yaml_char_t *)YAML_STR_TAG, (yaml_char_t *)scalar, (int)_length, 1, 1, YAML_DOUBLE_QUOTED_SCALAR_STYLE); \ if (!yaml_emitter_emit(emitter_ptr, event_ptr)) goto err_path; \ } #define YAML_NONNULL_STRING(event_ptr, emitter_ptr, key, value_ptr) \ From a0d248858712ef090cd4d213e818cdff54ced538 Mon Sep 17 00:00:00 2001 From: Danilo Egea Gondolfo Date: Tue, 6 Aug 2024 10:32:08 +0100 Subject: [PATCH 04/12] util.c: fix implicit conversions wifi_get_freq24 and wifi_get_freq5 are used with ap->channel values, which are guint. Change these functions to take guint as argument. --- src/util-internal.h | 4 ++-- src/util.c | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/util-internal.h b/src/util-internal.h index 3695aeec3..d073dea8d 100644 --- a/src/util-internal.h +++ b/src/util-internal.h @@ -56,10 +56,10 @@ const char* get_unspecified_address(int ip_family); int -wifi_get_freq24(int channel); +wifi_get_freq24(guint channel); int -wifi_get_freq5(int channel); +wifi_get_freq5(guint channel); gchar* systemd_escape(char* string); diff --git a/src/util.c b/src/util.c index 7957644ef..fdab5ab0b 100644 --- a/src/util.c +++ b/src/util.c @@ -440,7 +440,7 @@ netplan_util_dump_yaml_subtree(const char* prefix, int input_fd, int output_fd, * Get the frequency of a given 2.4GHz WiFi channel */ int -wifi_get_freq24(int channel) +wifi_get_freq24(guint channel) { if (channel < 1 || channel > 14) { g_fprintf(stderr, "ERROR: invalid 2.4GHz WiFi channel: %d\n", channel); @@ -466,9 +466,9 @@ wifi_get_freq24(int channel) * Get the frequency of a given 5GHz WiFi channel */ int -wifi_get_freq5(int channel) +wifi_get_freq5(guint channel) { - int channels[] = { 7, 8, 9, 11, 12, 16, 32, 34, 36, 38, 40, 42, 44, 46, 48, + guint channels[] = { 7, 8, 9, 11, 12, 16, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 68, 96, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 132, 134, 136, 138, 140, 142, 144, 149, 151, 153, From ec0466960a27044c2c212c90226135750b8c6b05 Mon Sep 17 00:00:00 2001 From: Danilo Egea Gondolfo Date: Tue, 6 Aug 2024 10:45:15 +0100 Subject: [PATCH 05/12] nm.c: fix implicit conversions --- src/nm.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/nm.c b/src/nm.c index f23f28693..3dbb85bfc 100644 --- a/src/nm.c +++ b/src/nm.c @@ -268,7 +268,7 @@ write_nm_bond_parameters(const NetplanNetDefinition* def, GKeyFile *kf) if (def->bond_params.monitor_interval) g_key_file_set_string(kf, "bond", "miimon", def->bond_params.monitor_interval); if (def->bond_params.min_links) - g_key_file_set_integer(kf, "bond", "min_links", def->bond_params.min_links); + g_key_file_set_uint64(kf, "bond", "min_links", def->bond_params.min_links); if (def->bond_params.transmit_hash_policy) g_key_file_set_string(kf, "bond", "xmit_hash_policy", def->bond_params.transmit_hash_policy); if (def->bond_params.selection_logic) @@ -298,17 +298,17 @@ write_nm_bond_parameters(const NetplanNetDefinition* def, GKeyFile *kf) if (def->bond_params.fail_over_mac_policy) g_key_file_set_string(kf, "bond", "fail_over_mac", def->bond_params.fail_over_mac_policy); if (def->bond_params.gratuitous_arp) { - g_key_file_set_integer(kf, "bond", "num_grat_arp", def->bond_params.gratuitous_arp); + g_key_file_set_uint64(kf, "bond", "num_grat_arp", def->bond_params.gratuitous_arp); /* Work around issue in NM where unset unsolicited_na will overwrite num_grat_arp: * https://github.com/NetworkManager/NetworkManager/commit/42b0bef33c77a0921590b2697f077e8ea7805166 */ - g_key_file_set_integer(kf, "bond", "num_unsol_na", def->bond_params.gratuitous_arp); + g_key_file_set_uint64(kf, "bond", "num_unsol_na", def->bond_params.gratuitous_arp); } if (def->bond_params.packets_per_member) - g_key_file_set_integer(kf, "bond", "packets_per_slave", def->bond_params.packets_per_member); /* wokeignore:rule=slave */ + g_key_file_set_uint64(kf, "bond", "packets_per_slave", def->bond_params.packets_per_member); /* wokeignore:rule=slave */ if (def->bond_params.primary_reselect_policy) g_key_file_set_string(kf, "bond", "primary_reselect", def->bond_params.primary_reselect_policy); if (def->bond_params.resend_igmp) - g_key_file_set_integer(kf, "bond", "resend_igmp", def->bond_params.resend_igmp); + g_key_file_set_uint64(kf, "bond", "resend_igmp", def->bond_params.resend_igmp); if (def->bond_params.learn_interval) g_key_file_set_string(kf, "bond", "lp_interval", def->bond_params.learn_interval); if (def->bond_params.primary_member) @@ -363,7 +363,7 @@ write_nm_wireguard_params(const NetplanNetDefinition* def, GKeyFile *kf, GError* g_autofree gchar* tmp_group = g_strdup_printf("wireguard-peer.%s", peer->public_key); if (peer->keepalive) - g_key_file_set_integer(kf, tmp_group, "persistent-keepalive", peer->keepalive); + g_key_file_set_uint64(kf, tmp_group, "persistent-keepalive", peer->keepalive); if (peer->endpoint) g_key_file_set_string(kf, tmp_group, "endpoint", peer->endpoint); @@ -791,7 +791,7 @@ write_nm_conf_access_point(const NetplanNetDefinition* def, const char* rootdir, if (def->mtubytes) g_key_file_set_uint64(kf, nm_type, "mtu", def->mtubytes); if (def->wowlan && def->wowlan > NETPLAN_WIFI_WOWLAN_DEFAULT) - g_key_file_set_uint64(kf, nm_type, "wake-on-wlan", def->wowlan); + g_key_file_set_integer(kf, nm_type, "wake-on-wlan", def->wowlan); if (def->ib_mode != NETPLAN_IB_MODE_KERNEL) g_key_file_set_string(kf, nm_type, "transport-mode", netplan_infiniband_mode_name(def->ib_mode)); } From e2743234644d46541ee5ceb00c0692685e3be0d5 Mon Sep 17 00:00:00 2001 From: Danilo Egea Gondolfo Date: Tue, 6 Aug 2024 11:08:28 +0100 Subject: [PATCH 06/12] openvswitch.c: fix implicit conversions --- src/openvswitch.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/openvswitch.c b/src/openvswitch.c index 09864b560..2c53c5c61 100644 --- a/src/openvswitch.c +++ b/src/openvswitch.c @@ -307,7 +307,8 @@ write_ovs_bridge_controller_targets(const NetplanOVSSettings* settings, const Ne } g_string_append_printf(s, "%s ", target); } - g_string_erase(s, s->len-1, 1); + g_assert(s->len < G_MAXSSIZE); + g_string_erase(s, (gssize)s->len-1, 1); append_systemd_cmd(cmds, OPENVSWITCH_OVS_VSCTL " set-controller %s %s", bridge, s->str); write_ovs_tag_setting(bridge, "Bridge", "global", "set-controller", s->str, cmds); From 8228a81b403cfefbf7562fd83d4173078fb51c57 Mon Sep 17 00:00:00 2001 From: Danilo Egea Gondolfo Date: Tue, 6 Aug 2024 11:29:53 +0100 Subject: [PATCH 07/12] parse.c: fix implicit conversions --- src/parse.c | 35 ++++++++++++++++++++--------------- src/types-internal.h | 2 +- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/parse.c b/src/parse.c index e0a6948e4..79c5c7145 100644 --- a/src/parse.c +++ b/src/parse.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -1218,7 +1219,7 @@ handle_tunnel_key_flags(NetplanParser* npp, yaml_node_t* node, __unused const vo gboolean found = FALSE; assert_type(npp, entry, YAML_SCALAR_NODE); - for (int i = 1; i < NETPLAN_KEY_FLAG_MAX_; i <<= 1) { + for (guint i = 1; i < NETPLAN_KEY_FLAG_MAX_; i <<= 1) { if (!g_ascii_strcasecmp(scalar(entry), netplan_key_flags_name(i))) { npp->current.netdef->tunnel_private_key_flags |= i; found = TRUE; @@ -1348,7 +1349,7 @@ handle_wowlan(NetplanParser* npp, yaml_node_t* node, __unused const void* _, GEr for (unsigned i = 0; NETPLAN_WIFI_WOWLAN_TYPES[i].name != NULL; ++i) { if (g_ascii_strcasecmp(scalar(entry), NETPLAN_WIFI_WOWLAN_TYPES[i].name) == 0) { - npp->current.netdef->wowlan |= NETPLAN_WIFI_WOWLAN_TYPES[i].flag; + npp->current.netdef->wowlan |= (gint)NETPLAN_WIFI_WOWLAN_TYPES[i].flag; found = TRUE; break; } @@ -1356,7 +1357,7 @@ handle_wowlan(NetplanParser* npp, yaml_node_t* node, __unused const void* _, GEr if (!found) return yaml_error(npp, node, error, "invalid value for wakeonwlan: '%s'", scalar(entry)); } - if (npp->current.netdef->wowlan > NETPLAN_WIFI_WOWLAN_DEFAULT && npp->current.netdef->wowlan & NETPLAN_WIFI_WOWLAN_TYPES[0].flag) + if (npp->current.netdef->wowlan > NETPLAN_WIFI_WOWLAN_DEFAULT && npp->current.netdef->wowlan & (gint)NETPLAN_WIFI_WOWLAN_TYPES[0].flag) return yaml_error(npp, node, error, "'default' is an exclusive flag for wakeonwlan"); return TRUE; } @@ -2073,7 +2074,7 @@ handle_bridge_path_cost(NetplanParser* npp, yaml_node_t* node, const char* key_p { for (yaml_node_pair_t* entry = node->data.mapping.pairs.start; entry < node->data.mapping.pairs.top; entry++) { yaml_node_t* key, *value; - guint v; + guint64 v; gchar* endptr; NetplanNetDefinition *component; guint* ref_ptr; @@ -2103,9 +2104,10 @@ handle_bridge_path_cost(NetplanParser* npp, yaml_node_t* node, const char* key_p if (*endptr != '\0') return yaml_error(npp, node, error, "invalid unsigned int value '%s'", scalar(value)); - g_debug("%s: adding path '%s' of cost: %d", npp->current.netdef->id, scalar(key), v); + g_debug("%s: adding path '%s' of cost: %" PRIu64, npp->current.netdef->id, scalar(key), v); - *ref_ptr = v; + g_assert(v < G_MAXUINT); + *ref_ptr = (guint)v; mark_data_as_dirty(npp, ref_ptr); } } @@ -2117,7 +2119,7 @@ handle_bridge_port_priority(NetplanParser* npp, yaml_node_t* node, const char* k { for (yaml_node_pair_t* entry = node->data.mapping.pairs.start; entry < node->data.mapping.pairs.top; entry++) { yaml_node_t* key, *value; - guint v; + guint64 v; gchar* endptr; NetplanNetDefinition *component; guint* ref_ptr; @@ -2148,9 +2150,10 @@ handle_bridge_port_priority(NetplanParser* npp, yaml_node_t* node, const char* k return yaml_error(npp, node, error, "invalid port priority value (must be between 0 and 63): %s", scalar(value)); - g_debug("%s: adding port '%s' of priority: %d", npp->current.netdef->id, scalar(key), v); + g_debug("%s: adding port '%s' of priority: %" PRIu64, npp->current.netdef->id, scalar(key), v); - *ref_ptr = v; + g_assert(v < G_MAXUINT); + *ref_ptr = (guint)v; mark_data_as_dirty(npp, ref_ptr); } } @@ -2343,8 +2346,9 @@ handle_arp_ip_targets(NetplanParser* npp, yaml_node_t* node, __unused const void /* Avoid adding the same arp_ip_targets in a 2nd parsing pass by comparing * the array size to the YAML sequence size. Skip if they are equal. */ - guint item_count = node->data.sequence.items.top - node->data.sequence.items.start; - if (npp->current.netdef->bond_params.arp_ip_targets->len == item_count) { + ptrdiff_t item_count = node->data.sequence.items.top - node->data.sequence.items.start; + g_assert(item_count >= 0); + if (npp->current.netdef->bond_params.arp_ip_targets->len == (guint)item_count) { g_debug("%s: all arp ip targets have already been added", npp->current.netdef->id); return TRUE; } @@ -2689,8 +2693,9 @@ handle_wireguard_peers(NetplanParser* npp, yaml_node_t* node, __unused const voi /* Avoid adding the same peers in a 2nd parsing pass by comparing * the array size to the YAML sequence size. Skip if they are equal. */ - guint item_count = node->data.sequence.items.top - node->data.sequence.items.start; - if (npp->current.netdef->wireguard_peers->len == item_count) { + ptrdiff_t item_count = node->data.sequence.items.top - node->data.sequence.items.start; + g_assert(item_count >= 0); + if (npp->current.netdef->wireguard_peers->len == (guint)item_count) { g_debug("%s: all wireguard peers have already been added", npp->current.netdef->id); return TRUE; } @@ -3538,8 +3543,8 @@ STATIC gboolean process_document(NetplanParser* npp, GError** error) { gboolean ret; - int previously_found; - int still_missing; + guint previously_found; + guint still_missing; g_assert(npp->missing_id == NULL); npp->missing_id = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free); diff --git a/src/types-internal.h b/src/types-internal.h index f449f0f2a..f8c1df3df 100644 --- a/src/types-internal.h +++ b/src/types-internal.h @@ -258,7 +258,7 @@ struct netplan_parser { * Appears to be unused? * */ GHashTable* ids_in_file; - int missing_ids_found; + guint missing_ids_found; /* Which fields have been nullified by a subsequent patch? */ GHashTable* null_fields; From 0a6f4429db19baf1efe0eb42aabc767e02a37c81 Mon Sep 17 00:00:00 2001 From: Danilo Egea Gondolfo Date: Tue, 6 Aug 2024 11:37:36 +0100 Subject: [PATCH 08/12] parse-nm.c: fix implicit conversions --- src/parse-nm.c | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/parse-nm.c b/src/parse-nm.c index 215916fd1..a995ac9f8 100644 --- a/src/parse-nm.c +++ b/src/parse-nm.c @@ -138,7 +138,7 @@ keyfile_handle_generic_uint(GKeyFile* kf, const gchar* group, const gchar* key, { 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); + guint data = (guint)g_key_file_get_uint64(kf, group, key, NULL); if (data != default_value) *dataptr = data; _kf_clear_key(kf, group, key); @@ -262,7 +262,7 @@ parse_routes(GKeyFile* kf, const gchar* group, GArray** routes_arr) /* Append metric */ if (split[0] && split[1] && split[2] && strtoul(split[2], NULL, 10) != NETPLAN_METRIC_UNSPEC) - route->metric = strtoul(split[2], NULL, 10); + route->metric = (guint)strtoul(split[2], NULL, 10); g_strfreev(split); /* Parse route options */ @@ -275,17 +275,17 @@ parse_routes(GKeyFile* kf, const gchar* group, GArray** routes_arr) if (g_strcmp0(kv[0], "onlink") == 0) route->onlink = (g_strcmp0(kv[1], "true") == 0); else if (g_strcmp0(kv[0], "initrwnd") == 0) - route->advertised_receive_window = strtoul(kv[1], NULL, 10); + route->advertised_receive_window = (guint)strtoul(kv[1], NULL, 10); else if (g_strcmp0(kv[0], "initcwnd") == 0) - route->congestion_window = strtoul(kv[1], NULL, 10); + route->congestion_window = (guint)strtoul(kv[1], NULL, 10); else if (g_strcmp0(kv[0], "mtu") == 0) - route->mtubytes = strtoul(kv[1], NULL, 10); + route->mtubytes = (guint)strtoul(kv[1], NULL, 10); else if (g_strcmp0(kv[0], "table") == 0) - route->table = strtoul(kv[1], NULL, 10); + route->table = (guint)strtoul(kv[1], NULL, 10); else if (g_strcmp0(kv[0], "src") == 0) route->from = g_strdup(kv[1]); //no need to free, will stay in netdef else if (g_strcmp0(kv[0], "advmss") == 0) - route->advmss = strtoul(kv[1], NULL, 10); + route->advmss = (guint)strtoul(kv[1], NULL, 10); else unhandled_data = TRUE; g_strfreev(kv); @@ -505,10 +505,10 @@ parse_tunnels(GKeyFile* kf, NetplanNetDefinition* nd) _kf_clear_key(kf, "wireguard", "private-key"); /* Reading the listen port */ - nd->tunnel.port = g_key_file_get_uint64(kf, "wireguard", "listen-port", NULL); + nd->tunnel.port = (guint)g_key_file_get_uint64(kf, "wireguard", "listen-port", NULL); _kf_clear_key(kf, "wireguard", "listen-port"); - nd->tunnel_private_key_flags = g_key_file_get_integer(kf, "wireguard", "private-key-flags", NULL); + nd->tunnel_private_key_flags = (guint)g_key_file_get_uint64(kf, "wireguard", "private-key-flags", NULL); _kf_clear_key(kf, "wireguard", "private-key-flags"); gchar** keyfile_groups = g_key_file_get_groups(kf, NULL); @@ -590,7 +590,7 @@ parse_tunnels(GKeyFile* kf, NetplanNetDefinition* nd) reset_vxlan(nd->vxlan); /* Reading the VXLAN ID*/ - nd->vxlan->vni = g_key_file_get_integer(kf, "vxlan", "id", NULL); + nd->vxlan->vni = (guint)g_key_file_get_uint64(kf, "vxlan", "id", NULL); _kf_clear_key(kf, "vxlan", "id"); nd->tunnel.local_ip = g_key_file_get_string(kf, "vxlan", "local", NULL); @@ -600,7 +600,7 @@ parse_tunnels(GKeyFile* kf, NetplanNetDefinition* nd) } else { /* Handle all the other types of tunnel */ - nd->tunnel.mode = g_key_file_get_integer(kf, "ip-tunnel", "mode", NULL); + nd->tunnel.mode = (guint)g_key_file_get_uint64(kf, "ip-tunnel", "mode", NULL); /* We don't want to automatically accept new types of tunnels introduced by Network Manager */ if (nd->tunnel.mode >= NETPLAN_TUNNEL_MODE_NM_MAX) { @@ -722,7 +722,7 @@ netplan_parser_load_keyfile(NetplanParser* npp, const char* filename, GError** e /* Handle VRFs */ if (nd_type == NETPLAN_DEF_TYPE_VRF) { if (g_key_file_has_key(kf, "vrf", "table", NULL)) { - nd->vrf_table = g_key_file_get_uint64(kf, "vrf", "table", NULL); + nd->vrf_table = (guint)g_key_file_get_uint64(kf, "vrf", "table", NULL); _kf_clear_key(kf, "vrf", "table"); } } @@ -841,7 +841,7 @@ netplan_parser_load_keyfile(NetplanParser* npp, const char* filename, GError** e if (nd_type == NETPLAN_DEF_TYPE_ETHERNET) nd->wake_on_lan = TRUE; //NM's default is "1" } else { - guint value = g_key_file_get_uint64(kf, "ethernet", "wake-on-lan", NULL); + guint64 value = g_key_file_get_uint64(kf, "ethernet", "wake-on-lan", NULL); //XXX: fix delta between options in NM (0x1, 0x2, 0x4, ...) and netplan (bool) nd->wake_on_lan = value > 0; // netplan only knows about "off" or "on" if (value == 0) @@ -855,7 +855,9 @@ netplan_parser_load_keyfile(NetplanParser* npp, const char* filename, GError** e /* Wifis */ if (g_key_file_has_group(kf, "wifi")) { if (g_key_file_get_uint64(kf, "wifi", "wake-on-wlan", NULL)) { - nd->wowlan = g_key_file_get_uint64(kf, "wifi", "wake-on-wlan", NULL); + guint64 wow = g_key_file_get_uint64(kf, "wifi", "wake-on-wlan", NULL); + g_assert(wow < G_MAXINT); + nd->wowlan = (gint)wow; _kf_clear_key(kf, "wifi", "wake-on-wlan"); } else { nd->wowlan = NETPLAN_WIFI_WOWLAN_DEFAULT; From 39e796e84d557f2624f61afb6ed5f47bb0117884 Mon Sep 17 00:00:00 2001 From: Danilo Egea Gondolfo Date: Tue, 6 Aug 2024 11:42:19 +0100 Subject: [PATCH 09/12] sriov.c: fix implicit conversions Here we probably should make _netplan_state_get_vf_count_for_def return a unsigned int, but as it is a public interface (even though only used inside netplan) I'll add some safety checks and cast the return value to int. --- src/sriov.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/sriov.c b/src/sriov.c index 733f78c9b..b08ca7a26 100644 --- a/src/sriov.c +++ b/src/sriov.c @@ -222,5 +222,12 @@ _netplan_state_get_vf_count_for_def(const NetplanState* np_state, const NetplanN g_set_error(error, NETPLAN_BACKEND_ERROR, NETPLAN_ERROR_VALIDATION, "more VFs allocated than the explicit size declared: %d > %d", count, netdef->sriov_explicit_vf_count); return -1; } - return netdef->sriov_explicit_vf_count != G_MAXUINT ? netdef->sriov_explicit_vf_count : count; + + if (netdef->sriov_explicit_vf_count != G_MAXUINT) { + g_assert(netdef->sriov_explicit_vf_count <= G_MAXINT); + count = netdef->sriov_explicit_vf_count; + } + + g_assert(count <= G_MAXINT); + return (int)count; } From 819ee2e5b5df7c0dd1f73069e15c5f8e1b934aa4 Mon Sep 17 00:00:00 2001 From: Danilo Egea Gondolfo Date: Tue, 6 Aug 2024 12:25:13 +0100 Subject: [PATCH 10/12] util.c: fix implicit conversions --- src/util-internal.h | 2 +- src/util.c | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/util-internal.h b/src/util-internal.h index d073dea8d..bed36d493 100644 --- a/src/util-internal.h +++ b/src/util-internal.h @@ -41,7 +41,7 @@ NETPLAN_INTERNAL void _netplan_g_string_free_to_file(GString* s, const char* rootdir, const char* path, const char* suffix); void -_netplan_g_string_free_to_file_with_permissions(GString* s, const char* rootdir, const char* path, const char* suffix, const char* owner, const char* group, mode_t mode); +_netplan_g_string_free_to_file_with_permissions(GString* s, const char* rootdir, const char* path, const char* suffix, const char* owner, const char* group, int mode); NETPLAN_INTERNAL void _netplan_unlink_glob(const char* rootdir, const char* _glob); diff --git a/src/util.c b/src/util.c index fdab5ab0b..0fc06f222 100644 --- a/src/util.c +++ b/src/util.c @@ -93,7 +93,7 @@ void _netplan_g_string_free_to_file(GString* s, const char* rootdir, const char* } } -void _netplan_g_string_free_to_file_with_permissions(GString* s, const char* rootdir, const char* path, const char* suffix, const char* owner, const char* group, mode_t mode) +void _netplan_g_string_free_to_file_with_permissions(GString* s, const char* rootdir, const char* path, const char* suffix, const char* owner, const char* group, int mode) { g_autofree char* full_path = NULL; g_autofree char* path_suffix = NULL; @@ -651,7 +651,8 @@ netplan_get_id_from_nm_filepath(const char* filename, const char* ssid, char* ou /* Move pointer to start of netplan ID inside filename string */ start = pos + strlen(nm_prefix); - id_len = end - start; + g_assert(end - start > 0); + id_len = (gsize)(end - start); if (out_buf_size < id_len + 1) return NETPLAN_BUFFER_TOO_SMALL; @@ -659,7 +660,8 @@ netplan_get_id_from_nm_filepath(const char* filename, const char* ssid, char* ou strncpy(out_buffer, start, id_len); out_buffer[id_len] = '\0'; - return id_len + 1; + g_assert(id_len + 1 <= G_MAXLONG); + return (ssize_t)id_len + 1; } ssize_t @@ -1001,7 +1003,8 @@ netplan_copy_string(const char* input, char* out_buffer, size_t out_size) char* end = stpncpy(out_buffer, input, out_size); // If it point to the first byte past the buffer, we don't have enough // space in the buffer. - size_t len = end - out_buffer; + g_assert(end - out_buffer >= 0); + size_t len = (size_t)(end - out_buffer); if (len == out_size) return NETPLAN_BUFFER_TOO_SMALL; return end - out_buffer + 1; @@ -1181,8 +1184,8 @@ is_route_present(const NetplanNetDefinition* netdef, const NetplanIPRoute* route entry->table == route->table && entry->metric == route->metric && g_strcmp0(entry->from, route->from) == 0 && - g_strcmp0(normalize_ip_address(entry->to, entry->family), - normalize_ip_address(route->to, route->family)) == 0 && + g_strcmp0(normalize_ip_address(entry->to, (guint)entry->family), + normalize_ip_address(route->to, (guint)route->family)) == 0 && g_strcmp0(entry->via, route->via) == 0 ) return TRUE; From 40671dbd7b0d6505417d3dace86f4640eb13b3f1 Mon Sep 17 00:00:00 2001 From: Danilo Egea Gondolfo Date: Tue, 6 Aug 2024 12:26:46 +0100 Subject: [PATCH 11/12] validation.c: fix implicit conversions --- src/validation.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/validation.c b/src/validation.c index 793d97a99..985e6450b 100644 --- a/src/validation.c +++ b/src/validation.c @@ -337,7 +337,7 @@ validate_tunnel_backend_rules(const NetplanParser* npp, NetplanNetDefinition* nd gboolean validate_netdef_grammar(const NetplanParser* npp, NetplanNetDefinition* nd, GError** error) { - int missing_id_count = g_hash_table_size(npp->missing_id); + guint missing_id_count = g_hash_table_size(npp->missing_id); gboolean valid = FALSE; NetplanBackend backend = nd->backend; From b56f5f88472a67a95c68627b141b6e339f37dcb8 Mon Sep 17 00:00:00 2001 From: Danilo Egea Gondolfo Date: Tue, 6 Aug 2024 14:41:27 +0100 Subject: [PATCH 12/12] ctests: fix implicit conversions --- tests/ctests/test_netplan_error.c | 2 +- tests/ctests/test_netplan_keyfile.c | 6 +++--- tests/ctests/test_netplan_parser.c | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/ctests/test_netplan_error.c b/tests/ctests/test_netplan_error.c index 3794f68ab..01bc1c6f3 100644 --- a/tests/ctests/test_netplan_error.c +++ b/tests/ctests/test_netplan_error.c @@ -26,7 +26,7 @@ test_netplan_error_code(__unused void** state) { GError *gerror = g_error_new(1234, 5678, "%s: error message", "it failed"); uint64_t error_code = netplan_error_code(gerror); - GQuark domain = error_code >> 32; + GQuark domain = (GQuark)(error_code >> 32); gint error = (gint) error_code; assert_int_equal(domain, 1234); diff --git a/tests/ctests/test_netplan_keyfile.c b/tests/ctests/test_netplan_keyfile.c index 7b1cbecc3..2cda89efb 100644 --- a/tests/ctests/test_netplan_keyfile.c +++ b/tests/ctests/test_netplan_keyfile.c @@ -260,8 +260,8 @@ test_load_keyfile_utf8_password(__unused void** state) { NetplanState *np_state = NULL; int fd; - int size; - int res; + size_t size; + ssize_t res; char* yaml; const char* keyfile = @@ -306,7 +306,7 @@ test_load_keyfile_utf8_password(__unused void** state) netplan_state_dump_yaml(np_state, fd, NULL); - size = lseek(fd, 0, SEEK_CUR) + 1; + size = (size_t)lseek(fd, 0, SEEK_CUR) + 1; yaml = malloc(size); memset(yaml, 0, size); lseek(fd, 0, SEEK_SET); diff --git a/tests/ctests/test_netplan_parser.c b/tests/ctests/test_netplan_parser.c index b04e228e7..d8020e420 100644 --- a/tests/ctests/test_netplan_parser.c +++ b/tests/ctests/test_netplan_parser.c @@ -389,8 +389,8 @@ test_parse_utf8_characters(__unused void** state) { NetplanState *np_state = NULL; int fd; - int size; - int res; + size_t size; + ssize_t res; char* yaml = "network:\n" @@ -418,7 +418,7 @@ test_parse_utf8_characters(__unused void** state) netplan_state_dump_yaml(np_state, fd, NULL); - size = lseek(fd, 0, SEEK_CUR) + 1; + size = (size_t)lseek(fd, 0, SEEK_CUR) + 1; yaml = malloc(size); memset(yaml, 0, size); lseek(fd, 0, SEEK_SET);