Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle implicity conversions #496

Merged
merged 12 commits into from
Sep 4, 2024
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
10 changes: 4 additions & 6 deletions src/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/netplan.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
14 changes: 7 additions & 7 deletions src/nm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
slyon marked this conversation as resolved.
Show resolved Hide resolved
if (peer->endpoint)
g_key_file_set_string(kf, tmp_group, "endpoint", peer->endpoint);

Expand Down Expand Up @@ -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));
}
Expand Down
3 changes: 2 additions & 1 deletion src/openvswitch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
30 changes: 16 additions & 14 deletions src/parse-nm.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 */
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand Down Expand Up @@ -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");
}
}
Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand Down
35 changes: 20 additions & 15 deletions src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <errno.h>
#include <regex.h>
#include <arpa/inet.h>
#include <inttypes.h>

#include <glib.h>
#include <glib/gstdio.h>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1348,15 +1349,15 @@ 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;
}
}
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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
slyon marked this conversation as resolved.
Show resolved Hide resolved

g_assert(npp->missing_id == NULL);
npp->missing_id = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, g_free);
Expand Down
9 changes: 8 additions & 1 deletion src/sriov.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion src/types-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/util-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Loading
Loading