>From 81171c21683af6be007efd5576fda264d2afe12d Mon Sep 17 00:00:00 2001 From: Alexander Bokovoy Date: Mon, 22 Jul 2013 16:03:39 +0300 Subject: [PATCH] pysss: prevent crashing when group is unresolvable In unlikely case that an NSS module returns a reference to a group and we are unable to resolve it shortly after that, make sure these groups are skipped. --- src/python/pysss.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/python/pysss.c b/src/python/pysss.c index 6ae9a25..4dd0351 100644 --- a/src/python/pysss.c +++ b/src/python/pysss.c @@ -763,7 +763,7 @@ static PyObject *py_sss_getgrouplist(PyObject *self, PyObject *args) struct group *gr; int ngroups; int ret; - Py_ssize_t i; + Py_ssize_t i, idx; PyObject *groups_tuple; if(!PyArg_ParseTuple(args, discard_const_p(char, "s"), &username)) { @@ -793,9 +793,20 @@ static PyObject *py_sss_getgrouplist(PyObject *self, PyObject *args) goto fail; } + /* Populate a tuple with names of groups + * In unlikely case of group not being able to resolve, skip it + * We also need to resize resulting tuple to avoid empty elements there */ + idx = 0; for (i = 0; i < ngroups; i++) { gr = getgrgid(groups[i]); - PyTuple_SetItem(groups_tuple, i, PyString_FromString(gr->gr_name)); + if (gr) { + PyTuple_SetItem(groups_tuple, idx, PyString_FromString(gr->gr_name)); + idx++; + } + } + + if (i != idx) { + _PyTuple_Resize(&groups_tuple, idx); } return groups_tuple; -- 1.8.3.1