From 9702310f25af4171d73a1e500714b2f8f8c575c6 Mon Sep 17 00:00:00 2001 From: Lubomir Rintel Date: Mon, 28 Mar 2022 13:51:44 +0200 Subject: clients: bulk removal of g_assert*() statements Assertions should be done in tests. If we detect an unexpected situation at runtime, we shall fail more gracefully than crashing right away. --- src/nmcli/connections.c | 48 +++++++++++++++++++++++++------------------- src/nmcli/utils.c | 6 +++--- src/nmtui/nmt-address-list.c | 15 +++++++------- src/nmtui/nmt-editor-grid.c | 2 +- src/nmtui/nmt-editor.c | 2 +- 5 files changed, 40 insertions(+), 33 deletions(-) diff --git a/src/nmcli/connections.c b/src/nmcli/connections.c index d61f8f7ce9..06665755be 100644 --- a/src/nmcli/connections.c +++ b/src/nmcli/connections.c @@ -1444,13 +1444,13 @@ nmc_connection_profile_details(NMConnection *connection, NmCli *nmc) TRUE, &prop_array, &error); - if (error) { + if (!print_settings_array) { + g_return_val_if_fail(error, FALSE); g_string_printf(nmc->return_text, _("Error: 'connection show': %s"), error->message); g_error_free(error); nmc->return_value = NMC_RESULT_ERROR_USER_INPUT; return FALSE; } - g_assert(print_settings_array); /* Main header */ { @@ -1551,13 +1551,13 @@ nmc_active_connection_details(NMActiveConnection *acon, NmCli *nmc) TRUE, &group_fields, &error); - if (error) { + if (!print_groups) { + g_return_val_if_fail(error, FALSE); g_string_printf(nmc->return_text, _("Error: 'connection show': %s"), error->message); g_error_free(error); nmc->return_value = NMC_RESULT_ERROR_USER_INPUT; return FALSE; } - g_assert(print_groups); /* Main header */ { @@ -2182,7 +2182,7 @@ do_connections_show(const NMCCommand *cmd, NmCli *nmc, int argc, const char *con goto finish; break; default: - g_assert_not_reached(); + g_return_if_reached(); break; } } @@ -2502,7 +2502,7 @@ find_device_for_connection(NmCli *nmc, g_return_val_if_fail(error == NULL || *error == NULL, FALSE); s_con = nm_connection_get_setting_connection(connection); - g_assert(s_con); + g_return_val_if_fail(s_con, FALSE); con_type = nm_setting_connection_get_connection_type(s_con); if (nm_streq(con_type, NM_SETTING_VPN_SETTING_NAME)) { @@ -2991,7 +2991,7 @@ do_connection_up(const NMCCommand *cmd, NmCli *nmc, int argc, const char *const gs_free char *line = NULL; /* nmc_do_cmd() should not call this with argc=0. */ - g_assert(!nmc->complete); + g_return_if_fail(!nmc->complete); line = nmc_readline(&nmc->nmc_config, PROMPT_CONNECTION); nmc_string_to_arg_array(line, NULL, TRUE, &arg_arr, &arg_num); @@ -3247,7 +3247,7 @@ do_connection_down(const NMCCommand *cmd, NmCli *nmc, int argc, const char *cons if (argc == 0) { /* nmc_do_cmd() should not call this with argc=0. */ - g_assert(!nmc->complete); + g_return_if_fail(!nmc->complete); if (nmc->ask) { gs_free char *line = NULL; @@ -3692,7 +3692,8 @@ is_setting_mandatory(NMConnection *connection, NMSetting *setting) guint i; s_con = nm_connection_get_setting_connection(connection); - g_assert(s_con); + g_return_val_if_fail(s_con, FALSE); + c_type = nm_setting_connection_get_connection_type(s_con); s_type = nm_setting_connection_get_slave_type(s_con); @@ -3771,7 +3772,7 @@ normalized_master_for_slave(const GPtrArray *connections, for (i = 0; i < connections->len; i++) { connection = NM_CONNECTION(connections->pdata[i]); s_con = nm_connection_get_setting_connection(connection); - g_assert(s_con); + g_return_val_if_fail(s_con, NULL); con_type = nm_setting_connection_get_connection_type(s_con); if (type && !nm_streq0(con_type, type)) continue; @@ -4275,7 +4276,7 @@ con_settings(NMConnection *connection, g_return_val_if_fail(slv_settings, FALSE); s_con = nm_connection_get_setting_connection(connection); - g_assert(s_con); + g_return_val_if_fail(s_con, FALSE); con_type = nm_setting_connection_get_slave_type(s_con); *slv_settings = nm_meta_setting_info_valid_parts_for_slave_type(con_type, NULL); @@ -7300,7 +7301,8 @@ editor_show_status_line(NMConnection *connection, gboolean dirty, gboolean temp) const char *con_type, *con_id, *con_uuid; s_con = nm_connection_get_setting_connection(connection); - g_assert(s_con); + g_return_if_fail(s_con); + con_type = nm_setting_connection_get_connection_type(s_con); con_id = nm_connection_get_id(connection); con_uuid = nm_connection_get_uuid(connection); @@ -7643,15 +7645,18 @@ confirm_connection_saving(const NmcConfig *nmc_config, NMConnection *local, NMCo gboolean confirmed = TRUE; s_con_loc = nm_connection_get_setting_connection(local); - g_assert(s_con_loc); + g_return_val_if_fail(s_con_loc, FALSE); + ac_local = nm_setting_connection_get_autoconnect(s_con_loc); if (remote) { s_con_rem = nm_connection_get_setting_connection(remote); - g_assert(s_con_rem); + g_return_val_if_fail(s_con_rem, FALSE); + ac_remote = nm_setting_connection_get_autoconnect(s_con_rem); - } else + } else { ac_remote = FALSE; + } if (ac_local && !ac_remote) { gs_free char *answer = NULL; @@ -8578,7 +8583,8 @@ editor_init_new_connection(NmCli *nmc, NMConnection *connection, const char *sla const char *con_type; s_con = nm_connection_get_setting_connection(connection); - g_assert(s_con); + g_return_if_fail(s_con); + con_type = nm_setting_connection_get_connection_type(s_con); /* Initialize new connection according to its type using sensible defaults. */ @@ -9008,7 +9014,7 @@ do_connection_clone(const NMCCommand *cmd, NmCli *nmc, int argc, const char *con gs_free char *line = NULL; /* nmc_do_cmd() should not call this with argc=0. */ - g_assert(!nmc->complete); + g_return_if_fail(!nmc->complete); line = nmc_readline(&nmc->nmc_config, PROMPT_CONNECTION); nmc_string_to_arg_array(line, NULL, TRUE, &arg_arr, &arg_num); @@ -9107,7 +9113,7 @@ do_connection_delete(const NMCCommand *cmd, NmCli *nmc, int argc, const char *co gs_free char *line = NULL; /* nmc_do_cmd() should not call this with argc=0. */ - g_assert(!nmc->complete); + g_return_if_fail(!nmc->complete); line = nmc_readline(&nmc->nmc_config, PROMPT_CONNECTIONS); nmc_string_to_arg_array(line, NULL, TRUE, &arg_arr, &arg_num); @@ -9245,7 +9251,7 @@ do_connection_monitor(const NMCCommand *cmd, NmCli *nmc, int argc, const char *c /* No connections specified. Monitor all. */ /* nmc_do_cmd() should not call this with argc=0. */ - g_assert(!nmc->complete); + g_return_if_fail(!nmc->complete); connections = nm_client_get_connections(nmc->client); } else { @@ -9402,7 +9408,7 @@ do_connection_import(const NMCCommand *cmd, NmCli *nmc, int argc, const char *co if (argc == 0) { /* nmc_do_cmd() should not call this with argc=0. */ - g_assert(!nmc->complete); + g_return_if_fail(!nmc->complete); if (nmc->ask) { type_ask = @@ -9545,7 +9551,7 @@ do_connection_export(const NMCCommand *cmd, NmCli *nmc, int argc, const char *co gs_free char *line = NULL; /* nmc_do_cmd() should not call this with argc=0. */ - g_assert(!nmc->complete); + g_return_if_fail(!nmc->complete); line = nmc_readline(&nmc->nmc_config, PROMPT_VPN_CONNECTION); nmc_string_to_arg_array(line, NULL, TRUE, &arg_arr, &arg_num); diff --git a/src/nmcli/utils.c b/src/nmcli/utils.c index 7645a08cd6..209a5b6938 100644 --- a/src/nmcli/utils.c +++ b/src/nmcli/utils.c @@ -156,7 +156,7 @@ next_arg(NmCli *nmc, int *argc, const char *const **argv, ...) va_list args; const char *cmd_option; - g_assert(*argc >= 0); + g_return_val_if_fail(*argc >= 0, -1); do { int cmd_option_pos = 1; @@ -1229,7 +1229,7 @@ _print_do(const NmcConfig *nmc_config, guint i_row, i_col; nm_auto_free_gstring GString *str = NULL; - g_assert(col_len); + g_return_if_fail(col_len); /* Main header */ if (nmc_config->print_output == NMC_PRINT_PRETTY && header_name_no_l10n) { @@ -1628,7 +1628,7 @@ print_required_fields(const NmcConfig *nmc_config, gboolean is_array = field_values[idx].value_is_array; /* section prefix can't be an array */ - g_assert(!is_array || !section_prefix || idx != 0); + g_return_if_fail(!is_array || !section_prefix || idx != 0); if (section_prefix && idx == 0) /* The first field is section prefix */ continue; diff --git a/src/nmtui/nmt-address-list.c b/src/nmtui/nmt-address-list.c index 1678f33f50..dba8e790c5 100644 --- a/src/nmtui/nmt-address-list.c +++ b/src/nmtui/nmt-address-list.c @@ -121,19 +121,20 @@ nmt_address_list_create_widget(NmtWidgetList *list, int num) NmtAddressListPrivate *priv = NMT_ADDRESS_LIST_GET_PRIVATE(list); NmtNewtWidget *entry; - if (priv->list_type == NMT_ADDRESS_LIST_IP4_WITH_PREFIX) + if (priv->list_type == NMT_ADDRESS_LIST_IP4_WITH_PREFIX) { entry = nmt_ip_entry_new(25, AF_INET, TRUE, FALSE); - else if (priv->list_type == NMT_ADDRESS_LIST_IP4) + } else if (priv->list_type == NMT_ADDRESS_LIST_IP4) { entry = nmt_ip_entry_new(25, AF_INET, FALSE, FALSE); - else if (priv->list_type == NMT_ADDRESS_LIST_IP6_WITH_PREFIX) + } else if (priv->list_type == NMT_ADDRESS_LIST_IP6_WITH_PREFIX) { entry = nmt_ip_entry_new(25, AF_INET6, TRUE, FALSE); - else if (priv->list_type == NMT_ADDRESS_LIST_IP6) + } else if (priv->list_type == NMT_ADDRESS_LIST_IP6) { entry = nmt_ip_entry_new(25, AF_INET6, FALSE, FALSE); - else if (priv->list_type == NMT_ADDRESS_LIST_HOSTNAME) { + } else if (priv->list_type == NMT_ADDRESS_LIST_HOSTNAME) { entry = nmt_newt_entry_new(25, NMT_NEWT_ENTRY_NONEMPTY); nmt_newt_entry_set_filter(NMT_NEWT_ENTRY(entry), hostname_filter, list); - } else - g_assert_not_reached(); + } else { + g_return_val_if_reached(NULL); + } g_object_bind_property_full(list, "strings", diff --git a/src/nmtui/nmt-editor-grid.c b/src/nmtui/nmt-editor-grid.c index bb25780559..b4beda9248 100644 --- a/src/nmtui/nmt-editor-grid.c +++ b/src/nmtui/nmt-editor-grid.c @@ -232,7 +232,7 @@ nmt_editor_grid_get_components(NmtNewtWidget *widget) if (rows[i].label) { child_cos = nmt_newt_widget_get_components(rows[i].label); - g_assert(child_cos[0] && !child_cos[1]); + g_return_val_if_fail(child_cos[0] && !child_cos[1], NULL); g_ptr_array_add(cos, child_cos[0]); g_free(child_cos); } diff --git a/src/nmtui/nmt-editor.c b/src/nmtui/nmt-editor.c index 8e6a760c1a..427ac0178b 100644 --- a/src/nmtui/nmt-editor.c +++ b/src/nmtui/nmt-editor.c @@ -377,7 +377,7 @@ nmt_editor_constructed(GObject *object) else if (nm_connection_is_type(priv->edit_connection, NM_SETTING_WIREGUARD_SETTING_NAME)) page = nmt_page_wireguard_new(priv->edit_connection, deventry); else - g_assert_not_reached(); + g_return_if_reached(); add_sections_for_page(editor, grid, page); nmt_editor_grid_append(grid, NULL, nmt_newt_separator_new(), NULL); -- cgit v1.2.1