From 5d7be9b25b69b8706b67f1806ca49c3fc26ed4f7 Mon Sep 17 00:00:00 2001 From: Iustin Pop Date: Fri, 29 Nov 2019 15:01:41 +0100 Subject: [PATCH] Apply no-unitialised-objects to Permset as well This had no checks at all (Entry has one check) on attribute access. --- acl.c | 30 ++++++++++++++++++++++-------- tests/test_acls.py | 22 ++++++++++++++++++++-- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/acl.c b/acl.c index b596f6d..e59467d 100644 --- a/acl.c +++ b/acl.c @@ -1013,14 +1013,30 @@ static PyObject* Entry_copy(PyObject *obj, PyObject *args) { static PyObject* Permset_new(PyTypeObject* type, PyObject* args, PyObject *keywds) { PyObject* newpermset; + Permset_Object* permset; + Entry_Object* parent = NULL; + + if (!PyArg_ParseTuple(args, "O!", &Entry_Type, &parent)) { + return NULL; + } newpermset = PyType_GenericNew(type, args, keywds); - if(newpermset != NULL) { - ((Permset_Object*)newpermset)->permset = NULL; - ((Permset_Object*)newpermset)->parent_entry = NULL; + if(newpermset == NULL) { + return NULL; } + permset = (Permset_Object*)newpermset; + + if(acl_get_permset(parent->entry, &permset->permset) == -1) { + PyErr_SetFromErrno(PyExc_IOError); + Py_DECREF(newpermset); + return NULL; + } + + permset->parent_entry = (PyObject*)parent; + Py_INCREF(parent); + return newpermset; } @@ -1032,14 +1048,12 @@ static int Permset_init(PyObject* obj, PyObject* args, PyObject *keywds) { if (!PyArg_ParseTuple(args, "O!", &Entry_Type, &parent)) return -1; - if(acl_get_permset(parent->entry, &self->permset) == -1) { - PyErr_SetFromErrno(PyExc_IOError); + if ((PyObject*)parent != self->parent_entry) { + PyErr_SetString(PyExc_ValueError, + "Can't reinitialize with a different parent"); return -1; } - self->parent_entry = (PyObject*)parent; - Py_INCREF(parent); - return 0; } diff --git a/tests/test_acls.py b/tests/test_acls.py index bed405a..16ebd78 100644 --- a/tests/test_acls.py +++ b/tests/test_acls.py @@ -29,7 +29,7 @@ import platform import re import errno import operator -import pytest +import pytest # type: ignore import contextlib import pathlib import io @@ -703,12 +703,30 @@ class TestModification: e = acl.append() p1 = e.permset p2 = Permset(e) - #self.assertEqual(p1, p2) + #assert p1 == p2 def test_permset_creation_wrong_arg(self): with pytest.raises(TypeError): Permset(object()) + def test_permset_reinitialisations(self): + a = posix1e.ACL() + e = posix1e.Entry(a) + f = posix1e.Entry(a) + p = e.permset + p.__init__(e) + with pytest.raises(ValueError, match="different parent"): + p.__init__(f) + + @NOT_PYPY + def test_permset_reinit_leaks_refcount(self): + acl = posix1e.ACL() + e = acl.append() + p = e.permset + ref = sys.getrefcount(e) + p.__init__(e) + assert ref == sys.getrefcount(e), "Uh-oh, ref leaks..." + def test_permset(self): """Test permissions""" acl = posix1e.ACL() -- 2.39.2