[devhelp/wip/swilmet/misc: 4/13] window: improve find_library_equivalent()
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [devhelp/wip/swilmet/misc: 4/13] window: improve find_library_equivalent()
- Date: Sun, 15 Nov 2015 18:01:07 +0000 (UTC)
commit 9a12c7c918c93a7d4a492f1b76b3c5ff3ec6a4e7
Author: Sébastien Wilmet <swilmet gnome org>
Date: Sun Nov 15 16:31:30 2015 +0100
window: improve find_library_equivalent()
- Prevent array out-of-boundary access
- Fix memory leak
- Use g_str_has_prefix()
- Improve variable names (it's important with nested loops)
https://bugzilla.gnome.org/show_bug.cgi?id=758141
src/dh-window.c | 48 ++++++++++++++++++++++++++----------------------
1 files changed, 26 insertions(+), 22 deletions(-)
---
diff --git a/src/dh-window.c b/src/dh-window.c
index bfc597c..cfe06ee 100644
--- a/src/dh-window.c
+++ b/src/dh-window.c
@@ -821,46 +821,50 @@ find_library_equivalent (DhWindow *window,
const gchar *uri)
{
gchar **components;
- GList *iter;
- DhLink *link;
- DhBookManager *book_manager;
+ guint n_components;
gchar *book_id;
gchar *filename;
- gchar *local_uri = NULL;
+ DhBookManager *book_manager;
GList *books;
+ gchar *local_uri = NULL;
components = g_strsplit (uri, "/", 0);
- if (strncmp (uri, "http://library.gnome.org/devel/", 31) == 0 ||
- strncmp (uri, "https://library.gnome.org/devel/", 32) == 0) {
+ n_components = g_strv_length (components);
+ if ((g_str_has_prefix (uri, "http://library.gnome.org/devel/") ||
+ g_str_has_prefix (uri, "https://library.gnome.org/devel/")) &&
+ n_components >= 7) {
book_id = components[4];
filename = components[6];
- } else if (strncmp (uri, "http://developer.gnome.org/", 27 ) == 0 ||
- strncmp (uri, "https://developer.gnome.org/", 28 ) == 0) {
+ } else if ((g_str_has_prefix (uri, "http://developer.gnome.org/") ||
+ g_str_has_prefix (uri, "https://developer.gnome.org/")) &&
+ n_components >= 6) {
book_id = components[3];
filename = components[5];
} else {
+ g_strfreev (components);
return NULL;
}
book_manager = dh_app_peek_book_manager (DH_APP (gtk_window_get_application (GTK_WINDOW (window))));
- /* use list pointer to iterate */
for (books = dh_book_manager_get_books (book_manager);
- !local_uri && books;
- books = g_list_next (books)) {
- DhBook *book = DH_BOOK (books->data);
-
- for (iter = dh_book_get_keywords (book);
- iter;
- iter = g_list_next (iter)) {
- link = iter->data;
- if (g_strcmp0 (dh_link_get_book_id (link), book_id) != 0) {
+ local_uri == NULL && books != NULL;
+ books = books->next) {
+ DhBook *cur_book = DH_BOOK (books->data);
+ GList *keywords;
+
+ for (keywords = dh_book_get_keywords (cur_book);
+ keywords != NULL;
+ keywords = keywords->next) {
+ DhLink *cur_link = keywords->data;
+
+ if (g_strcmp0 (dh_link_get_book_id (cur_link), book_id) != 0)
continue;
- }
- if (g_strcmp0 (dh_link_get_file_name (link), filename) != 0) {
+
+ if (g_strcmp0 (dh_link_get_file_name (cur_link), filename) != 0)
continue;
- }
- local_uri = dh_link_get_uri (link);
+
+ local_uri = dh_link_get_uri (cur_link);
break;
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]