From e259b2f30d86fdde7dab27a25e8088d36421acc7 Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Thu, 16 Jul 2015 16:12:35 +0100 Subject: =?UTF-8?q?Avoid=20O(n=C2=B2)=20walking=20of=20string=20arrays?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "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. --- modules/printbackends/cups/gtkcupssecretsutils.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'modules/printbackends') diff --git a/modules/printbackends/cups/gtkcupssecretsutils.c b/modules/printbackends/cups/gtkcupssecretsutils.c index d5df4a7326..925f7d561c 100644 --- a/modules/printbackends/cups/gtkcupssecretsutils.c +++ b/modules/printbackends/cups/gtkcupssecretsutils.c @@ -109,7 +109,7 @@ get_secret_cb (GObject *source_object, *key = NULL, *value = NULL; GVariantIter *iter = NULL; - guint i; + guint i, required_len; gint pw_field = -1; task = user_data; @@ -228,7 +228,8 @@ get_secret_cb (GObject *source_object, fail: /* Error out */ GTK_NOTE (PRINTING, g_print ("Failed to lookup secret.\n")); - for (i = 0; i < g_strv_length (task_data->auth_info_required); i++) + required_len = g_strv_length (task_data->auth_info_required); + for (i = 0; i < required_len; i++) { /* Not all fields of auth_info are neccessarily written so we can not use strfreev here */ -- cgit v1.2.1