[libgepub] libgepub: Add guards to public functions



commit 0da0ba35e21cc6a09149d7bf827576fbfd2556e6
Author: Bastien Nocera <hadess hadess net>
Date:   Thu Mar 30 12:53:08 2017 +0200

    libgepub: Add guards to public functions
    
    So as to catch incorrect usage of the APIs as soon as possible.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=780707

 libgepub/gepub-doc.c    |   79 ++++++++++++++++++++++++++++++++++++++++++----
 libgepub/gepub-widget.c |    4 ++
 2 files changed, 76 insertions(+), 7 deletions(-)
---
diff --git a/libgepub/gepub-doc.c b/libgepub/gepub-doc.c
index 2273aef..b2c2d80 100644
--- a/libgepub/gepub-doc.c
+++ b/libgepub/gepub-doc.c
@@ -311,6 +311,8 @@ gepub_doc_fill_spine (GepubDoc *doc)
 GBytes *
 gepub_doc_get_content (GepubDoc *doc)
 {
+    g_return_val_if_fail (GEPUB_IS_DOC (doc), NULL);
+
     return doc->content;
 }
 
@@ -333,6 +335,9 @@ gepub_doc_get_metadata (GepubDoc *doc, const gchar *mdata)
     const char *data;
     gsize size;
 
+    g_return_val_if_fail (GEPUB_IS_DOC (doc), NULL);
+    g_return_val_if_fail (mdata != NULL, NULL);
+
     data = g_bytes_get_data (doc->content, &size);
     xdoc = xmlRecoverMemory (data, size);
     root_element = xmlDocGetRootElement (xdoc);
@@ -357,6 +362,8 @@ gepub_doc_get_metadata (GepubDoc *doc, const gchar *mdata)
 GHashTable *
 gepub_doc_get_resources (GepubDoc *doc)
 {
+    g_return_val_if_fail (GEPUB_IS_DOC (doc), NULL);
+
     return doc->resources;
 }
 
