From 9065850b92b0c23f57509b8c3c1186792b4ed425 Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Thu, 21 Nov 2019 21:27:30 +1300 Subject: [PATCH 01/10] printers: Replace g_variant_iter_next_value with the simpler g_variant_iter_next --- panels/printers/pp-new-printer-dialog.c | 12 ++---- panels/printers/pp-new-printer.c | 21 +++------- panels/printers/pp-utils.c | 56 ++++--------------------- 3 files changed, 19 insertions(+), 70 deletions(-) diff --git a/panels/printers/pp-new-printer-dialog.c b/panels/printers/pp-new-printer-dialog.c index 3ddc85d109..657f45e8a4 100644 --- a/panels/printers/pp-new-printer-dialog.c +++ b/panels/printers/pp-new-printer-dialog.c @@ -916,24 +916,20 @@ group_physical_devices_dbus_cb (GObject *source_object, GVariantIter *iter; GVariantIter *subiter; GVariant *item; - GVariant *subitem; - gchar *device_uri; result = g_new0 (gchar **, g_variant_n_children (array) + 1); g_variant_get (array, "aas", &iter); i = 0; while ((item = g_variant_iter_next_value (iter))) { + const gchar *device_uri; + result[i] = g_new0 (gchar *, g_variant_n_children (item) + 1); g_variant_get (item, "as", &subiter); j = 0; - while ((subitem = g_variant_iter_next_value (subiter))) + while (g_variant_iter_next (subiter, "&s", &device_uri)) { - g_variant_get (subitem, "s", &device_uri); - - result[i][j] = device_uri; - - g_variant_unref (subitem); + result[i][j] = g_strdup (device_uri); j++; } diff --git a/panels/printers/pp-new-printer.c b/panels/printers/pp-new-printer.c index bcd4c2d5c6..b9d734afd0 100644 --- a/panels/printers/pp-new-printer.c +++ b/panels/printers/pp-new-printer.c @@ -484,16 +484,14 @@ get_ppd_item_from_output (GVariant *output) if (array) { GVariantIter *iter; - GVariant *item; for (j = 0; j < G_N_ELEMENTS (match_levels) && !ppd_item; j++) { + const gchar *driver, *match; + g_variant_get (array, "a(ss)", &iter); - while ((item = g_variant_iter_next_value (iter)) && !ppd_item) + while (g_variant_iter_next (iter, "(&s&s)", &driver, &match) && !ppd_item) { - const gchar *driver, *match; - - g_variant_get (item, "(&s&s)", &driver, &match); if (g_str_equal (match, match_levels[j])) { ppd_item = g_new0 (PPDName, 1); @@ -510,8 +508,6 @@ get_ppd_item_from_output (GVariant *output) else if (g_strcmp0 (match, "none") == 0) ppd_item->ppd_match_level = PPD_NO_MATCH; } - - g_variant_unref (item); } } @@ -1040,16 +1036,11 @@ get_missing_executables_cb (GObject *source_object, if (array) { GVariantIter *iter; - GVariant *item; - gchar *executable; + const gchar *executable; g_variant_get (array, "as", &iter); - while ((item = g_variant_iter_next_value (iter))) - { - g_variant_get (item, "s", &executable); - executables = g_list_append (executables, executable); - g_variant_unref (item); - } + while (g_variant_iter_next (iter, "&s", &executable)) + executables = g_list_append (executables, g_strdup (executable)); g_variant_unref (array); } diff --git a/panels/printers/pp-utils.c b/panels/printers/pp-utils.c index 5320903e65..3309f30d50 100644 --- a/panels/printers/pp-utils.c +++ b/panels/printers/pp-utils.c @@ -1935,23 +1935,17 @@ get_ppd_names_async_dbus_scb (GObject *source_object, if (array) { GVariantIter *iter; - GVariant *item; for (j = 0; j < G_N_ELEMENTS (match_levels) && n < data->count; j++) { + const gchar *driver, *match; + g_variant_get (array, "a(ss)", &iter); - while ((item = g_variant_iter_next_value (iter))) + while (g_variant_iter_next (iter, "(&s&s)", &driver, &match)) { - const gchar *driver, *match; - - g_variant_get (item, - "(&s&s)", - &driver, - &match); - if (g_str_equal (match, match_levels[j]) && n < data->count) { ppd_item = g_new0 (PPDName, 1); @@ -1972,8 +1966,6 @@ get_ppd_names_async_dbus_scb (GObject *source_object, n++; } - - g_variant_unref (item); } } @@ -2172,26 +2164,19 @@ get_device_attributes_async_dbus_cb (GObject *source_object, if (devices_variant) { GVariantIter *iter; - GVariant *item; gint index = -1; if (data->device_uri) { + const gchar *key, *value; g_autofree gchar *suffix = NULL; g_variant_get (devices_variant, "a{ss}", &iter); - while ((item = g_variant_iter_next_value (iter))) + while (g_variant_iter_next (iter, "{&s&s}", &key, &value)) { - const gchar *key, *value; - - g_variant_get (item, - "{&s&s}", - &key, - &value); - if (g_str_equal (value, data->device_uri)) { gchar *number = g_strrstr (key, ":"); @@ -2205,8 +2190,6 @@ get_device_attributes_async_dbus_cb (GObject *source_object, index = -1; } } - - g_variant_unref (item); } suffix = g_strdup_printf (":%d", index); @@ -2215,15 +2198,8 @@ get_device_attributes_async_dbus_cb (GObject *source_object, "a{ss}", &iter); - while ((item = g_variant_iter_next_value (iter))) + while (g_variant_iter_next (iter, "{&s&s}", &key, &value)) { - const gchar *key, *value; - - g_variant_get (item, - "{&s&s}", - &key, - &value); - if (g_str_has_suffix (key, suffix)) { if (g_str_has_prefix (key, "device-id")) @@ -2236,8 +2212,6 @@ get_device_attributes_async_dbus_cb (GObject *source_object, device_make_and_model = g_strdup (value); } } - - g_variant_unref (item); } } @@ -3333,21 +3307,15 @@ get_cups_devices_async_dbus_cb (GObject *source_object, if (devices_variant) { GVariantIter *iter; - GVariant *item; + const gchar *key, *value; gint index = -1, max_index = -1, i; g_variant_get (devices_variant, "a{ss}", &iter); - while ((item = g_variant_iter_next_value (iter))) + while (g_variant_iter_next (iter, "{&s&s}", &key, &value)) { - const gchar *key, *value; - - g_variant_get (item, "{&s&s}", &key, &value); - index = get_suffix_index (key); if (index > max_index) max_index = index; - - g_variant_unref (item); } if (max_index >= 0) @@ -3356,12 +3324,8 @@ get_cups_devices_async_dbus_cb (GObject *source_object, devices = g_new0 (PpPrintDevice *, num_of_devices); g_variant_get (devices_variant, "a{ss}", &iter); - while ((item = g_variant_iter_next_value (iter))) + while (g_variant_iter_next (iter, "{&s&s}", &key, &value)) { - const gchar *key, *value; - - g_variant_get (item, "{&s&s}", &key, &value); - index = get_suffix_index (key); if (index >= 0) { @@ -3391,8 +3355,6 @@ get_cups_devices_async_dbus_cb (GObject *source_object, g_object_set (devices[index], "acquisition-method", ACQUISITION_METHOD_DEFAULT_CUPS_SERVER, NULL); } - - g_variant_unref (item); } for (i = 0; i < num_of_devices; i++) -- GitLab From 46443af0ca5b63fb0eae6e1287d986a78fada677 Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Thu, 21 Nov 2019 21:29:58 +1300 Subject: [PATCH 02/10] printers: Replace GVariant iteration with direct call to copy value --- panels/printers/pp-new-printer-dialog.c | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/panels/printers/pp-new-printer-dialog.c b/panels/printers/pp-new-printer-dialog.c index 657f45e8a4..eed08d063c 100644 --- a/panels/printers/pp-new-printer-dialog.c +++ b/panels/printers/pp-new-printer-dialog.c @@ -898,7 +898,7 @@ group_physical_devices_dbus_cb (GObject *source_object, GVariant *output; g_autoptr(GError) error = NULL; gchar ***result = NULL; - gint i, j; + gint i; output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), res, @@ -914,26 +914,14 @@ group_physical_devices_dbus_cb (GObject *source_object, if (array) { GVariantIter *iter; - GVariantIter *subiter; - GVariant *item; + GStrv device_uris; result = g_new0 (gchar **, g_variant_n_children (array) + 1); g_variant_get (array, "aas", &iter); i = 0; - while ((item = g_variant_iter_next_value (iter))) + while (g_variant_iter_next (iter, "^as", &device_uris)) { - const gchar *device_uri; - - result[i] = g_new0 (gchar *, g_variant_n_children (item) + 1); - g_variant_get (item, "as", &subiter); - j = 0; - while (g_variant_iter_next (subiter, "&s", &device_uri)) - { - result[i][j] = g_strdup (device_uri); - j++; - } - - g_variant_unref (item); + result[i] = device_uris; i++; } -- GitLab From 67074b7aeaa276568bd8657641f57cb14c8906af Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Thu, 21 Nov 2019 21:35:08 +1300 Subject: [PATCH 03/10] printers: Fix GVariantIter leaks --- panels/printers/pp-new-printer-dialog.c | 2 +- panels/printers/pp-new-printer.c | 5 ++--- panels/printers/pp-utils.c | 13 +++++++------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/panels/printers/pp-new-printer-dialog.c b/panels/printers/pp-new-printer-dialog.c index eed08d063c..f4a6f97575 100644 --- a/panels/printers/pp-new-printer-dialog.c +++ b/panels/printers/pp-new-printer-dialog.c @@ -913,7 +913,7 @@ group_physical_devices_dbus_cb (GObject *source_object, if (array) { - GVariantIter *iter; + g_autoptr(GVariantIter) iter = NULL; GStrv device_uris; result = g_new0 (gchar **, g_variant_n_children (array) + 1); diff --git a/panels/printers/pp-new-printer.c b/panels/printers/pp-new-printer.c index b9d734afd0..b0412e1830 100644 --- a/panels/printers/pp-new-printer.c +++ b/panels/printers/pp-new-printer.c @@ -483,10 +483,9 @@ get_ppd_item_from_output (GVariant *output) g_variant_get (output, "(@a(ss))", &array); if (array) { - GVariantIter *iter; - for (j = 0; j < G_N_ELEMENTS (match_levels) && !ppd_item; j++) { + g_autoptr(GVariantIter) iter = NULL; const gchar *driver, *match; g_variant_get (array, "a(ss)", &iter); @@ -1035,7 +1034,7 @@ get_missing_executables_cb (GObject *source_object, if (array) { - GVariantIter *iter; + g_autoptr(GVariantIter) iter = NULL; const gchar *executable; g_variant_get (array, "as", &iter); diff --git a/panels/printers/pp-utils.c b/panels/printers/pp-utils.c index 3309f30d50..486c655dc0 100644 --- a/panels/printers/pp-utils.c +++ b/panels/printers/pp-utils.c @@ -1934,10 +1934,9 @@ get_ppd_names_async_dbus_scb (GObject *source_object, if (array) { - GVariantIter *iter; - for (j = 0; j < G_N_ELEMENTS (match_levels) && n < data->count; j++) { + g_autoptr(GVariantIter) iter = NULL; const gchar *driver, *match; g_variant_get (array, @@ -2163,11 +2162,11 @@ get_device_attributes_async_dbus_cb (GObject *source_object, if (devices_variant) { - GVariantIter *iter; gint index = -1; if (data->device_uri) { + g_autoptr(GVariantIter) iter = NULL; const gchar *key, *value; g_autofree gchar *suffix = NULL; @@ -3306,7 +3305,7 @@ get_cups_devices_async_dbus_cb (GObject *source_object, if (devices_variant) { - GVariantIter *iter; + g_autoptr(GVariantIter) iter = NULL; const gchar *key, *value; gint index = -1, max_index = -1, i; @@ -3320,11 +3319,13 @@ get_cups_devices_async_dbus_cb (GObject *source_object, if (max_index >= 0) { + g_autoptr(GVariantIter) iter2 = NULL; + num_of_devices = max_index + 1; devices = g_new0 (PpPrintDevice *, num_of_devices); - g_variant_get (devices_variant, "a{ss}", &iter); - while (g_variant_iter_next (iter, "{&s&s}", &key, &value)) + g_variant_get (devices_variant, "a{ss}", &iter2); + while (g_variant_iter_next (iter2, "{&s&s}", &key, &value)) { index = get_suffix_index (key); if (index >= 0) -- GitLab From 3a7533da1b8dd8e6d2d53972fcb3a6ceae22cac5 Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Thu, 21 Nov 2019 21:47:01 +1300 Subject: [PATCH 04/10] printers: Use g_autoptr for the results of D-Bus calls --- panels/printers/pp-job.c | 14 +- panels/printers/pp-new-printer-dialog.c | 10 +- panels/printers/pp-new-printer.c | 75 ++++------- panels/printers/pp-printer.c | 22 ++-- panels/printers/pp-utils.c | 168 ++++++++++-------------- 5 files changed, 110 insertions(+), 179 deletions(-) diff --git a/panels/printers/pp-job.c b/panels/printers/pp-job.c index 7664222a38..81e4341ec6 100644 --- a/panels/printers/pp-job.c +++ b/panels/printers/pp-job.c @@ -72,17 +72,12 @@ pp_job_cancel_purge_async_dbus_cb (GObject *source_object, GAsyncResult *res, gpointer user_data) { - GVariant *output; + g_autoptr(GVariant) output = NULL; output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), res, NULL); g_object_unref (source_object); - - if (output != NULL) - { - g_variant_unref (output); - } } void @@ -123,17 +118,12 @@ pp_job_set_hold_until_async_dbus_cb (GObject *source_object, GAsyncResult *res, gpointer user_data) { - GVariant *output; + g_autoptr(GVariant) output = NULL; output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), res, NULL); g_object_unref (source_object); - - if (output) - { - g_variant_unref (output); - } } void diff --git a/panels/printers/pp-new-printer-dialog.c b/panels/printers/pp-new-printer-dialog.c index f4a6f97575..ebeba08491 100644 --- a/panels/printers/pp-new-printer-dialog.c +++ b/panels/printers/pp-new-printer-dialog.c @@ -895,10 +895,10 @@ group_physical_devices_dbus_cb (GObject *source_object, GAsyncResult *res, gpointer user_data) { - GVariant *output; - g_autoptr(GError) error = NULL; - gchar ***result = NULL; - gint i; + g_autoptr(GVariant) output = NULL; + g_autoptr(GError) error = NULL; + gchar ***result = NULL; + gint i; output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), res, @@ -927,8 +927,6 @@ group_physical_devices_dbus_cb (GObject *source_object, g_variant_unref (array); } - - g_variant_unref (output); } else if (error && error->domain == G_DBUS_ERROR && diff --git a/panels/printers/pp-new-printer.c b/panels/printers/pp-new-printer.c index b0412e1830..4461dcfb39 100644 --- a/panels/printers/pp-new-printer.c +++ b/panels/printers/pp-new-printer.c @@ -392,7 +392,7 @@ printer_add_real_async_dbus_cb (GObject *source_object, gpointer user_data) { PpNewPrinter *self = user_data; - GVariant *output; + g_autoptr(GVariant) output = NULL; g_autoptr(GError) error = NULL; output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), @@ -409,8 +409,6 @@ printer_add_real_async_dbus_cb (GObject *source_object, { g_warning ("cups-pk-helper: addition of printer %s failed: %s", self->name, ret_error); } - - g_variant_unref (output); } else { @@ -524,7 +522,7 @@ printer_add_async_scb3 (GObject *source_object, gpointer user_data) { PpNewPrinter *self = user_data; - GVariant *output; + g_autoptr(GVariant) output = NULL; PPDName *ppd_item = NULL; g_autoptr(GError) error = NULL; @@ -536,7 +534,6 @@ printer_add_async_scb3 (GObject *source_object, if (output) { ppd_item = get_ppd_item_from_output (output); - g_variant_unref (output); } else { @@ -568,7 +565,7 @@ install_printer_drivers_cb (GObject *source_object, gpointer user_data) { PpNewPrinter *self = user_data; - GVariant *output; + g_autoptr(GVariant) output = NULL; g_autoptr(GError) error = NULL; output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), @@ -576,11 +573,7 @@ install_printer_drivers_cb (GObject *source_object, &error); g_object_unref (source_object); - if (output) - { - g_variant_unref (output); - } - else + if (output == NULL) { if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) g_warning ("%s", error->message); @@ -627,7 +620,7 @@ printer_add_async_scb (GObject *source_object, PpNewPrinter *self = user_data; GDBusConnection *bus; GVariantBuilder array_builder; - GVariant *output; + g_autoptr(GVariant) output = NULL; gboolean cancelled = FALSE; PPDName *ppd_item = NULL; g_autoptr(GError) error = NULL; @@ -640,7 +633,6 @@ printer_add_async_scb (GObject *source_object, if (output) { ppd_item = get_ppd_item_from_output (output); - g_variant_unref (output); } else { @@ -786,20 +778,16 @@ printer_set_accepting_jobs_cb (GObject *source_object, GAsyncResult *res, gpointer user_data) { - GVariant *output; - PCData *data = (PCData *) user_data; - g_autoptr(GError) error = NULL; + g_autoptr(GVariant) output = NULL; + PCData *data = (PCData *) user_data; + g_autoptr(GError) error = NULL; output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), res, &error); g_object_unref (source_object); - if (output) - { - g_variant_unref (output); - } - else + if (output == NULL) { if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) g_warning ("%s", error->message); @@ -814,20 +802,16 @@ printer_set_enabled_cb (GObject *source_object, GAsyncResult *res, gpointer user_data) { - GVariant *output; - PCData *data = (PCData *) user_data; - g_autoptr(GError) error = NULL; + g_autoptr(GVariant) output = NULL; + PCData *data = (PCData *) user_data; + g_autoptr(GError) error = NULL; output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), res, &error); g_object_unref (source_object); - if (output) - { - g_variant_unref (output); - } - else + if (output == NULL) { if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) g_warning ("%s", error->message); @@ -884,20 +868,16 @@ install_package_names_cb (GObject *source_object, GAsyncResult *res, gpointer user_data) { - GVariant *output; - IMEData *data = (IMEData *) user_data; - g_autoptr(GError) error = NULL; + g_autoptr(GVariant) output = NULL; + IMEData *data = (IMEData *) user_data; + g_autoptr(GError) error = NULL; output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), res, &error); g_object_unref (source_object); - if (output) - { - g_variant_unref (output); - } - else + if (output == NULL) { if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) g_warning ("%s", error->message); @@ -912,10 +892,10 @@ search_files_cb (GObject *source_object, GAsyncResult *res, gpointer user_data) { - GVariant *output; - IMEData *data = (IMEData *) user_data; - g_autoptr(GError) error = NULL; - GList *item; + g_autoptr(GVariant) output = NULL; + IMEData *data = (IMEData *) user_data; + g_autoptr(GError) error = NULL; + GList *item; output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), res, @@ -932,7 +912,6 @@ search_files_cb (GObject *source_object, if (!installed) data->packages = g_list_append (data->packages, g_strdup (package)); - g_variant_unref (output); } else { @@ -1010,11 +989,11 @@ get_missing_executables_cb (GObject *source_object, GAsyncResult *res, gpointer user_data) { - GVariant *output; - IMEData *data = (IMEData *) user_data; - g_autoptr(GError) error = NULL; - GList *executables = NULL; - GList *item; + g_autoptr(GVariant) output = NULL; + IMEData *data = (IMEData *) user_data; + g_autoptr(GError) error = NULL; + GList *executables = NULL; + GList *item; if (data->ppd_file_name) { @@ -1043,8 +1022,6 @@ get_missing_executables_cb (GObject *source_object, g_variant_unref (array); } - - g_variant_unref (output); } else if (error->domain == G_DBUS_ERROR && (error->code == G_DBUS_ERROR_SERVICE_UNKNOWN || diff --git a/panels/printers/pp-printer.c b/panels/printers/pp-printer.c index 817da60580..5a823ba44d 100644 --- a/panels/printers/pp-printer.c +++ b/panels/printers/pp-printer.c @@ -155,11 +155,11 @@ printer_rename_dbus_cb (GObject *source_object, GAsyncResult *res, gpointer user_data) { - PpPrinter *self; - GVariant *output; - gboolean result = FALSE; - g_autoptr(GError) error = NULL; - GTask *task = user_data; + PpPrinter *self; + g_autoptr(GVariant) output = NULL; + gboolean result = FALSE; + g_autoptr(GError) error = NULL; + GTask *task = user_data; output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), res, @@ -186,8 +186,6 @@ printer_rename_dbus_cb (GObject *source_object, } g_task_return_boolean (task, result); - - g_variant_unref (output); } else { @@ -424,10 +422,10 @@ pp_printer_delete_dbus_cb (GObject *source_object, GAsyncResult *res, gpointer user_data) { - GVariant *output; - gboolean result = FALSE; - g_autoptr(GError) error = NULL; - GTask *task = user_data; + g_autoptr(GVariant) output = NULL; + gboolean result = FALSE; + g_autoptr(GError) error = NULL; + GTask *task = user_data; output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), res, @@ -448,8 +446,6 @@ pp_printer_delete_dbus_cb (GObject *source_object, result = TRUE; g_task_return_boolean (task, result); - - g_variant_unref (output); } else { diff --git a/panels/printers/pp-utils.c b/panels/printers/pp-utils.c index 486c655dc0..1f5f1c7ded 100644 --- a/panels/printers/pp-utils.c +++ b/panels/printers/pp-utils.c @@ -458,7 +458,7 @@ printer_rename (const gchar *old_name, } else { - GVariant *output; + g_autoptr(GVariant) output = NULL; g_autoptr(GError) add_error = NULL; output = g_dbus_connection_call_sync (bus, @@ -486,8 +486,6 @@ printer_rename (const gchar *old_name, g_variant_get (output, "(&s)", &ret_error); if (ret_error[0] != '\0') g_warning ("cups-pk-helper: rename of printer %s to %s failed: %s", old_name, new_name, ret_error); - - g_variant_unref (output); } else { @@ -538,10 +536,10 @@ gboolean printer_set_location (const gchar *printer_name, const gchar *location) { - GDBusConnection *bus; - GVariant *output; - gboolean result = FALSE; - g_autoptr(GError) error = NULL; + GDBusConnection *bus; + g_autoptr(GVariant) output = NULL; + gboolean result = FALSE; + g_autoptr(GError) error = NULL; if (!printer_name || !location) return TRUE; @@ -575,8 +573,6 @@ printer_set_location (const gchar *printer_name, g_warning ("cups-pk-helper: setting of location for printer %s failed: %s", printer_name, ret_error); else result = TRUE; - - g_variant_unref (output); } else { @@ -591,10 +587,10 @@ printer_set_accepting_jobs (const gchar *printer_name, gboolean accepting_jobs, const gchar *reason) { - GDBusConnection *bus; - GVariant *output; - gboolean result = FALSE; - g_autoptr(GError) error = NULL; + GDBusConnection *bus; + g_autoptr(GVariant) output = NULL; + gboolean result = FALSE; + g_autoptr(GError) error = NULL; if (!printer_name) return TRUE; @@ -631,7 +627,6 @@ printer_set_accepting_jobs (const gchar *printer_name, g_warning ("cups-pk-helper: setting of acceptance of jobs for printer %s failed: %s", printer_name, ret_error); else result = TRUE; - g_variant_unref (output); } else { @@ -645,10 +640,10 @@ gboolean printer_set_enabled (const gchar *printer_name, gboolean enabled) { - GDBusConnection *bus; - GVariant *output; - gboolean result = FALSE; - g_autoptr(GError) error = NULL; + GDBusConnection *bus; + g_autoptr(GVariant) output = NULL; + gboolean result = FALSE; + g_autoptr(GError) error = NULL; if (!printer_name) return TRUE; @@ -682,8 +677,6 @@ printer_set_enabled (const gchar *printer_name, g_warning ("cups-pk-helper: setting of enablement of printer %s failed: %s", printer_name, ret_error); else result = TRUE; - - g_variant_unref (output); } else { @@ -696,10 +689,10 @@ printer_set_enabled (const gchar *printer_name, gboolean printer_delete (const gchar *printer_name) { - GDBusConnection *bus; - GVariant *output; - gboolean result = FALSE; - g_autoptr(GError) error = NULL; + GDBusConnection *bus; + g_autoptr(GVariant) output = NULL; + gboolean result = FALSE; + g_autoptr(GError) error = NULL; if (!printer_name) return TRUE; @@ -733,8 +726,6 @@ printer_delete (const gchar *printer_name) g_warning ("cups-pk-helper: removing of printer %s failed: %s", printer_name, ret_error); else result = TRUE; - - g_variant_unref (output); } else { @@ -749,7 +740,6 @@ printer_set_default (const gchar *printer_name) { GDBusConnection *bus; const char *cups_server; - GVariant *output; gboolean result = FALSE; g_autoptr(GError) error = NULL; @@ -774,6 +764,8 @@ printer_set_default (const gchar *printer_name) } else { + g_autoptr(GVariant) output = NULL; + output = g_dbus_connection_call_sync (bus, MECHANISM_BUS, "/", @@ -796,8 +788,6 @@ printer_set_default (const gchar *printer_name) g_warning ("cups-pk-helper: setting default printer to %s failed: %s", printer_name, ret_error); else result = TRUE; - - g_variant_unref (output); } else { @@ -820,10 +810,10 @@ gboolean printer_set_shared (const gchar *printer_name, gboolean shared) { - GDBusConnection *bus; - GVariant *output; - gboolean result = FALSE; - g_autoptr(GError) error = NULL; + GDBusConnection *bus; + g_autoptr(GVariant) output = NULL; + gboolean result = FALSE; + g_autoptr(GError) error = NULL; if (!printer_name) return TRUE; @@ -857,8 +847,6 @@ printer_set_shared (const gchar *printer_name, g_warning ("cups-pk-helper: setting of sharing of printer %s failed: %s", printer_name, ret_error); else result = TRUE; - - g_variant_unref (output); } else { @@ -873,10 +861,10 @@ printer_set_job_sheets (const gchar *printer_name, const gchar *start_sheet, const gchar *end_sheet) { - GDBusConnection *bus; - GVariant *output; - g_autoptr(GError) error = NULL; - gboolean result = FALSE; + GDBusConnection *bus; + g_autoptr(GVariant) output = NULL; + g_autoptr(GError) error = NULL; + gboolean result = FALSE; if (!printer_name || !start_sheet || !end_sheet) return TRUE; @@ -910,8 +898,6 @@ printer_set_job_sheets (const gchar *printer_name, g_warning ("cups-pk-helper: setting of job sheets for printer %s failed: %s", printer_name, ret_error); else result = TRUE; - - g_variant_unref (output); } else { @@ -926,10 +912,10 @@ printer_set_policy (const gchar *printer_name, const gchar *policy, gboolean error_policy) { - GDBusConnection *bus; - GVariant *output; - gboolean result = FALSE; - g_autoptr(GError) error = NULL; + GDBusConnection *bus; + g_autoptr(GVariant) output = NULL; + gboolean result = FALSE; + g_autoptr(GError) error = NULL; if (!printer_name || !policy) return TRUE; @@ -976,8 +962,6 @@ printer_set_policy (const gchar *printer_name, g_warning ("cups-pk-helper: setting of a policy for printer %s failed: %s", printer_name, ret_error); else result = TRUE; - - g_variant_unref (output); } else { @@ -992,12 +976,12 @@ printer_set_users (const gchar *printer_name, gchar **users, gboolean allowed) { - GDBusConnection *bus; - GVariantBuilder array_builder; - gint i; - GVariant *output; - gboolean result = FALSE; - g_autoptr(GError) error = NULL; + GDBusConnection *bus; + GVariantBuilder array_builder; + gint i; + g_autoptr(GVariant) output = NULL; + gboolean result = FALSE; + g_autoptr(GError) error = NULL; if (!printer_name || !users) return TRUE; @@ -1048,8 +1032,6 @@ printer_set_users (const gchar *printer_name, g_warning ("cups-pk-helper: setting of access list for printer %s failed: %s", printer_name, ret_error); else result = TRUE; - - g_variant_unref (output); } else { @@ -1063,10 +1045,10 @@ gboolean class_add_printer (const gchar *class_name, const gchar *printer_name) { - GDBusConnection *bus; - GVariant *output; - gboolean result = FALSE; - g_autoptr(GError) error = NULL; + GDBusConnection *bus; + g_autoptr(GVariant) output = NULL; + gboolean result = FALSE; + g_autoptr(GError) error = NULL; if (!class_name || !printer_name) return TRUE; @@ -1100,8 +1082,6 @@ class_add_printer (const gchar *class_name, g_warning ("cups-pk-helper: adding of printer %s to class %s failed: %s", printer_name, class_name, ret_error); else result = TRUE; - - g_variant_unref (output); } else { @@ -1470,10 +1450,10 @@ printer_set_ppd_async_dbus_cb (GObject *source_object, GAsyncResult *res, gpointer user_data) { - GVariant *output; - gboolean result = FALSE; - PSPData *data = (PSPData *) user_data; - g_autoptr(GError) error = NULL; + g_autoptr(GVariant) output = NULL; + gboolean result = FALSE; + PSPData *data = (PSPData *) user_data; + g_autoptr(GError) error = NULL; output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), res, @@ -1489,8 +1469,6 @@ printer_set_ppd_async_dbus_cb (GObject *source_object, g_warning ("cups-pk-helper: setting of driver for printer %s failed: %s", data->printer_name, ret_error); else result = TRUE; - - g_variant_unref (output); } else { @@ -1905,14 +1883,14 @@ get_ppd_names_async_dbus_scb (GObject *source_object, GAsyncResult *res, gpointer user_data) { - GVariant *output; - PPDName *ppd_item; - PPDName **result = NULL; - GPNData *data = (GPNData *) user_data; - g_autoptr(GError) error = NULL; - GList *driver_list = NULL; - GList *iter; - gint i, j, n = 0; + g_autoptr(GVariant) output = NULL; + PPDName *ppd_item; + PPDName **result = NULL; + GPNData *data = (GPNData *) user_data; + g_autoptr(GError) error = NULL; + GList *driver_list = NULL; + GList *iter; + gint i, j, n = 0; static const char * const match_levels[] = { "exact-cmd", "exact", @@ -1970,8 +1948,6 @@ get_ppd_names_async_dbus_scb (GObject *source_object, g_variant_unref (array); } - - g_variant_unref (output); } else { @@ -2134,12 +2110,12 @@ get_device_attributes_async_dbus_cb (GObject *source_object, gpointer user_data) { - GVariant *output; - GDAData *data = (GDAData *) user_data; - g_autoptr(GError) error = NULL; - GList *tmp; - gchar *device_id = NULL; - gchar *device_make_and_model = NULL; + g_autoptr(GVariant) output = NULL; + GDAData *data = (GDAData *) user_data; + g_autoptr(GError) error = NULL; + GList *tmp; + gchar *device_id = NULL; + gchar *device_make_and_model = NULL; output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), res, @@ -2216,8 +2192,6 @@ get_device_attributes_async_dbus_cb (GObject *source_object, g_variant_unref (devices_variant); } - - g_variant_unref (output); } else { @@ -3154,10 +3128,10 @@ printer_add_option_async_dbus_cb (GObject *source_object, GAsyncResult *res, gpointer user_data) { - GVariant *output; - gboolean success = FALSE; - PAOData *data = (PAOData *) user_data; - g_autoptr(GError) error = NULL; + g_autoptr(GVariant) output = NULL; + gboolean success = FALSE; + PAOData *data = (PAOData *) user_data; + g_autoptr(GError) error = NULL; output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), res, @@ -3173,8 +3147,6 @@ printer_add_option_async_dbus_cb (GObject *source_object, g_warning ("cups-pk-helper: setting of an option failed: %s", ret_error); else success = TRUE; - - g_variant_unref (output); } else { @@ -3277,12 +3249,12 @@ get_cups_devices_async_dbus_cb (GObject *source_object, gpointer user_data) { - PpPrintDevice **devices = NULL; - GVariant *output; - GCDData *data = (GCDData *) user_data; - g_autoptr(GError) error = NULL; - GList *result = NULL; - gint num_of_devices = 0; + PpPrintDevice **devices = NULL; + g_autoptr(GVariant) output = NULL; + GCDData *data = (GCDData *) user_data; + g_autoptr(GError) error = NULL; + GList *result = NULL; + gint num_of_devices = 0; output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object), res, @@ -3366,8 +3338,6 @@ get_cups_devices_async_dbus_cb (GObject *source_object, g_variant_unref (devices_variant); } - - g_variant_unref (output); } else { -- GitLab From 815db8f4043824c1a898c3bf769c793a34cc913c Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Thu, 21 Nov 2019 21:52:09 +1300 Subject: [PATCH 05/10] printers: Replace explicit g_variant_unref calls with g_autoptr --- panels/printers/cc-printers-panel.c | 28 ++++++++++--------------- panels/printers/pp-new-printer-dialog.c | 4 +--- panels/printers/pp-new-printer.c | 9 +++----- panels/printers/pp-utils.c | 12 +++-------- 4 files changed, 18 insertions(+), 35 deletions(-) diff --git a/panels/printers/cc-printers-panel.c b/panels/printers/cc-printers-panel.c index 5d3322384f..db5bc9bc8b 100644 --- a/panels/printers/cc-printers-panel.c +++ b/panels/printers/cc-printers-panel.c @@ -136,19 +136,21 @@ execute_action (CcPrintersPanel *self, PpPrinterEntry *printer_entry; const gchar *action_name; const gchar *printer_name; - GVariant *variant; - GVariant *action_variant; gint count; count = g_variant_n_children (action); if (count == 2) { + g_autoptr(GVariant) action_variant = NULL; + g_variant_get_child (action, 0, "v", &action_variant); action_name = g_variant_get_string (action_variant, NULL); /* authenticate-jobs printer-name */ if (g_strcmp0 (action_name, "authenticate-jobs") == 0) { + g_autoptr(GVariant) variant = NULL; + g_variant_get_child (action, 1, "v", &variant); printer_name = g_variant_get_string (variant, NULL); @@ -157,12 +159,12 @@ execute_action (CcPrintersPanel *self, pp_printer_entry_authenticate_jobs (printer_entry); else g_warning ("Could not find printer \"%s\"!", printer_name); - - g_variant_unref (variant); } /* show-jobs printer-name */ else if (g_strcmp0 (action_name, "show-jobs") == 0) { + g_autoptr(GVariant) variant = NULL; + g_variant_get_child (action, 1, "v", &variant); printer_name = g_variant_get_string (variant, NULL); @@ -171,11 +173,7 @@ execute_action (CcPrintersPanel *self, pp_printer_entry_show_jobs_dialog (printer_entry); else g_warning ("Could not find printer \"%s\"!", printer_name); - - g_variant_unref (variant); } - - g_variant_unref (action_variant); } } @@ -336,9 +334,7 @@ on_get_job_attributes_cb (GObject *source_object, CcPrintersPanel *self = (CcPrintersPanel*) user_data; const gchar *job_originating_user_name; const gchar *job_printer_uri; - GVariant *attributes; - GVariant *username; - GVariant *printer_uri; + g_autoptr(GVariant) attributes = NULL; g_autoptr(GError) error = NULL; attributes = pp_job_get_attributes_finish (PP_JOB (source_object), res, &error); @@ -346,8 +342,12 @@ on_get_job_attributes_cb (GObject *source_object, if (attributes != NULL) { + g_autoptr(GVariant) username = NULL; + if ((username = g_variant_lookup_value (attributes, "job-originating-user-name", G_VARIANT_TYPE ("as"))) != NULL) { + g_autoptr(GVariant) printer_uri = NULL; + if ((printer_uri = g_variant_lookup_value (attributes, "job-printer-uri", G_VARIANT_TYPE ("as"))) != NULL) { job_originating_user_name = g_variant_get_string (g_variant_get_child_value (username, 0), NULL); @@ -366,14 +366,8 @@ on_get_job_attributes_cb (GObject *source_object, pp_printer_entry_update_jobs_count (printer_entry); } - - g_variant_unref (printer_uri); } - - g_variant_unref (username); } - - g_variant_unref (attributes); } } diff --git a/panels/printers/pp-new-printer-dialog.c b/panels/printers/pp-new-printer-dialog.c index ebeba08491..71424eda74 100644 --- a/panels/printers/pp-new-printer-dialog.c +++ b/panels/printers/pp-new-printer-dialog.c @@ -907,7 +907,7 @@ group_physical_devices_dbus_cb (GObject *source_object, if (output) { - GVariant *array; + g_autoptr(GVariant) array = NULL; g_variant_get (output, "(@aas)", &array); @@ -924,8 +924,6 @@ group_physical_devices_dbus_cb (GObject *source_object, result[i] = device_uris; i++; } - - g_variant_unref (array); } } else if (error && diff --git a/panels/printers/pp-new-printer.c b/panels/printers/pp-new-printer.c index 4461dcfb39..0e4593feb4 100644 --- a/panels/printers/pp-new-printer.c +++ b/panels/printers/pp-new-printer.c @@ -466,7 +466,6 @@ printer_add_real_async (PpNewPrinter *self) static PPDName * get_ppd_item_from_output (GVariant *output) { - GVariant *array; PPDName *ppd_item = NULL; gint j; static const char * const match_levels[] = { @@ -478,6 +477,8 @@ get_ppd_item_from_output (GVariant *output) if (output) { + g_autoptr(GVariant) array = NULL; + g_variant_get (output, "(@a(ss))", &array); if (array) { @@ -507,8 +508,6 @@ get_ppd_item_from_output (GVariant *output) } } } - - g_variant_unref (array); } } @@ -1007,7 +1006,7 @@ get_missing_executables_cb (GObject *source_object, if (output) { - GVariant *array; + g_autoptr(GVariant) array = NULL; g_variant_get (output, "(@as)", &array); @@ -1019,8 +1018,6 @@ get_missing_executables_cb (GObject *source_object, g_variant_get (array, "as", &iter); while (g_variant_iter_next (iter, "&s", &executable)) executables = g_list_append (executables, g_strdup (executable)); - - g_variant_unref (array); } } else if (error->domain == G_DBUS_ERROR && diff --git a/panels/printers/pp-utils.c b/panels/printers/pp-utils.c index 1f5f1c7ded..2b56474b65 100644 --- a/panels/printers/pp-utils.c +++ b/panels/printers/pp-utils.c @@ -1905,7 +1905,7 @@ get_ppd_names_async_dbus_scb (GObject *source_object, if (output) { - GVariant *array; + g_autoptr(GVariant) array = NULL; g_variant_get (output, "(@a(ss))", &array); @@ -1945,8 +1945,6 @@ get_ppd_names_async_dbus_scb (GObject *source_object, } } } - - g_variant_unref (array); } } else @@ -2125,7 +2123,7 @@ get_device_attributes_async_dbus_cb (GObject *source_object, if (output) { const gchar *ret_error; - GVariant *devices_variant = NULL; + g_autoptr(GVariant) devices_variant = NULL; g_variant_get (output, "(&s@a{ss})", &ret_error, @@ -2189,8 +2187,6 @@ get_device_attributes_async_dbus_cb (GObject *source_object, } } } - - g_variant_unref (devices_variant); } } else @@ -3263,7 +3259,7 @@ get_cups_devices_async_dbus_cb (GObject *source_object, if (output) { const gchar *ret_error; - GVariant *devices_variant = NULL; + g_autoptr(GVariant) devices_variant = NULL; gboolean is_network_device; g_variant_get (output, "(&s@a{ss})", @@ -3335,8 +3331,6 @@ get_cups_devices_async_dbus_cb (GObject *source_object, g_free (devices); } - - g_variant_unref (devices_variant); } } else -- GitLab From 2b9815dbf84a7b99470d9ee76b29fe2515c1e4ed Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Thu, 21 Nov 2019 21:56:08 +1300 Subject: [PATCH 06/10] printers: Remove unnecessary checks on result of g_variant_get This always returns a value or hits an assertion. --- panels/printers/pp-new-printer.c | 57 ++++---- panels/printers/pp-utils.c | 219 +++++++++++++++---------------- 2 files changed, 129 insertions(+), 147 deletions(-) diff --git a/panels/printers/pp-new-printer.c b/panels/printers/pp-new-printer.c index 0e4593feb4..a44c6bf1be 100644 --- a/panels/printers/pp-new-printer.c +++ b/panels/printers/pp-new-printer.c @@ -480,32 +480,29 @@ get_ppd_item_from_output (GVariant *output) g_autoptr(GVariant) array = NULL; g_variant_get (output, "(@a(ss))", &array); - if (array) + for (j = 0; j < G_N_ELEMENTS (match_levels) && !ppd_item; j++) { - for (j = 0; j < G_N_ELEMENTS (match_levels) && !ppd_item; j++) - { - g_autoptr(GVariantIter) iter = NULL; - const gchar *driver, *match; + g_autoptr(GVariantIter) iter = NULL; + const gchar *driver, *match; - g_variant_get (array, "a(ss)", &iter); - while (g_variant_iter_next (iter, "(&s&s)", &driver, &match) && !ppd_item) + g_variant_get (array, "a(ss)", &iter); + while (g_variant_iter_next (iter, "(&s&s)", &driver, &match) && !ppd_item) + { + if (g_str_equal (match, match_levels[j])) { - if (g_str_equal (match, match_levels[j])) - { - ppd_item = g_new0 (PPDName, 1); - ppd_item->ppd_name = g_strdup (driver); - - if (g_strcmp0 (match, "exact-cmd") == 0) - ppd_item->ppd_match_level = PPD_EXACT_CMD_MATCH; - else if (g_strcmp0 (match, "exact") == 0) - ppd_item->ppd_match_level = PPD_EXACT_MATCH; - else if (g_strcmp0 (match, "close") == 0) - ppd_item->ppd_match_level = PPD_CLOSE_MATCH; - else if (g_strcmp0 (match, "generic") == 0) - ppd_item->ppd_match_level = PPD_GENERIC_MATCH; - else if (g_strcmp0 (match, "none") == 0) - ppd_item->ppd_match_level = PPD_NO_MATCH; - } + ppd_item = g_new0 (PPDName, 1); + ppd_item->ppd_name = g_strdup (driver); + + if (g_strcmp0 (match, "exact-cmd") == 0) + ppd_item->ppd_match_level = PPD_EXACT_CMD_MATCH; + else if (g_strcmp0 (match, "exact") == 0) + ppd_item->ppd_match_level = PPD_EXACT_MATCH; + else if (g_strcmp0 (match, "close") == 0) + ppd_item->ppd_match_level = PPD_CLOSE_MATCH; + else if (g_strcmp0 (match, "generic") == 0) + ppd_item->ppd_match_level = PPD_GENERIC_MATCH; + else if (g_strcmp0 (match, "none") == 0) + ppd_item->ppd_match_level = PPD_NO_MATCH; } } } @@ -1007,18 +1004,14 @@ get_missing_executables_cb (GObject *source_object, if (output) { g_autoptr(GVariant) array = NULL; + g_autoptr(GVariantIter) iter = NULL; + const gchar *executable; g_variant_get (output, "(@as)", &array); - if (array) - { - g_autoptr(GVariantIter) iter = NULL; - const gchar *executable; - - g_variant_get (array, "as", &iter); - while (g_variant_iter_next (iter, "&s", &executable)) - executables = g_list_append (executables, g_strdup (executable)); - } + g_variant_get (array, "as", &iter); + while (g_variant_iter_next (iter, "&s", &executable)) + executables = g_list_append (executables, g_strdup (executable)); } else if (error->domain == G_DBUS_ERROR && (error->code == G_DBUS_ERROR_SERVICE_UNKNOWN || diff --git a/panels/printers/pp-utils.c b/panels/printers/pp-utils.c index 2b56474b65..c1e8b5c21a 100644 --- a/panels/printers/pp-utils.c +++ b/panels/printers/pp-utils.c @@ -1910,39 +1910,36 @@ get_ppd_names_async_dbus_scb (GObject *source_object, g_variant_get (output, "(@a(ss))", &array); - if (array) + for (j = 0; j < G_N_ELEMENTS (match_levels) && n < data->count; j++) { - for (j = 0; j < G_N_ELEMENTS (match_levels) && n < data->count; j++) - { - g_autoptr(GVariantIter) iter = NULL; - const gchar *driver, *match; + g_autoptr(GVariantIter) iter = NULL; + const gchar *driver, *match; - g_variant_get (array, - "a(ss)", - &iter); + g_variant_get (array, + "a(ss)", + &iter); - while (g_variant_iter_next (iter, "(&s&s)", &driver, &match)) + while (g_variant_iter_next (iter, "(&s&s)", &driver, &match)) + { + if (g_str_equal (match, match_levels[j]) && n < data->count) { - if (g_str_equal (match, match_levels[j]) && n < data->count) - { - ppd_item = g_new0 (PPDName, 1); - ppd_item->ppd_name = g_strdup (driver); - - if (g_strcmp0 (match, "exact-cmd") == 0) - ppd_item->ppd_match_level = PPD_EXACT_CMD_MATCH; - else if (g_strcmp0 (match, "exact") == 0) - ppd_item->ppd_match_level = PPD_EXACT_MATCH; - else if (g_strcmp0 (match, "close") == 0) - ppd_item->ppd_match_level = PPD_CLOSE_MATCH; - else if (g_strcmp0 (match, "generic") == 0) - ppd_item->ppd_match_level = PPD_GENERIC_MATCH; - else if (g_strcmp0 (match, "none") == 0) - ppd_item->ppd_match_level = PPD_NO_MATCH; - - driver_list = g_list_append (driver_list, ppd_item); - - n++; - } + ppd_item = g_new0 (PPDName, 1); + ppd_item->ppd_name = g_strdup (driver); + + if (g_strcmp0 (match, "exact-cmd") == 0) + ppd_item->ppd_match_level = PPD_EXACT_CMD_MATCH; + else if (g_strcmp0 (match, "exact") == 0) + ppd_item->ppd_match_level = PPD_EXACT_MATCH; + else if (g_strcmp0 (match, "close") == 0) + ppd_item->ppd_match_level = PPD_CLOSE_MATCH; + else if (g_strcmp0 (match, "generic") == 0) + ppd_item->ppd_match_level = PPD_GENERIC_MATCH; + else if (g_strcmp0 (match, "none") == 0) + ppd_item->ppd_match_level = PPD_NO_MATCH; + + driver_list = g_list_append (driver_list, ppd_item); + + n++; } } } @@ -2124,6 +2121,7 @@ get_device_attributes_async_dbus_cb (GObject *source_object, { const gchar *ret_error; g_autoptr(GVariant) devices_variant = NULL; + gint index = -1; g_variant_get (output, "(&s@a{ss})", &ret_error, @@ -2134,56 +2132,51 @@ get_device_attributes_async_dbus_cb (GObject *source_object, g_warning ("cups-pk-helper: getting of attributes for printer %s failed: %s", data->printer_name, ret_error); } - if (devices_variant) + if (data->device_uri) { - gint index = -1; - - if (data->device_uri) - { - g_autoptr(GVariantIter) iter = NULL; - const gchar *key, *value; - g_autofree gchar *suffix = NULL; + g_autoptr(GVariantIter) iter = NULL; + const gchar *key, *value; + g_autofree gchar *suffix = NULL; - g_variant_get (devices_variant, - "a{ss}", - &iter); + g_variant_get (devices_variant, + "a{ss}", + &iter); - while (g_variant_iter_next (iter, "{&s&s}", &key, &value)) + while (g_variant_iter_next (iter, "{&s&s}", &key, &value)) + { + if (g_str_equal (value, data->device_uri)) { - if (g_str_equal (value, data->device_uri)) + gchar *number = g_strrstr (key, ":"); + if (number != NULL) { - gchar *number = g_strrstr (key, ":"); - if (number != NULL) - { - gchar *endptr; + gchar *endptr; - number++; - index = g_ascii_strtoll (number, &endptr, 10); - if (index == 0 && endptr == (number)) - index = -1; - } + number++; + index = g_ascii_strtoll (number, &endptr, 10); + if (index == 0 && endptr == (number)) + index = -1; } } + } - suffix = g_strdup_printf (":%d", index); + suffix = g_strdup_printf (":%d", index); - g_variant_get (devices_variant, - "a{ss}", - &iter); + g_variant_get (devices_variant, + "a{ss}", + &iter); - while (g_variant_iter_next (iter, "{&s&s}", &key, &value)) + while (g_variant_iter_next (iter, "{&s&s}", &key, &value)) + { + if (g_str_has_suffix (key, suffix)) { - if (g_str_has_suffix (key, suffix)) + if (g_str_has_prefix (key, "device-id")) { - if (g_str_has_prefix (key, "device-id")) - { - device_id = g_strdup (value); - } + device_id = g_strdup (value); + } - if (g_str_has_prefix (key, "device-make-and-model")) - { - device_make_and_model = g_strdup (value); - } + if (g_str_has_prefix (key, "device-make-and-model")) + { + device_make_and_model = g_strdup (value); } } } @@ -3261,6 +3254,9 @@ get_cups_devices_async_dbus_cb (GObject *source_object, const gchar *ret_error; g_autoptr(GVariant) devices_variant = NULL; gboolean is_network_device; + g_autoptr(GVariantIter) iter = NULL; + const gchar *key, *value; + gint index = -1, max_index = -1, i; g_variant_get (output, "(&s@a{ss})", &ret_error, @@ -3271,66 +3267,59 @@ get_cups_devices_async_dbus_cb (GObject *source_object, g_warning ("cups-pk-helper: getting of CUPS devices failed: %s", ret_error); } - if (devices_variant) + g_variant_get (devices_variant, "a{ss}", &iter); + while (g_variant_iter_next (iter, "{&s&s}", &key, &value)) { - g_autoptr(GVariantIter) iter = NULL; - const gchar *key, *value; - gint index = -1, max_index = -1, i; - - g_variant_get (devices_variant, "a{ss}", &iter); - while (g_variant_iter_next (iter, "{&s&s}", &key, &value)) - { - index = get_suffix_index (key); - if (index > max_index) - max_index = index; - } + index = get_suffix_index (key); + if (index > max_index) + max_index = index; + } - if (max_index >= 0) - { - g_autoptr(GVariantIter) iter2 = NULL; + if (max_index >= 0) + { + g_autoptr(GVariantIter) iter2 = NULL; - num_of_devices = max_index + 1; - devices = g_new0 (PpPrintDevice *, num_of_devices); + num_of_devices = max_index + 1; + devices = g_new0 (PpPrintDevice *, num_of_devices); - g_variant_get (devices_variant, "a{ss}", &iter2); - while (g_variant_iter_next (iter2, "{&s&s}", &key, &value)) + g_variant_get (devices_variant, "a{ss}", &iter2); + while (g_variant_iter_next (iter2, "{&s&s}", &key, &value)) + { + index = get_suffix_index (key); + if (index >= 0) { - index = get_suffix_index (key); - if (index >= 0) - { - if (!devices[index]) - devices[index] = pp_print_device_new (); + if (!devices[index]) + devices[index] = pp_print_device_new (); - if (g_str_has_prefix (key, "device-class")) - { - is_network_device = g_strcmp0 (value, "network") == 0; - g_object_set (devices[index], "is-network-device", is_network_device, NULL); - } - else if (g_str_has_prefix (key, "device-id")) - g_object_set (devices[index], "device-id", value, NULL); - else if (g_str_has_prefix (key, "device-info")) - g_object_set (devices[index], "device-info", value, NULL); - else if (g_str_has_prefix (key, "device-make-and-model")) - { - g_object_set (devices[index], - "device-make-and-model", value, - "device-name", value, - NULL); - } - else if (g_str_has_prefix (key, "device-uri")) - g_object_set (devices[index], "device-uri", value, NULL); - else if (g_str_has_prefix (key, "device-location")) - g_object_set (devices[index], "device-location", value, NULL); - - g_object_set (devices[index], "acquisition-method", ACQUISITION_METHOD_DEFAULT_CUPS_SERVER, NULL); + if (g_str_has_prefix (key, "device-class")) + { + is_network_device = g_strcmp0 (value, "network") == 0; + g_object_set (devices[index], "is-network-device", is_network_device, NULL); } + else if (g_str_has_prefix (key, "device-id")) + g_object_set (devices[index], "device-id", value, NULL); + else if (g_str_has_prefix (key, "device-info")) + g_object_set (devices[index], "device-info", value, NULL); + else if (g_str_has_prefix (key, "device-make-and-model")) + { + g_object_set (devices[index], + "device-make-and-model", value, + "device-name", value, + NULL); + } + else if (g_str_has_prefix (key, "device-uri")) + g_object_set (devices[index], "device-uri", value, NULL); + else if (g_str_has_prefix (key, "device-location")) + g_object_set (devices[index], "device-location", value, NULL); + + g_object_set (devices[index], "acquisition-method", ACQUISITION_METHOD_DEFAULT_CUPS_SERVER, NULL); } + } - for (i = 0; i < num_of_devices; i++) - result = g_list_append (result, devices[i]); + for (i = 0; i < num_of_devices; i++) + result = g_list_append (result, devices[i]); - g_free (devices); - } + g_free (devices); } } else -- GitLab From 6b79e7ffa168f89ddae6c12840cb6fba55e6b2ba Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Thu, 21 Nov 2019 22:05:56 +1300 Subject: [PATCH 07/10] printers: Replace D-Bus unrefs with g_autoptr --- panels/printers/pp-utils.c | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/panels/printers/pp-utils.c b/panels/printers/pp-utils.c index c1e8b5c21a..19857a9429 100644 --- a/panels/printers/pp-utils.c +++ b/panels/printers/pp-utils.c @@ -536,7 +536,7 @@ gboolean printer_set_location (const gchar *printer_name, const gchar *location) { - GDBusConnection *bus; + g_autoptr(GDBusConnection) bus = NULL; g_autoptr(GVariant) output = NULL; gboolean result = FALSE; g_autoptr(GError) error = NULL; @@ -562,7 +562,6 @@ printer_set_location (const gchar *printer_name, -1, NULL, &error); - g_object_unref (bus); if (output) { @@ -587,7 +586,7 @@ printer_set_accepting_jobs (const gchar *printer_name, gboolean accepting_jobs, const gchar *reason) { - GDBusConnection *bus; + g_autoptr(GDBusConnection) bus = NULL; g_autoptr(GVariant) output = NULL; gboolean result = FALSE; g_autoptr(GError) error = NULL; @@ -616,7 +615,6 @@ printer_set_accepting_jobs (const gchar *printer_name, -1, NULL, &error); - g_object_unref (bus); if (output) { @@ -640,7 +638,7 @@ gboolean printer_set_enabled (const gchar *printer_name, gboolean enabled) { - GDBusConnection *bus; + g_autoptr(GDBusConnection) bus = NULL; g_autoptr(GVariant) output = NULL; gboolean result = FALSE; g_autoptr(GError) error = NULL; @@ -666,7 +664,6 @@ printer_set_enabled (const gchar *printer_name, -1, NULL, &error); - g_object_unref (bus); if (output) { @@ -689,7 +686,7 @@ printer_set_enabled (const gchar *printer_name, gboolean printer_delete (const gchar *printer_name) { - GDBusConnection *bus; + g_autoptr(GDBusConnection) bus = NULL; g_autoptr(GVariant) output = NULL; gboolean result = FALSE; g_autoptr(GError) error = NULL; @@ -715,7 +712,6 @@ printer_delete (const gchar *printer_name) -1, NULL, &error); - g_object_unref (bus); if (output) { @@ -738,7 +734,6 @@ printer_delete (const gchar *printer_name) gboolean printer_set_default (const gchar *printer_name) { - GDBusConnection *bus; const char *cups_server; gboolean result = FALSE; g_autoptr(GError) error = NULL; @@ -752,6 +747,8 @@ printer_set_default (const gchar *printer_name) g_ascii_strncasecmp (cups_server, "::1", 3) == 0 || cups_server[0] == '/') { + g_autoptr(GDBusConnection) bus = NULL; + /* Clean .cups/lpoptions before setting * default printer on local CUPS server. */ @@ -777,7 +774,6 @@ printer_set_default (const gchar *printer_name) -1, NULL, &error); - g_object_unref (bus); if (output) { @@ -810,7 +806,7 @@ gboolean printer_set_shared (const gchar *printer_name, gboolean shared) { - GDBusConnection *bus; + g_autoptr(GDBusConnection) bus = NULL; g_autoptr(GVariant) output = NULL; gboolean result = FALSE; g_autoptr(GError) error = NULL; @@ -836,7 +832,6 @@ printer_set_shared (const gchar *printer_name, -1, NULL, &error); - g_object_unref (bus); if (output) { @@ -861,7 +856,7 @@ printer_set_job_sheets (const gchar *printer_name, const gchar *start_sheet, const gchar *end_sheet) { - GDBusConnection *bus; + g_autoptr(GDBusConnection) bus = NULL; g_autoptr(GVariant) output = NULL; g_autoptr(GError) error = NULL; gboolean result = FALSE; @@ -887,7 +882,6 @@ printer_set_job_sheets (const gchar *printer_name, -1, NULL, &error); - g_object_unref (bus); if (output) { @@ -912,7 +906,7 @@ printer_set_policy (const gchar *printer_name, const gchar *policy, gboolean error_policy) { - GDBusConnection *bus; + g_autoptr(GDBusConnection) bus = NULL; g_autoptr(GVariant) output = NULL; gboolean result = FALSE; g_autoptr(GError) error = NULL; @@ -951,7 +945,6 @@ printer_set_policy (const gchar *printer_name, -1, NULL, &error); - g_object_unref (bus); if (output) { @@ -976,7 +969,7 @@ printer_set_users (const gchar *printer_name, gchar **users, gboolean allowed) { - GDBusConnection *bus; + g_autoptr(GDBusConnection) bus = NULL; GVariantBuilder array_builder; gint i; g_autoptr(GVariant) output = NULL; @@ -1021,7 +1014,6 @@ printer_set_users (const gchar *printer_name, -1, NULL, &error); - g_object_unref (bus); if (output) { @@ -1045,7 +1037,7 @@ gboolean class_add_printer (const gchar *class_name, const gchar *printer_name) { - GDBusConnection *bus; + g_autoptr(GDBusConnection) bus = NULL; g_autoptr(GVariant) output = NULL; gboolean result = FALSE; g_autoptr(GError) error = NULL; @@ -1071,7 +1063,6 @@ class_add_printer (const gchar *class_name, -1, NULL, &error); - g_object_unref (bus); if (output) { -- GitLab From e256dd57c3776e05296bd23a08571d421c729cbe Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Thu, 21 Nov 2019 22:06:40 +1300 Subject: [PATCH 08/10] printers: Replace D-Bus unrefs with g_autoptr This case could leak the GDBusConnection --- panels/printers/pp-utils.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/panels/printers/pp-utils.c b/panels/printers/pp-utils.c index 19857a9429..933a71f9e0 100644 --- a/panels/printers/pp-utils.c +++ b/panels/printers/pp-utils.c @@ -258,7 +258,7 @@ printer_rename (const gchar *old_name, cups_dest_t *dests = NULL; cups_dest_t *dest = NULL; cups_job_t *jobs = NULL; - GDBusConnection *bus; + g_autoptr(GDBusConnection) bus = NULL; const gchar *printer_location = NULL; const gchar *printer_info = NULL; const gchar *printer_uri = NULL; @@ -477,7 +477,6 @@ printer_rename (const gchar *old_name, -1, NULL, &add_error); - g_object_unref (bus); if (output) { -- GitLab From 02d001564ad8243644549cf39001b536bfd46bcd Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Thu, 21 Nov 2019 22:22:24 +1300 Subject: [PATCH 09/10] printers: Simplify D-Bus error case branches --- panels/printers/pp-utils.c | 275 +++++++++++++++++-------------------- 1 file changed, 127 insertions(+), 148 deletions(-) diff --git a/panels/printers/pp-utils.c b/panels/printers/pp-utils.c index 933a71f9e0..043091695e 100644 --- a/panels/printers/pp-utils.c +++ b/panels/printers/pp-utils.c @@ -537,8 +537,8 @@ printer_set_location (const gchar *printer_name, { g_autoptr(GDBusConnection) bus = NULL; g_autoptr(GVariant) output = NULL; - gboolean result = FALSE; - g_autoptr(GError) error = NULL; + const gchar *ret_error; + g_autoptr(GError) error = NULL; if (!printer_name || !location) return TRUE; @@ -562,22 +562,20 @@ printer_set_location (const gchar *printer_name, NULL, &error); - if (output) + if (output == NULL) { - const gchar *ret_error; - - g_variant_get (output, "(&s)", &ret_error); - if (ret_error[0] != '\0') - g_warning ("cups-pk-helper: setting of location for printer %s failed: %s", printer_name, ret_error); - else - result = TRUE; + g_warning ("%s", error->message); + return FALSE; } - else + + g_variant_get (output, "(&s)", &ret_error); + if (ret_error[0] != '\0') { - g_warning ("%s", error->message); + g_warning ("cups-pk-helper: setting of location for printer %s failed: %s", printer_name, ret_error); + return FALSE; } - return result; + return TRUE; } gboolean @@ -587,8 +585,8 @@ printer_set_accepting_jobs (const gchar *printer_name, { g_autoptr(GDBusConnection) bus = NULL; g_autoptr(GVariant) output = NULL; - gboolean result = FALSE; - g_autoptr(GError) error = NULL; + const gchar *ret_error; + g_autoptr(GError) error = NULL; if (!printer_name) return TRUE; @@ -615,22 +613,20 @@ printer_set_accepting_jobs (const gchar *printer_name, NULL, &error); - if (output) + if (output == NULL) { - const gchar *ret_error; - - g_variant_get (output, "(&s)", &ret_error); - if (ret_error[0] != '\0') - g_warning ("cups-pk-helper: setting of acceptance of jobs for printer %s failed: %s", printer_name, ret_error); - else - result = TRUE; + g_warning ("%s", error->message); + return FALSE; } - else + + g_variant_get (output, "(&s)", &ret_error); + if (ret_error[0] != '\0') { - g_warning ("%s", error->message); + g_warning ("cups-pk-helper: setting of acceptance of jobs for printer %s failed: %s", printer_name, ret_error); + return FALSE; } - return result; + return TRUE; } gboolean @@ -639,8 +635,8 @@ printer_set_enabled (const gchar *printer_name, { g_autoptr(GDBusConnection) bus = NULL; g_autoptr(GVariant) output = NULL; - gboolean result = FALSE; - g_autoptr(GError) error = NULL; + const gchar *ret_error; + g_autoptr(GError) error = NULL; if (!printer_name) return TRUE; @@ -664,22 +660,20 @@ printer_set_enabled (const gchar *printer_name, NULL, &error); - if (output) + if (output == NULL) { - const gchar *ret_error; - - g_variant_get (output, "(&s)", &ret_error); - if (ret_error[0] != '\0') - g_warning ("cups-pk-helper: setting of enablement of printer %s failed: %s", printer_name, ret_error); - else - result = TRUE; + g_warning ("%s", error->message); + return FALSE; } - else + + g_variant_get (output, "(&s)", &ret_error); + if (ret_error[0] != '\0') { - g_warning ("%s", error->message); + g_warning ("cups-pk-helper: setting of enablement of printer %s failed: %s", printer_name, ret_error); + return FALSE; } - return result; + return TRUE; } gboolean @@ -687,8 +681,8 @@ printer_delete (const gchar *printer_name) { g_autoptr(GDBusConnection) bus = NULL; g_autoptr(GVariant) output = NULL; - gboolean result = FALSE; - g_autoptr(GError) error = NULL; + const gchar *ret_error; + g_autoptr(GError) error = NULL; if (!printer_name) return TRUE; @@ -712,29 +706,26 @@ printer_delete (const gchar *printer_name) NULL, &error); - if (output) + if (output == NULL) { - const gchar *ret_error; - - g_variant_get (output, "(&s)", &ret_error); - if (ret_error[0] != '\0') - g_warning ("cups-pk-helper: removing of printer %s failed: %s", printer_name, ret_error); - else - result = TRUE; + g_warning ("%s", error->message); + return FALSE; } - else + + g_variant_get (output, "(&s)", &ret_error); + if (ret_error[0] != '\0') { - g_warning ("%s", error->message); + g_warning ("cups-pk-helper: removing of printer %s failed: %s", printer_name, ret_error); + return FALSE; } - return result; + return TRUE; } gboolean printer_set_default (const gchar *printer_name) { - const char *cups_server; - gboolean result = FALSE; + const char *cups_server; g_autoptr(GError) error = NULL; if (!printer_name) @@ -747,6 +738,8 @@ printer_set_default (const gchar *printer_name) cups_server[0] == '/') { g_autoptr(GDBusConnection) bus = NULL; + g_autoptr(GVariant) output = NULL; + const gchar *ret_error; /* Clean .cups/lpoptions before setting * default printer on local CUPS server. @@ -757,38 +750,35 @@ printer_set_default (const gchar *printer_name) if (!bus) { g_warning ("Failed to get system bus: %s", error->message); + return FALSE; } - else - { - g_autoptr(GVariant) output = NULL; - output = g_dbus_connection_call_sync (bus, - MECHANISM_BUS, - "/", - MECHANISM_BUS, - "PrinterSetDefault", - g_variant_new ("(s)", printer_name), - G_VARIANT_TYPE ("(s)"), - G_DBUS_CALL_FLAGS_NONE, - -1, - NULL, - &error); - - if (output) - { - const gchar *ret_error; + output = g_dbus_connection_call_sync (bus, + MECHANISM_BUS, + "/", + MECHANISM_BUS, + "PrinterSetDefault", + g_variant_new ("(s)", printer_name), + G_VARIANT_TYPE ("(s)"), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + + if (output == NULL) + { + g_warning ("%s", error->message); + return FALSE; + } - g_variant_get (output, "(&s)", &ret_error); - if (ret_error[0] != '\0') - g_warning ("cups-pk-helper: setting default printer to %s failed: %s", printer_name, ret_error); - else - result = TRUE; - } - else - { - g_warning ("%s", error->message); - } + g_variant_get (output, "(&s)", &ret_error); + if (ret_error[0] != '\0') + { + g_warning ("cups-pk-helper: setting default printer to %s failed: %s", printer_name, ret_error); + return FALSE; } + + return TRUE; } else /* Store default printer to .cups/lpoptions @@ -796,9 +786,8 @@ printer_set_default (const gchar *printer_name) */ { set_local_default_printer (printer_name); + return TRUE; } - - return result; } gboolean @@ -807,8 +796,8 @@ printer_set_shared (const gchar *printer_name, { g_autoptr(GDBusConnection) bus = NULL; g_autoptr(GVariant) output = NULL; - gboolean result = FALSE; - g_autoptr(GError) error = NULL; + const gchar *ret_error; + g_autoptr(GError) error = NULL; if (!printer_name) return TRUE; @@ -832,22 +821,20 @@ printer_set_shared (const gchar *printer_name, NULL, &error); - if (output) + if (output == NULL) { - const gchar *ret_error; - - g_variant_get (output, "(&s)", &ret_error); - if (ret_error[0] != '\0') - g_warning ("cups-pk-helper: setting of sharing of printer %s failed: %s", printer_name, ret_error); - else - result = TRUE; + g_warning ("%s", error->message); + return FALSE; } - else + + g_variant_get (output, "(&s)", &ret_error); + if (ret_error[0] != '\0') { - g_warning ("%s", error->message); + g_warning ("cups-pk-helper: setting of sharing of printer %s failed: %s", printer_name, ret_error); + return FALSE; } - return result; + return TRUE; } gboolean @@ -857,8 +844,8 @@ printer_set_job_sheets (const gchar *printer_name, { g_autoptr(GDBusConnection) bus = NULL; g_autoptr(GVariant) output = NULL; - g_autoptr(GError) error = NULL; - gboolean result = FALSE; + const gchar *ret_error; + g_autoptr(GError) error = NULL; if (!printer_name || !start_sheet || !end_sheet) return TRUE; @@ -882,22 +869,20 @@ printer_set_job_sheets (const gchar *printer_name, NULL, &error); - if (output) + if (output == NULL) { - const gchar *ret_error; - - g_variant_get (output, "(&s)", &ret_error); - if (ret_error[0] != '\0') - g_warning ("cups-pk-helper: setting of job sheets for printer %s failed: %s", printer_name, ret_error); - else - result = TRUE; + g_warning ("%s", error->message); + return FALSE; } - else + + g_variant_get (output, "(&s)", &ret_error); + if (ret_error[0] != '\0') { - g_warning ("%s", error->message); + g_warning ("cups-pk-helper: setting of job sheets for printer %s failed: %s", printer_name, ret_error); + return FALSE; } - return result; + return TRUE; } gboolean @@ -907,8 +892,8 @@ printer_set_policy (const gchar *printer_name, { g_autoptr(GDBusConnection) bus = NULL; g_autoptr(GVariant) output = NULL; - gboolean result = FALSE; - g_autoptr(GError) error = NULL; + const gchar *ret_error; + g_autoptr(GError) error = NULL; if (!printer_name || !policy) return TRUE; @@ -945,22 +930,20 @@ printer_set_policy (const gchar *printer_name, NULL, &error); - if (output) + if (output == NULL) { - const gchar *ret_error; - - g_variant_get (output, "(&s)", &ret_error); - if (ret_error[0] != '\0') - g_warning ("cups-pk-helper: setting of a policy for printer %s failed: %s", printer_name, ret_error); - else - result = TRUE; + g_warning ("%s", error->message); + return FALSE; } - else + + g_variant_get (output, "(&s)", &ret_error); + if (ret_error[0] != '\0') { - g_warning ("%s", error->message); + g_warning ("cups-pk-helper: setting of a policy for printer %s failed: %s", printer_name, ret_error); + return FALSE; } - return result; + return TRUE; } gboolean @@ -972,7 +955,7 @@ printer_set_users (const gchar *printer_name, GVariantBuilder array_builder; gint i; g_autoptr(GVariant) output = NULL; - gboolean result = FALSE; + const gchar *ret_error; g_autoptr(GError) error = NULL; if (!printer_name || !users) @@ -1014,22 +997,20 @@ printer_set_users (const gchar *printer_name, NULL, &error); - if (output) + if (output == NULL) { - const gchar *ret_error; - - g_variant_get (output, "(&s)", &ret_error); - if (ret_error[0] != '\0') - g_warning ("cups-pk-helper: setting of access list for printer %s failed: %s", printer_name, ret_error); - else - result = TRUE; + g_warning ("%s", error->message); + return FALSE; } - else + + g_variant_get (output, "(&s)", &ret_error); + if (ret_error[0] != '\0') { - g_warning ("%s", error->message); + g_warning ("cups-pk-helper: setting of access list for printer %s failed: %s", printer_name, ret_error); + return FALSE; } - return result; + return TRUE; } gboolean @@ -1038,8 +1019,8 @@ class_add_printer (const gchar *class_name, { g_autoptr(GDBusConnection) bus = NULL; g_autoptr(GVariant) output = NULL; - gboolean result = FALSE; - g_autoptr(GError) error = NULL; + const gchar *ret_error; + g_autoptr(GError) error = NULL; if (!class_name || !printer_name) return TRUE; @@ -1063,22 +1044,20 @@ class_add_printer (const gchar *class_name, NULL, &error); - if (output) + if (output == NULL) { - const gchar *ret_error; - - g_variant_get (output, "(&s)", &ret_error); - if (ret_error[0] != '\0') - g_warning ("cups-pk-helper: adding of printer %s to class %s failed: %s", printer_name, class_name, ret_error); - else - result = TRUE; + g_warning ("%s", error->message); + return FALSE; } - else + + g_variant_get (output, "(&s)", &ret_error); + if (ret_error[0] != '\0') { - g_warning ("%s", error->message); + g_warning ("cups-pk-helper: adding of printer %s to class %s failed: %s", printer_name, class_name, ret_error); + return FALSE; } - return result; + return TRUE; } gboolean -- GitLab From fe91d17b43368caf0ee54a8e7cdbab06045423fa Mon Sep 17 00:00:00 2001 From: Robert Ancell Date: Thu, 21 Nov 2019 22:26:59 +1300 Subject: [PATCH 10/10] printers: Remove nesting on a GVariant iteration --- panels/printers/pp-new-printer.c | 60 +++++++++++++++++--------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/panels/printers/pp-new-printer.c b/panels/printers/pp-new-printer.c index a44c6bf1be..1c325b46ed 100644 --- a/panels/printers/pp-new-printer.c +++ b/panels/printers/pp-new-printer.c @@ -466,7 +466,7 @@ printer_add_real_async (PpNewPrinter *self) static PPDName * get_ppd_item_from_output (GVariant *output) { - PPDName *ppd_item = NULL; + g_autoptr(GVariant) array = NULL; gint j; static const char * const match_levels[] = { "exact-cmd", @@ -475,40 +475,42 @@ get_ppd_item_from_output (GVariant *output) "generic", "none"}; - if (output) + if (output == NULL) + return NULL; + + g_variant_get (output, "(@a(ss))", &array); + for (j = 0; j < G_N_ELEMENTS (match_levels); j++) { - g_autoptr(GVariant) array = NULL; + g_autoptr(GVariantIter) iter = NULL; + const gchar *driver, *match; - g_variant_get (output, "(@a(ss))", &array); - for (j = 0; j < G_N_ELEMENTS (match_levels) && !ppd_item; j++) + g_variant_get (array, "a(ss)", &iter); + while (g_variant_iter_next (iter, "(&s&s)", &driver, &match)) { - g_autoptr(GVariantIter) iter = NULL; - const gchar *driver, *match; - - g_variant_get (array, "a(ss)", &iter); - while (g_variant_iter_next (iter, "(&s&s)", &driver, &match) && !ppd_item) - { - if (g_str_equal (match, match_levels[j])) - { - ppd_item = g_new0 (PPDName, 1); - ppd_item->ppd_name = g_strdup (driver); - - if (g_strcmp0 (match, "exact-cmd") == 0) - ppd_item->ppd_match_level = PPD_EXACT_CMD_MATCH; - else if (g_strcmp0 (match, "exact") == 0) - ppd_item->ppd_match_level = PPD_EXACT_MATCH; - else if (g_strcmp0 (match, "close") == 0) - ppd_item->ppd_match_level = PPD_CLOSE_MATCH; - else if (g_strcmp0 (match, "generic") == 0) - ppd_item->ppd_match_level = PPD_GENERIC_MATCH; - else if (g_strcmp0 (match, "none") == 0) - ppd_item->ppd_match_level = PPD_NO_MATCH; - } - } + PPDName *ppd_item; + + if (!g_str_equal (match, match_levels[j])) + continue; + + ppd_item = g_new0 (PPDName, 1); + ppd_item->ppd_name = g_strdup (driver); + + if (g_strcmp0 (match, "exact-cmd") == 0) + ppd_item->ppd_match_level = PPD_EXACT_CMD_MATCH; + else if (g_strcmp0 (match, "exact") == 0) + ppd_item->ppd_match_level = PPD_EXACT_MATCH; + else if (g_strcmp0 (match, "close") == 0) + ppd_item->ppd_match_level = PPD_CLOSE_MATCH; + else if (g_strcmp0 (match, "generic") == 0) + ppd_item->ppd_match_level = PPD_GENERIC_MATCH; + else if (g_strcmp0 (match, "none") == 0) + ppd_item->ppd_match_level = PPD_NO_MATCH; + + return ppd_item; } } - return ppd_item; + return NULL; } -- GitLab