gbookmarkfile: Fix error reporting with unexpected nesting of elements

The parser state machine for bookmark files did not handle unexpected
nesting of elements, such as a <bookmark> element inside a <title>
element — it would print a critical warning rather than returning a
GError.

Fix that, and add various unit tests for it. The set of tests includes
various other general markup tests as provided by Jussi Judin.

Signed-off-by: Philip Withnall <withnall@endlessm.com>

https://gitlab.gnome.org/GNOME/glib/issues/1035
This commit is contained in:
Philip Withnall 2018-07-30 19:10:11 +01:00
parent 4127c84c3a
commit 376234f9b2
23 changed files with 98 additions and 4 deletions

View File

@ -210,7 +210,7 @@ struct _GBookmarkFile
};
/* parser state machine */
enum
typedef enum
{
STATE_STARTED = 0,
@ -228,7 +228,7 @@ enum
STATE_ICON,
STATE_FINISHED
};
} ParserState;
static void g_bookmark_file_init (GBookmarkFile *bookmark);
static void g_bookmark_file_clear (GBookmarkFile *bookmark);
@ -681,7 +681,7 @@ g_bookmark_file_clear (GBookmarkFile *bookmark)
struct _ParseData
{
gint state;
ParserState state;
GHashTable *namespaces;
@ -1083,6 +1083,43 @@ is_element_full (ParseData *parse_data,
#define IS_ELEMENT(p,s,e) (is_element_full ((p), (s), NULL, (e), '\0'))
#define IS_ELEMENT_NS(p,s,n,e) (is_element_full ((p), (s), (n), (e), '|'))
static const gchar *
parser_state_to_element_name (ParserState state)
{
switch (state)
{
case STATE_STARTED:
case STATE_FINISHED:
return "(top-level)";
case STATE_ROOT:
return XBEL_ROOT_ELEMENT;
case STATE_BOOKMARK:
return XBEL_BOOKMARK_ELEMENT;
case STATE_TITLE:
return XBEL_TITLE_ELEMENT;
case STATE_DESC:
return XBEL_DESC_ELEMENT;
case STATE_INFO:
return XBEL_INFO_ELEMENT;
case STATE_METADATA:
return XBEL_METADATA_ELEMENT;
case STATE_APPLICATIONS:
return BOOKMARK_APPLICATIONS_ELEMENT;
case STATE_APPLICATION:
return BOOKMARK_APPLICATION_ELEMENT;
case STATE_GROUPS:
return BOOKMARK_GROUPS_ELEMENT;
case STATE_GROUP:
return BOOKMARK_GROUP_ELEMENT;
case STATE_MIME:
return MIME_TYPE_ELEMENT;
case STATE_ICON:
return BOOKMARK_ICON_ELEMENT;
default:
g_assert_not_reached ();
}
}
static void
start_element_raw_cb (GMarkupParseContext *context,
const gchar *element_name,
@ -1263,8 +1300,23 @@ start_element_raw_cb (GMarkupParseContext *context,
element_name,
BOOKMARK_GROUP_ELEMENT);
break;
case STATE_TITLE:
case STATE_DESC:
case STATE_APPLICATION:
case STATE_GROUP:
case STATE_MIME:
case STATE_ICON:
case STATE_FINISHED:
g_set_error (error, G_MARKUP_ERROR,
G_MARKUP_ERROR_INVALID_CONTENT,
_("Unexpected tag “%s” inside “%s”"),
element_name,
parser_state_to_element_name (parse_data->state));
break;
default:
g_warn_if_reached ();
g_assert_not_reached ();
break;
}
}

View File

@ -143,6 +143,27 @@ dist_test_data += \
bookmarks/fail-15.xbel \
bookmarks/fail-16.xbel \
bookmarks/fail-17.xbel \
bookmarks/fail-18.xbel \
bookmarks/fail-19.xbel \
bookmarks/fail-20.xbel \
bookmarks/fail-21.xbel \
bookmarks/fail-22.xbel \
bookmarks/fail-23.xbel \
bookmarks/fail-24.xbel \
bookmarks/fail-25.xbel \
bookmarks/fail-26.xbel \
bookmarks/fail-27.xbel \
bookmarks/fail-28.xbel \
bookmarks/fail-29.xbel \
bookmarks/fail-30.xbel \
bookmarks/fail-31.xbel \
bookmarks/fail-32.xbel \
bookmarks/fail-33.xbel \
bookmarks/fail-34.xbel \
bookmarks/fail-35.xbel \
bookmarks/fail-36.xbel \
bookmarks/fail-37.xbel \
bookmarks/fail-38.xbel \
bookmarks/valid-01.xbel \
bookmarks/valid-02.xbel \
bookmarks/valid-03.xbel \

View File

@ -0,0 +1 @@
<?><!DOCTYPE><xbel version="1.0"xmlns:bookmark="http://www.freedesktop.org/standards/desktop-bookmarks"><title><bookmark:application c=""/><bookmark:application name=""exec=""/

View File

@ -0,0 +1 @@
<?><!DOCTYPE><xbel version="1.0"xmlns:mime="http://www.freedesktop.org/standards/shared-mime-info"xmlns:bookmark="http://www.freedesktop.org/standards/desktop-bookmarks"><title><mime:mime-type></mime:mime-type><bookmark:applications><bookmark:application name=""exec=""/

View File

@ -0,0 +1 @@
<?><!DOCTYPE<<><>>></

View File

@ -0,0 +1 @@
<?><!DOCTYPE><xbel version="1.0"xmlns:bookmark="http://www.freedesktop.org/standards/desktop-bookmarks"><title><bookmark:application e=""/><bookmark:application name=""exec=""/

View File

@ -0,0 +1 @@
<?><!DOCTYPE><xbel version="1.0"xmlns:bookmark="http://www.freedesktop.org/standards/desktop-bookmarks"><title><bookmark:application e=""/><bookmark:application name=""exec="">

View File

@ -0,0 +1 @@
</

View File

@ -0,0 +1 @@
<?><!DOCTYPE><xbel version="1.0"xmlns:bookmark="http://www.freedesktop.org/standards/desktop-bookmarks"><title><bookmark:application n=""/><bookmark:application name=""exec="">

View File

@ -0,0 +1 @@
<?><!DOCTYPE><xbel version="1.0"xmlns:bookmark="http://www.freedesktop.org/standards/desktop-bookmarks"><title><bookmark:applications></bookmark:applications><bookmark:groups><bookmark:group><

View File

@ -0,0 +1 @@
<?><!DOCTYPE<><>></

View File

@ -0,0 +1 @@
<?><!DOCTYPE><xbel version="1.0"xmlns:bookmark="http://www.freedesktop.org/standards/desktop-bookmarks"f=""><title><bookmark:application e=""/><bookmark:application name=""exec=""/

View File

@ -0,0 +1 @@
<?><!DOCTYPE<><><>></

View File

@ -0,0 +1 @@
<?></

View File

@ -0,0 +1 @@
<?><!DOCTYPE<><<>><>></

View File

@ -0,0 +1 @@
<?><!DOCTYPE<><<>>></

View File

@ -0,0 +1 @@
<?><!DOCTYPE></

View File

@ -0,0 +1 @@
<?><!DOCTYPE<><><><>></

View File

@ -0,0 +1 @@
<?><!DOCTYPE<<>>></

View File

@ -0,0 +1 @@
<?><!DOCTYPE><xbel version="1.0"xmlns:mime="http://www.freedesktop.org/standards/shared-mime-info"m=""><title><mime:mime-type></mime:mime-type><mime:mime-type>

View File

@ -0,0 +1 @@
<?><!DOCTYPE><xbel version="1.0"xmlns:mime="http://www.freedesktop.org/standards/shared-mime-info"xmlns:bookmark="http://www.freedesktop.org/standards/desktop-bookmarks"><title><mime:mime-type></mime:mime-type><bookmark:applications><application name=""exec="">

View File

@ -0,0 +1 @@
<?><!DOCTYPE><xbel version="1.0"xmlns:mime="http://www.freedesktop.org/standards/shared-mime-info"xmlns:bookmark="http://www.freedesktop.org/standards/desktop-bookmarks"><title><mime:mime-type></mime:mime-type><bookmark:applications><bookmark:application name=""exec="">

View File

@ -0,0 +1 @@
<?><!DOCTYPE><xbel x=""/><o xmlns:bookmark="http://www.freedesktop.org/standards/desktop-bookmarks"><bookmark:application e=""/><bookmark:application name=""exec=""/