@@ -370,7 +377,12 @@ gepub_doc_get_resources (GepubDoc *doc)
 GBytes *
 gepub_doc_get_resource_by_id (GepubDoc *doc, const gchar *id)
 {
-    GepubResource *gres = g_hash_table_lookup (doc->resources, id);
+    GepubResource *gres;
+
+    g_return_val_if_fail (GEPUB_IS_DOC (doc), NULL);
+    g_return_val_if_fail (id != NULL, NULL);
+
+    gres = g_hash_table_lookup (doc->resources, id);
     if (!gres) {
         // not found
         return NULL;
@@ -389,6 +401,9 @@ gepub_doc_get_resource_by_id (GepubDoc *doc, const gchar *id)
 GBytes *
 gepub_doc_get_resource (GepubDoc *doc, const gchar *path)
 {
+    g_return_val_if_fail (GEPUB_IS_DOC (doc), NULL);
+    g_return_val_if_fail (path != NULL, NULL);
+
     return gepub_archive_read_entry (doc->archive, path);
 }
 
@@ -404,6 +419,7 @@ gepub_doc_get_resource_mime_by_id (GepubDoc *doc, const gchar *id)
 {
     GepubResource *gres;
 
+    g_return_val_if_fail (GEPUB_IS_DOC (doc), NULL);
     g_return_val_if_fail (id != NULL, NULL);
 
     gres = g_hash_table_lookup (doc->resources, id);
@@ -426,7 +442,12 @@ gchar *
 gepub_doc_get_resource_mime (GepubDoc *doc, const gchar *path)
 {
     GepubResource *gres;
-    GList *keys = g_hash_table_get_keys (doc->resources);
+    GList *keys;
+
+    g_return_val_if_fail (GEPUB_IS_DOC (doc), NULL);
+    g_return_val_if_fail (path != NULL, NULL);
+
+    keys = g_hash_table_get_keys (doc->resources);
 
     while (keys) {
         gres = ((GepubResource*)g_hash_table_lookup (doc->resources, keys->data));
@@ -450,6 +471,9 @@ gepub_doc_get_resource_mime (GepubDoc *doc, const gchar *path)
 gchar *
 gepub_doc_get_current_mime (GepubDoc *doc)
 {
+    g_return_val_if_fail (GEPUB_IS_DOC (doc), NULL);
+    g_return_val_if_fail (doc->page != NULL, NULL);
+
     return gepub_doc_get_resource_mime_by_id (doc, doc->page->data);
 }
 
@@ -462,6 +486,9 @@ gepub_doc_get_current_mime (GepubDoc *doc)
 GBytes *
 gepub_doc_get_current (GepubDoc *doc)
 {
+    g_return_val_if_fail (GEPUB_IS_DOC (doc), NULL);
+    g_return_val_if_fail (doc->page != NULL, NULL);
+
     return gepub_doc_get_resource_by_id (doc, doc->page->data);
 }
 
@@ -476,12 +503,17 @@ gepub_doc_get_current (GepubDoc *doc)
 GBytes *
 gepub_doc_get_current_with_epub_uris (GepubDoc *doc)
 {
-    GBytes *content = gepub_doc_get_current (doc);
-    gchar *path = gepub_doc_get_current_path (doc);
+    GBytes *content, *replaced;
+    gchar *path, *base;
+
+    g_return_val_if_fail (GEPUB_IS_DOC (doc), NULL);
+
+    content = gepub_doc_get_current (doc);
+    path = gepub_doc_get_current_path (doc);
     // getting the basepath of the current xhtml loaded
-    gchar *base = g_path_get_dirname (path);
+    base = g_path_get_dirname (path);
 
-    GBytes *replaced = gepub_utils_replace_resources (content, base);
+    replaced = gepub_utils_replace_resources (content, base);
 
     g_free (path);
     g_bytes_unref (content);
@@ -506,6 +538,8 @@ gepub_doc_get_text (GepubDoc *doc)
 
     GList *texts = NULL;
 
+    g_return_val_if_fail (GEPUB_IS_DOC (doc), NULL);
+
     current = gepub_doc_get_current (doc);
     if (!current) {
         return NULL;
@@ -539,6 +573,9 @@ gepub_doc_get_text_by_id (GepubDoc *doc, const gchar *id)
 
     GList *texts = NULL;
 
+    g_return_val_if_fail (GEPUB_IS_DOC (doc), NULL);
+    g_return_val_if_fail (id != NULL, NULL);
+
     contents = gepub_doc_get_resource_by_id (doc, id);
     if (!res) {
         return NULL;
@@ -577,6 +614,9 @@ gepub_doc_set_page_internal (GepubDoc *doc,
 gboolean
 gepub_doc_go_next (GepubDoc *doc)
 {
+    g_return_val_if_fail (GEPUB_IS_DOC (doc), FALSE);
+    g_return_val_if_fail (doc->page != NULL, FALSE);
+
     return gepub_doc_set_page_internal (doc, doc->page->next);
 }
 
@@ -589,6 +629,9 @@ gepub_doc_go_next (GepubDoc *doc)
 gboolean
 gepub_doc_go_prev (GepubDoc *doc)
 {
+    g_return_val_if_fail (GEPUB_IS_DOC (doc), FALSE);
+    g_return_val_if_fail (doc->page != NULL, FALSE);
+
     return gepub_doc_set_page_internal (doc, doc->page->prev);
 }
 
@@ -601,6 +644,8 @@ gepub_doc_go_prev (GepubDoc *doc)
 int
 gepub_doc_get_n_pages (GepubDoc *doc)
 {
+    g_return_val_if_fail (GEPUB_IS_DOC (doc), 0);
+
     return g_list_length (doc->spine);
 }
 
@@ -613,6 +658,10 @@ gepub_doc_get_n_pages (GepubDoc *doc)
 int
 gepub_doc_get_page (GepubDoc *doc)
 {
+    g_return_val_if_fail (GEPUB_IS_DOC (doc), 0);
+    g_return_val_if_fail (doc->spine != NULL, 0);
+    g_return_val_if_fail (doc->page != NULL, 0);
+
     return g_list_position (doc->spine, doc->page);
 }
 
@@ -629,6 +678,8 @@ gepub_doc_set_page (GepubDoc *doc,
 {
     GList *page;
 
+    g_return_if_fail (GEPUB_IS_DOC (doc));
+
     g_return_if_fail (index >= 0 && index <= gepub_doc_get_n_pages (doc));
 
     page = g_list_nth (doc->spine, index);
@@ -653,6 +704,9 @@ gepub_doc_get_cover (GepubDoc *doc)
     const char *data;
     gsize size;
 
+    g_return_val_if_fail (GEPUB_IS_DOC (doc), NULL);
+    g_return_val_if_fail (doc->content != NULL, NULL);
+
     data = g_bytes_get_data (doc->content, &size);
     xdoc = xmlRecoverMemory (data, size);
     root_element = xmlDocGetRootElement (xdoc);
@@ -677,7 +731,12 @@ gepub_doc_get_cover (GepubDoc *doc)
 gchar *
 gepub_doc_get_resource_path (GepubDoc *doc, const gchar *id)
 {
-    GepubResource *gres = g_hash_table_lookup (doc->resources, id);
+    GepubResource *gres;
+
+    g_return_val_if_fail (GEPUB_IS_DOC (doc), NULL);
+    g_return_val_if_fail (id != NULL, NULL);
+
+    gres = g_hash_table_lookup (doc->resources, id);
     if (!gres) {
         // not found
         return NULL;
@@ -695,6 +754,9 @@ gepub_doc_get_resource_path (GepubDoc *doc, const gchar *id)
 gchar *
 gepub_doc_get_current_path (GepubDoc *doc)
 {
+    g_return_val_if_fail (GEPUB_IS_DOC (doc), NULL);
+    g_return_val_if_fail (doc->page != NULL, NULL);
+
     return gepub_doc_get_resource_path (doc, doc->page->data);
 }
 
@@ -708,5 +770,8 @@ gepub_doc_get_current_path (GepubDoc *doc)
 const gchar *
 gepub_doc_get_current_id (GepubDoc *doc)
 {
+    g_return_val_if_fail (GEPUB_IS_DOC (doc), NULL);
+    g_return_val_if_fail (doc->page != NULL, NULL);
+
     return doc->page->data;
 }
diff --git a/libgepub/gepub-widget.c b/libgepub/gepub-widget.c
index 7e7a239..149b3dc 100644
--- a/libgepub/gepub-widget.c
+++ b/libgepub/gepub-widget.c
@@ -180,6 +180,8 @@ gepub_widget_new (void)
 GepubDoc *
 gepub_widget_get_doc (GepubWidget *widget)
 {
+    g_return_val_if_fail (GEPUB_IS_WIDGET (widget), NULL);
+
     return widget->doc;
 }
 
@@ -208,6 +210,8 @@ void
 gepub_widget_set_doc (GepubWidget *widget,
                       GepubDoc    *doc)
 {
+    g_return_if_fail (GEPUB_IS_WIDGET (widget));
+
     if (widget->doc == doc)
         return;
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]