From 2fde09a614399ab805e94b036ac367beaa5a16c9 Mon Sep 17 00:00:00 2001 From: Jim Evins Date: Fri, 24 Dec 2010 13:53:33 -0500 Subject: [PATCH] Fix crash when encountering an invalid barcode backend A file may have been saved from an instance of glabels that had a backend enabled that is not enabled in the current instance of glabels. This fix accounts for this. It is also possible that the glabels file was created manually with an invalid backend ID or a typo. --- src/bc-backends.c | 25 +++++++++++++++++++++++++ src/bc-backends.h | 1 + src/xml-label.c | 12 ++++++++++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/bc-backends.c b/src/bc-backends.c index 9f9904e2..0442f7f9 100644 --- a/src/bc-backends.c +++ b/src/bc-backends.c @@ -628,6 +628,31 @@ gl_barcode_backends_backend_name_to_id (const gchar *backend_name) } +/*****************************************************************************/ +/* Test if backend id is valid. */ +/*****************************************************************************/ +gboolean +gl_barcode_backends_is_backend_id_valid (const gchar *backend_id) +{ + gint i; + + if (backend_id == NULL) + { + return FALSE; + } + + for (i=0; backends[i].id != NULL; i++) + { + if (g_ascii_strcasecmp (backend_id, backends[i].id) == 0) + { + return TRUE; + } + } + + return FALSE; +} + + /*****************************************************************************/ /* Guess backend id from style id (for backwards compatability). */ /*****************************************************************************/ diff --git a/src/bc-backends.h b/src/bc-backends.h index 8ed764db..e284c171 100644 --- a/src/bc-backends.h +++ b/src/bc-backends.h @@ -33,6 +33,7 @@ void gl_barcode_backends_free_backend_list (GList *backe const gchar *gl_barcode_backends_backend_id_to_name (const gchar *backend_id); const gchar *gl_barcode_backends_backend_name_to_id (const gchar *backend_name); +gboolean gl_barcode_backends_is_backend_id_valid (const gchar *backend_id); const gchar *gl_barcode_backends_guess_backend_id (const gchar *id); GList *gl_barcode_backends_get_styles_list (const gchar *backend_id); diff --git a/src/xml-label.c b/src/xml-label.c index 0e7e7716..79b46469 100644 --- a/src/xml-label.c +++ b/src/xml-label.c @@ -768,9 +768,17 @@ xml_parse_object_barcode (xmlNodePtr node, style = gl_label_barcode_style_new (); backend_id = lgl_xml_get_prop_string (node, "backend", NULL); id = lgl_xml_get_prop_string (node, "style", NULL); - if ( !backend_id ) + if ( !gl_barcode_backends_is_backend_id_valid (backend_id) ) { - backend_id = g_strdup (gl_barcode_backends_guess_backend_id (id)); + if ( backend_id == NULL ) + { + backend_id = g_strdup (gl_barcode_backends_guess_backend_id (id)); + } + else + { + g_free (backend_id); + backend_id = g_strdup ("built-in"); + } } gl_label_barcode_style_set_backend_id (style, backend_id); gl_label_barcode_style_set_style_id (style, id); -- 2.39.5