summaryrefslogtreecommitdiff
path: root/gtk/gtkprintbackend.c
diff options
context:
space:
mode:
authorEmmanuele Bassi <ebassi@gnome.org>2015-07-16 16:12:35 +0100
committerEmmanuele Bassi <ebassi@gnome.org>2015-07-16 16:19:55 +0100
commite259b2f30d86fdde7dab27a25e8088d36421acc7 (patch)
tree7c3499bb6a4f9ecf237fe64cdc9f53efacc67c78 /gtk/gtkprintbackend.c
parent3b41daca780e9e83d04dcad43e2fbd5a2ab8c120 (diff)
downloadgtk+-e259b2f30d86fdde7dab27a25e8088d36421acc7.tar.gz
Avoid O(n²) walking of string arrays
"Yo, we heard you like traversing NULL-terminated arrays to operate on them, so we called g_strv_length() as the for condition, so you can iterate the array while iterating the array." Instead of making famed rapper and television producer Xzibit proud, we should avoid calling g_strv_length() on an array while looping on the array, to avoid quadratic complexity. We do this in various places that deal with arrays of strings that we cannot really guess are short enough not to matter — e.g. the list of CSS selectors in the inspector, or the required authentication information for printing.
Diffstat (limited to 'gtk/gtkprintbackend.c')
-rw-r--r--gtk/gtkprintbackend.c25
1 files changed, 14 insertions, 11 deletions
diff --git a/gtk/gtkprintbackend.c b/gtk/gtkprintbackend.c
index cda9376007..8ec6dbc99b 100644
--- a/gtk/gtkprintbackend.c
+++ b/gtk/gtkprintbackend.c
@@ -706,24 +706,27 @@ password_dialog_response (GtkWidget *dialog,
GtkPrintBackend *backend)
{
GtkPrintBackendPrivate *priv = backend->priv;
- gint i;
+ gint i, auth_info_len;
if (response_id == GTK_RESPONSE_OK)
gtk_print_backend_set_password (backend, priv->auth_info_required, priv->auth_info, priv->store_auth_info);
else
gtk_print_backend_set_password (backend, priv->auth_info_required, NULL, FALSE);
- for (i = 0; i < g_strv_length (priv->auth_info_required); i++)
- if (priv->auth_info[i] != NULL)
- {
- memset (priv->auth_info[i], 0, strlen (priv->auth_info[i]));
- g_free (priv->auth_info[i]);
- priv->auth_info[i] = NULL;
- }
- g_free (priv->auth_info);
- priv->auth_info = NULL;
+ /* We want to clear the data before freeing it */
+ auth_info_len = g_strv_length (priv->auth_info_required);
+ for (i = 0; i < auth_info_len; i++)
+ {
+ if (priv->auth_info[i] != NULL)
+ {
+ memset (priv->auth_info[i], 0, strlen (priv->auth_info[i]));
+ g_free (priv->auth_info[i]);
+ priv->auth_info[i] = NULL;
+ }
+ }
- g_strfreev (priv->auth_info_required);
+ g_clear_pointer (&priv->auth_info, g_free);
+ g_clear_pointer (&priv->auth_info_required, g_strfreev);
gtk_widget_destroy (dialog);