[devhelp] Don't crash with empty .devhelp* files



commit 0ad2461ede6002eab14b3becfd0bb4e5e00a3be1
Author: Debarshi Ray <debarshir gnome org>
Date:   Tue Jan 6 16:51:54 2015 +0100

    Don't crash with empty .devhelp* files
    
    Call g_markup_parse_context_end_parse after we are done reading the
    file to catch empty files among other things. This is the only method
    that raises G_MARKUP_ERROR_EMPTY, so it is particularly necessary for
    this case.
    
    It was necessary to remove parser_error_cb because we should not be
    freeing the context inside it and dh_parser_free would have done it
    for us anyway. The error callback is called from set_error_literal
    inside g_markup_parse_context_end_parse, and invalidating the context
    can lead to bad things.
    
    Construction of a DhBook instance can fail due to parsing errors, so
    be careful about that.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=742447

 src/dh-book-manager.c |    2 ++
 src/dh-parser.c       |   17 +++++------------
 2 files changed, 7 insertions(+), 12 deletions(-)
---
diff --git a/src/dh-book-manager.c b/src/dh-book-manager.c
index a8e1ad2..51110d2 100644
--- a/src/dh-book-manager.c
+++ b/src/dh-book-manager.c
@@ -648,6 +648,8 @@ book_manager_add_from_filepath (DhBookManager *book_manager,
 
         /* Allocate new book struct */
         book = dh_book_new (book_path);
+        if (book == NULL)
+                return;
 
         /* Check if book with same path was already loaded in the manager */
         if (g_list_find_custom (priv->books,
diff --git a/src/dh-parser.c b/src/dh-parser.c
index 35d3179..469da8e 100644
--- a/src/dh-parser.c
+++ b/src/dh-parser.c
@@ -468,17 +468,6 @@ parser_end_node_cb (GMarkupParseContext  *context,
        }
 }
 
-static void
-parser_error_cb (GMarkupParseContext *context,
-                GError              *error,
-                gpointer             user_data)
-{
-       DhParser *parser = user_data;
-
-       g_markup_parse_context_free (parser->context);
-       parser->context = NULL;
-}
-
 static gboolean
 parser_read_gz_file (DhParser     *parser,
                      const gchar  *path,
@@ -565,7 +554,6 @@ dh_parser_read_file (const gchar  *path,
 
        parser->m_parser->start_element = parser_start_node_cb;
        parser->m_parser->end_element = parser_end_node_cb;
-       parser->m_parser->error = parser_error_cb;
 
        parser->context = g_markup_parse_context_new (parser->m_parser, 0,
                                                      parser, NULL);
@@ -614,6 +602,11 @@ dh_parser_read_file (const gchar  *path,
                                 goto exit;
                         }
                 }
+
+                if (!g_markup_parse_context_end_parse (parser->context, error)) {
+                        result = FALSE;
+                        goto exit;
+                }
         }
 
  exit:


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