Skip to content

Commit

Permalink
PyCOMPS_fromxml_str: fix a crash on valid xml but no comps input
Browse files Browse the repository at this point in the history
On input such as: `<whatever />` there is no `comps_doc` created ->
create it to make sure we don't crash.
This unifies the behavior with `PyCOMPS_fromxml_f`.

Also adds a unit test.
  • Loading branch information
kontura authored and jan-kolarik committed Oct 4, 2023
1 parent a5321b3 commit 62c69d8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
11 changes: 10 additions & 1 deletion libcomps/src/python/src/pycomps.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,16 @@ PyObject* PyCOMPS_fromxml_str(PyObject *self, PyObject *args, PyObject *kwds) {
Py_CLEAR(self_comps->p_whiteout);
COMPS_OBJECT_DESTROY(self_comps->comps_doc);

self_comps->comps_doc = parsed->comps_doc;
if (parsed->comps_doc) {
self_comps->comps_doc = parsed->comps_doc;
} else {
COMPS_Object *tmpstr;
tmpstr = (COMPS_Object*)comps_str("UTF-8");
self_comps->comps_doc = COMPS_OBJECT_CREATE(COMPS_Doc,
(COMPS_Object*[]){tmpstr});
COMPS_OBJECT_DESTROY(tmpstr);
}

COMPS_OBJECT_DESTROY(self_comps->comps_doc->log);
self_comps->comps_doc->log = parsed->log;
parsed->log = NULL;
Expand Down
7 changes: 7 additions & 0 deletions libcomps/src/python/tests/__test.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,13 @@ def test_xml(self):
comps4 = libcomps.Comps()
self.assertRaises(libcomps.ParserError, comps4.fromxml_str, str(INVALID_COMPS_XML))

VALID_XML_NO_COMPS = "<whatever />"
comps5 = libcomps.Comps()
ret = comps5.fromxml_str(str(VALID_XML_NO_COMPS))
# return code 1 is non fatal error
self.assertTrue(ret == 1, comps5.get_last_errors())


#@unittest.skip("")
def test_fedora(self):
comps = libcomps.Comps()
Expand Down

0 comments on commit 62c69d8

Please sign in to comment.