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.
This commit is contained in:
Lubomir Rintel
2022-03-28 13:51:44 +02:00
parent 18b4ea7468
commit 9702310f25
5 changed files with 40 additions and 33 deletions

View File

@@ -1444,13 +1444,13 @@ nmc_connection_profile_details(NMConnection *connection, NmCli *nmc)
TRUE, TRUE,
&prop_array, &prop_array,
&error); &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_string_printf(nmc->return_text, _("Error: 'connection show': %s"), error->message);
g_error_free(error); g_error_free(error);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT; nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
return FALSE; return FALSE;
} }
g_assert(print_settings_array);
/* Main header */ /* Main header */
{ {
@@ -1551,13 +1551,13 @@ nmc_active_connection_details(NMActiveConnection *acon, NmCli *nmc)
TRUE, TRUE,
&group_fields, &group_fields,
&error); &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_string_printf(nmc->return_text, _("Error: 'connection show': %s"), error->message);
g_error_free(error); g_error_free(error);
nmc->return_value = NMC_RESULT_ERROR_USER_INPUT; nmc->return_value = NMC_RESULT_ERROR_USER_INPUT;
return FALSE; return FALSE;
} }
g_assert(print_groups);
/* Main header */ /* Main header */
{ {
@@ -2182,7 +2182,7 @@ do_connections_show(const NMCCommand *cmd, NmCli *nmc, int argc, const char *con
goto finish; goto finish;
break; break;
default: default:
g_assert_not_reached(); g_return_if_reached();
break; break;
} }
} }
@@ -2502,7 +2502,7 @@ find_device_for_connection(NmCli *nmc,
g_return_val_if_fail(error == NULL || *error == NULL, FALSE); g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
s_con = nm_connection_get_setting_connection(connection); 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); con_type = nm_setting_connection_get_connection_type(s_con);
if (nm_streq(con_type, NM_SETTING_VPN_SETTING_NAME)) { 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; gs_free char *line = NULL;
/* nmc_do_cmd() should not call this with argc=0. */ /* 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); line = nmc_readline(&nmc->nmc_config, PROMPT_CONNECTION);
nmc_string_to_arg_array(line, NULL, TRUE, &arg_arr, &arg_num); 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) { if (argc == 0) {
/* nmc_do_cmd() should not call this with 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) { if (nmc->ask) {
gs_free char *line = NULL; gs_free char *line = NULL;
@@ -3692,7 +3692,8 @@ is_setting_mandatory(NMConnection *connection, NMSetting *setting)
guint i; guint i;
s_con = nm_connection_get_setting_connection(connection); 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); c_type = nm_setting_connection_get_connection_type(s_con);
s_type = nm_setting_connection_get_slave_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++) { for (i = 0; i < connections->len; i++) {
connection = NM_CONNECTION(connections->pdata[i]); connection = NM_CONNECTION(connections->pdata[i]);
s_con = nm_connection_get_setting_connection(connection); 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); con_type = nm_setting_connection_get_connection_type(s_con);
if (type && !nm_streq0(con_type, type)) if (type && !nm_streq0(con_type, type))
continue; continue;
@@ -4275,7 +4276,7 @@ con_settings(NMConnection *connection,
g_return_val_if_fail(slv_settings, FALSE); g_return_val_if_fail(slv_settings, FALSE);
s_con = nm_connection_get_setting_connection(connection); 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); con_type = nm_setting_connection_get_slave_type(s_con);
*slv_settings = nm_meta_setting_info_valid_parts_for_slave_type(con_type, NULL); *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; const char *con_type, *con_id, *con_uuid;
s_con = nm_connection_get_setting_connection(connection); 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_type = nm_setting_connection_get_connection_type(s_con);
con_id = nm_connection_get_id(connection); con_id = nm_connection_get_id(connection);
con_uuid = nm_connection_get_uuid(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; gboolean confirmed = TRUE;
s_con_loc = nm_connection_get_setting_connection(local); 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); ac_local = nm_setting_connection_get_autoconnect(s_con_loc);
if (remote) { if (remote) {
s_con_rem = nm_connection_get_setting_connection(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); ac_remote = nm_setting_connection_get_autoconnect(s_con_rem);
} else } else {
ac_remote = FALSE; ac_remote = FALSE;
}
if (ac_local && !ac_remote) { if (ac_local && !ac_remote) {
gs_free char *answer = NULL; gs_free char *answer = NULL;
@@ -8578,7 +8583,8 @@ editor_init_new_connection(NmCli *nmc, NMConnection *connection, const char *sla
const char *con_type; const char *con_type;
s_con = nm_connection_get_setting_connection(connection); 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_type = nm_setting_connection_get_connection_type(s_con);
/* Initialize new connection according to its type using sensible defaults. */ /* 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; gs_free char *line = NULL;
/* nmc_do_cmd() should not call this with argc=0. */ /* 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); line = nmc_readline(&nmc->nmc_config, PROMPT_CONNECTION);
nmc_string_to_arg_array(line, NULL, TRUE, &arg_arr, &arg_num); 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; gs_free char *line = NULL;
/* nmc_do_cmd() should not call this with argc=0. */ /* 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); line = nmc_readline(&nmc->nmc_config, PROMPT_CONNECTIONS);
nmc_string_to_arg_array(line, NULL, TRUE, &arg_arr, &arg_num); 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. */ /* No connections specified. Monitor all. */
/* nmc_do_cmd() should not call this with argc=0. */ /* 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); connections = nm_client_get_connections(nmc->client);
} else { } else {
@@ -9402,7 +9408,7 @@ do_connection_import(const NMCCommand *cmd, NmCli *nmc, int argc, const char *co
if (argc == 0) { if (argc == 0) {
/* nmc_do_cmd() should not call this with 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) { if (nmc->ask) {
type_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; gs_free char *line = NULL;
/* nmc_do_cmd() should not call this with argc=0. */ /* 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); line = nmc_readline(&nmc->nmc_config, PROMPT_VPN_CONNECTION);
nmc_string_to_arg_array(line, NULL, TRUE, &arg_arr, &arg_num); nmc_string_to_arg_array(line, NULL, TRUE, &arg_arr, &arg_num);

View File

@@ -156,7 +156,7 @@ next_arg(NmCli *nmc, int *argc, const char *const **argv, ...)
va_list args; va_list args;
const char *cmd_option; const char *cmd_option;
g_assert(*argc >= 0); g_return_val_if_fail(*argc >= 0, -1);
do { do {
int cmd_option_pos = 1; int cmd_option_pos = 1;
@@ -1229,7 +1229,7 @@ _print_do(const NmcConfig *nmc_config,
guint i_row, i_col; guint i_row, i_col;
nm_auto_free_gstring GString *str = NULL; nm_auto_free_gstring GString *str = NULL;
g_assert(col_len); g_return_if_fail(col_len);
/* Main header */ /* Main header */
if (nmc_config->print_output == NMC_PRINT_PRETTY && header_name_no_l10n) { 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; gboolean is_array = field_values[idx].value_is_array;
/* section prefix can't be an 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 */ if (section_prefix && idx == 0) /* The first field is section prefix */
continue; continue;

View File

@@ -121,19 +121,20 @@ nmt_address_list_create_widget(NmtWidgetList *list, int num)
NmtAddressListPrivate *priv = NMT_ADDRESS_LIST_GET_PRIVATE(list); NmtAddressListPrivate *priv = NMT_ADDRESS_LIST_GET_PRIVATE(list);
NmtNewtWidget *entry; 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); 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); 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); 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); 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); entry = nmt_newt_entry_new(25, NMT_NEWT_ENTRY_NONEMPTY);
nmt_newt_entry_set_filter(NMT_NEWT_ENTRY(entry), hostname_filter, list); nmt_newt_entry_set_filter(NMT_NEWT_ENTRY(entry), hostname_filter, list);
} else } else {
g_assert_not_reached(); g_return_val_if_reached(NULL);
}
g_object_bind_property_full(list, g_object_bind_property_full(list,
"strings", "strings",

View File

@@ -232,7 +232,7 @@ nmt_editor_grid_get_components(NmtNewtWidget *widget)
if (rows[i].label) { if (rows[i].label) {
child_cos = nmt_newt_widget_get_components(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_ptr_array_add(cos, child_cos[0]);
g_free(child_cos); g_free(child_cos);
} }

View File

@@ -377,7 +377,7 @@ nmt_editor_constructed(GObject *object)
else if (nm_connection_is_type(priv->edit_connection, NM_SETTING_WIREGUARD_SETTING_NAME)) else if (nm_connection_is_type(priv->edit_connection, NM_SETTING_WIREGUARD_SETTING_NAME))
page = nmt_page_wireguard_new(priv->edit_connection, deventry); page = nmt_page_wireguard_new(priv->edit_connection, deventry);
else else
g_assert_not_reached(); g_return_if_reached();
add_sections_for_page(editor, grid, page); add_sections_for_page(editor, grid, page);
nmt_editor_grid_append(grid, NULL, nmt_newt_separator_new(), NULL); nmt_editor_grid_append(grid, NULL, nmt_newt_separator_new(), NULL);