From cd78f21438536e0ddef495edbd4fe53ddc6ae8d6 Mon Sep 17 00:00:00 2001 From: Cosimo Cecchi Date: Tue, 1 Jul 2008 17:14:06 +0200 Subject: [PATCH] Finish GErrorizing the _unserialize () funcs. --- src/mm-string-utils.c | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/src/mm-string-utils.c b/src/mm-string-utils.c index 19373d2..baf5ea0 100644 --- a/src/mm-string-utils.c +++ b/src/mm-string-utils.c @@ -91,6 +91,12 @@ set_operator_error (int res, GError **error) } static void +set_filter_error (int res, GError **error) +{ + set_error (res, error, "Filter"); +} + +static void serialize_value (xmlTextWriterPtr writer, GValue *v) { xmlChar *safe; @@ -589,13 +595,12 @@ mm_hit_collection_serialize (MMHitCollection *hc) MMFilter * mm_filter_unserialize (const char *s, GError **error) { - MMFilter *f; + MMFilter *f = NULL; MMFilterParam *fp; xmlTextReaderPtr reader; int res; const xmlChar *node_name; - f = mm_filter_new (); reader = xmlReaderForMemory (s, strlen (s), NULL, NULL, 0); /* cut all the elements before */ @@ -604,24 +609,41 @@ mm_filter_unserialize (const char *s, GError **error) node_name = xmlTextReaderConstName (reader); } while (xmlStrcmp (node_name, BAD_CAST ("filter")) != 0); + /* do the error checking here and not in each iteration of the cycle */ + if (res <= 0) { + set_filter_error (res, error); + goto out; + } + res = xmlTextReaderRead (reader); + node_name = xmlTextReaderConstName (reader); + f = mm_filter_new (); + /* we're either on the first or on if the * object is empty. cycle until we're on . */ while (!((xmlTextReaderNodeType (reader) == XML_READER_TYPE_END_ELEMENT) && - xmlStrcmp (xmlTextReaderConstName (reader), BAD_CAST ("filter")) == 0) && res > 0) { + xmlStrcmp (node_name, BAD_CAST ("filter")) == 0) && res > 0) { fp = unserialize_filter_param (reader, error); - if (!fp) { - break; + if (error) { + g_object_unref (f); + f = NULL; + goto out; } mm_filter_add_filtering_param (f, fp); + g_object_unref (fp); res = xmlTextReaderRead (reader); + node_name = xmlTextReaderConstName (reader); } - if (res == -1) { - g_warning ("Error while parsing the serialized filter."); + if (res <= 0) { + /* do not return an incomplete filter */ + set_filter_error (res, error); + g_object_unref (f); + f = NULL; } +out: xmlFreeTextReader (reader); return f; @@ -644,6 +666,9 @@ mm_hit_collection_unserialize (const char *s, GError **error) node_name = xmlTextReaderConstName (reader); } while (xmlStrcmp (node_name, BAD_CAST ("hit-collection")) != 0); + /* check for errors before all at once, and not + * in every iteration of the cycle. + */ if (res <= 0) { set_hit_collection_error (res, error); goto out; -- 2.11.4.GIT