From 60ca8fe3db7d2032a815f19a8299a86ee63c5267 Mon Sep 17 00:00:00 2001 From: Aleš Matěj Date: Thu, 9 May 2019 08:48:32 +0200 Subject: [PATCH] Fix crash when dumping updateinfo and module is ommited (RhBug:1707981) --- src/xml_dump_updateinfo.c | 23 +++++++++++++---------- tests/python/tests/test_updateinfo.py | 179 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 192 insertions(+), 10 deletions(-) diff --git a/src/xml_dump_updateinfo.c b/src/xml_dump_updateinfo.c index d5ae96f..fafe686 100644 --- a/src/xml_dump_updateinfo.c +++ b/src/xml_dump_updateinfo.c @@ -68,16 +68,19 @@ cr_xml_dump_updatecollectionpackages(xmlNodePtr collection, GSList *packages) void cr_xml_dump_updatecollectionmodule(xmlNodePtr collection, cr_UpdateCollectionModule *module) { - xmlNodePtr xml_module; - xml_module = xmlNewChild(collection, NULL, BAD_CAST "module", NULL); - - cr_xmlNewProp_c(xml_module, BAD_CAST "name", BAD_CAST module->name); - cr_xmlNewProp_c(xml_module, BAD_CAST "stream", BAD_CAST module->stream); - gchar buf[21]; //20 + '\0' is max number of chars of guint64: G_MAXUINT64 (= 18,446,744,073,709,551,615) - snprintf(buf, 21, "%" G_GUINT64_FORMAT, module->version); - cr_xmlNewProp_c(xml_module, BAD_CAST "version", BAD_CAST buf); - cr_xmlNewProp_c(xml_module, BAD_CAST "context", BAD_CAST module->context); - cr_xmlNewProp_c(xml_module, BAD_CAST "arch", BAD_CAST module->arch); + if (!module) + return; + + xmlNodePtr xml_module; + xml_module = xmlNewChild(collection, NULL, BAD_CAST "module", NULL); + + cr_xmlNewProp_c(xml_module, BAD_CAST "name", BAD_CAST module->name); + cr_xmlNewProp_c(xml_module, BAD_CAST "stream", BAD_CAST module->stream); + gchar buf[21]; //20 + '\0' is max number of chars of guint64: G_MAXUINT64 (= 18,446,744,073,709,551,615) + snprintf(buf, 21, "%" G_GUINT64_FORMAT, module->version); + cr_xmlNewProp_c(xml_module, BAD_CAST "version", BAD_CAST buf); + cr_xmlNewProp_c(xml_module, BAD_CAST "context", BAD_CAST module->context); + cr_xmlNewProp_c(xml_module, BAD_CAST "arch", BAD_CAST module->arch); } void diff --git a/tests/python/tests/test_updateinfo.py b/tests/python/tests/test_updateinfo.py index e89f1a8..727b707 100644 --- a/tests/python/tests/test_updateinfo.py +++ b/tests/python/tests/test_updateinfo.py @@ -210,3 +210,182 @@ class TestCaseUpdateInfo(unittest.TestCase): """ % {"now": now.strftime("%Y-%m-%d %H:%M:%S")}) + + def test_updateinfo_xml_dump_04(self): + now = datetime.now() + # Microseconds are always 0 in updateinfo + now = datetime(now.year, now.month, now.day, now.hour, now.minute, + now.second, 0) + + pkg = cr.UpdateCollectionPackage() + pkg.name = "foo" + pkg.version = "1.2" + pkg.release = "3" + pkg.epoch = "0" + pkg.arch = "x86" + pkg.src = "foo.src.rpm" + pkg.filename = "foo.rpm" + pkg.sum = "abcdef" + pkg.sum_type = cr.SHA1 + pkg.reboot_suggested = True + + # Collection without module + col = cr.UpdateCollection() + col.shortname = "short name" + col.name = "long name" + col.append(pkg) + + ref = cr.UpdateReference() + ref.href = "href" + ref.id = "id" + ref.type = "type" + ref.title = "title" + + rec = cr.UpdateRecord() + rec.fromstr = "from" + rec.status = "status" + rec.type = "type" + rec.version = "version" + rec.id = "id" + rec.title = "title" + rec.issued_date = now + rec.updated_date = now + rec.rights = "rights" + rec.release = "release" + rec.pushcount = "pushcount" + rec.severity = "severity" + rec.summary = "summary" + rec.description = "description" + rec.solution = "solution" + rec.append_collection(col) + rec.append_reference(ref) + + ui = cr.UpdateInfo() + ui.append(rec) + + xml = ui.xml_dump() + + self.assertEqual(xml, +""" + + + id + title + + + rights + release + pushcount + severity + summary + description + solution + + + + + + long name + + foo.rpm + abcdef + + + + + + +""" % {"now": now.strftime("%Y-%m-%d %H:%M:%S")}) + + def test_updateinfo_xml_dump_05(self): + now = datetime.now() + # Microseconds are always 0 in updateinfo + now = datetime(now.year, now.month, now.day, now.hour, now.minute, + now.second, 0) + + # Collection module with unset fields + mod = cr.UpdateCollectionModule() + mod.version = 18446744073709551615 + mod.context = "deadbeef" + mod.arch = "x86" + + pkg = cr.UpdateCollectionPackage() + pkg.name = "foo" + pkg.version = "1.2" + pkg.release = "3" + pkg.epoch = "0" + pkg.arch = "x86" + pkg.src = "foo.src.rpm" + pkg.filename = "foo.rpm" + pkg.sum = "abcdef" + pkg.sum_type = cr.SHA1 + pkg.reboot_suggested = True + + col = cr.UpdateCollection() + col.shortname = "short name" + col.name = "long name" + col.module = mod + col.append(pkg) + + ref = cr.UpdateReference() + ref.href = "href" + ref.id = "id" + ref.type = "type" + ref.title = "title" + + rec = cr.UpdateRecord() + rec.fromstr = "from" + rec.status = "status" + rec.type = "type" + rec.version = "version" + rec.id = "id" + rec.title = "title" + rec.issued_date = now + rec.updated_date = now + rec.rights = "rights" + rec.release = "release" + rec.pushcount = "pushcount" + rec.severity = "severity" + rec.summary = "summary" + rec.description = "description" + rec.solution = "solution" + rec.append_collection(col) + rec.append_reference(ref) + + ui = cr.UpdateInfo() + ui.append(rec) + + xml = ui.xml_dump() + + self.assertEqual(xml, +""" + + + id + title + + + rights + release + pushcount + severity + summary + description + solution + + + + + + long name + + + foo.rpm + abcdef + + + + + + +""" % {"now": now.strftime("%Y-%m-%d %H:%M:%S")}) -- libgit2 0.27.7