From a99b4d5a0751f1c9d24a0840ab7dcfe50feb6f34 Mon Sep 17 00:00:00 2001 From: Mattia Verga Date: Wed, 4 Oct 2023 13:42:08 +0200 Subject: [PATCH] Backport additional patch to fix segfault in fromxml_str --- ...crash-on-valid-xml-with-but-no-comps.patch | 70 +++++++++++++++++++ libcomps.spec | 6 +- 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 fix-a-crash-on-valid-xml-with-but-no-comps.patch diff --git a/fix-a-crash-on-valid-xml-with-but-no-comps.patch b/fix-a-crash-on-valid-xml-with-but-no-comps.patch new file mode 100644 index 0000000..2651e91 --- /dev/null +++ b/fix-a-crash-on-valid-xml-with-but-no-comps.patch @@ -0,0 +1,70 @@ +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): diff --git a/libcomps.spec b/libcomps.spec index 3a9f6e0..03a6f95 100644 --- a/libcomps.spec +++ b/libcomps.spec @@ -2,7 +2,7 @@ Name: libcomps Version: 0.1.19 -Release: 4%{?dist} +Release: 5%{?dist} Summary: Comps XML file manipulation library License: GPL-2.0-or-later @@ -11,6 +11,7 @@ Source0: %{url}/archive/%{version}/%{name}-%{version}.tar.gz # Backported Patch: fix-fromxml_str-segfault.patch +Patch: fix-a-crash-on-valid-xml-with-but-no-comps.patch BuildRequires: gcc-c++ BuildRequires: cmake @@ -119,6 +120,9 @@ popd %{python3_sitearch}/%{name}-%{version}-py%{python3_version}.egg-info %changelog +* Wed Oct 04 2023 Mattia Verga - 0.1.19-5 +- Backport additional patch to fix segfault in fromxml_str + * Tue Sep 05 2023 Mattia Verga - 0.1.19-4 - Backport patch to fix segfault in fromxml_str