From 51fc7782472cdfb1905aa48f405c0f192ba6c373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1=20Mat=C4=9Bj?= Date: Wed, 27 Sep 2023 08:27:58 +0200 Subject: [PATCH] xml parsing: fix a crash on valid xml with but no comps On input like: ``` ``` the document is parsed but there is no `comps_doc` so we cannot work with it. Also adds a unit test. --- libcomps/src/comps_parse.c | 6 +++--- libcomps/src/python/tests/__test.py | 9 +++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/libcomps/src/comps_parse.c b/libcomps/src/comps_parse.c index 18133a2..7b4bf3c 100644 --- a/libcomps/src/comps_parse.c +++ b/libcomps/src/comps_parse.c @@ -176,21 +176,21 @@ int comps_parse_validate_dtd(char *filename, char *dtd_file) { } void __comps_after_parse(COMPS_Parsed *parsed) { - if (parsed->doctype_name) { + if (parsed->doctype_name && parsed->comps_doc) { COMPS_OBJECT_DESTROY(parsed->comps_doc->doctype_name); parsed->comps_doc->doctype_name = (COMPS_Str*) COMPS_OBJECT_INCREF(parsed->doctype_name); } else { //parsed->comps_doc->doctype_name = comps_str(comps_default_doctype_name); } - if (parsed->doctype_sysid) { + if (parsed->doctype_sysid && parsed->comps_doc) { COMPS_OBJECT_DESTROY(parsed->comps_doc->doctype_sysid); parsed->comps_doc->doctype_sysid = (COMPS_Str*) COMPS_OBJECT_INCREF(parsed->doctype_sysid); } else { //parsed->comps_doc->doctype_sysid = comps_str(comps_default_doctype_sysid); } - if (parsed->doctype_pubid) { + if (parsed->doctype_pubid && parsed->comps_doc) { COMPS_OBJECT_DESTROY(parsed->comps_doc->doctype_pubid); parsed->comps_doc->doctype_pubid = (COMPS_Str*) COMPS_OBJECT_INCREF(parsed->doctype_pubid); diff --git a/libcomps/src/python/tests/__test.py b/libcomps/src/python/tests/__test.py index ba4b349..18200c5 100644 --- a/libcomps/src/python/tests/__test.py +++ b/libcomps/src/python/tests/__test.py @@ -743,6 +743,15 @@ def test_xml(self): # return code 1 is non fatal error self.assertTrue(ret == 1, comps5.get_last_errors()) + VALID_XML_HEADER_NO_COMPS = """ + + + """ + comps6 = libcomps.Comps() + ret = comps6.fromxml_str(str(VALID_XML_HEADER_NO_COMPS)) + # return code 1 is non fatal error + self.assertTrue(ret == 1, comps6.get_last_errors()) + #@unittest.skip("") def test_fedora(self):