Author: Armin Rigo <ar...@tunes.org> Branch: Changeset: r78356:56d1b320d259 Date: 2015-06-30 09:19 +0200 http://bitbucket.org/pypy/pypy/changeset/56d1b320d259/
Log: Issue #209: check for dereferencing NULL pointers diff --git a/pypy/module/_cffi_backend/ctypeptr.py b/pypy/module/_cffi_backend/ctypeptr.py --- a/pypy/module/_cffi_backend/ctypeptr.py +++ b/pypy/module/_cffi_backend/ctypeptr.py @@ -223,9 +223,13 @@ if (isinstance(w_cdata, cdataobj.W_CDataNewOwning) or isinstance(w_cdata, cdataobj.W_CDataPtrToStructOrUnion)): if i != 0: - space = self.space - raise oefmt(space.w_IndexError, + raise oefmt(self.space.w_IndexError, "cdata '%s' can only be indexed by 0", self.name) + else: + if not w_cdata.unsafe_escaping_ptr(): + raise oefmt(self.space.w_RuntimeError, + "cannot dereference null pointer from cdata '%s'", + self.name) return self def _check_slice_index(self, w_cdata, start, stop): diff --git a/pypy/module/_cffi_backend/test/_backend_test_c.py b/pypy/module/_cffi_backend/test/_backend_test_c.py --- a/pypy/module/_cffi_backend/test/_backend_test_c.py +++ b/pypy/module/_cffi_backend/test/_backend_test_c.py @@ -2099,8 +2099,7 @@ p = cast(BVoidP, 123456) py.test.raises(TypeError, "p[0]") p = cast(BVoidP, 0) - if 'PY_DOT_PY' in globals(): py.test.skip("NULL crashes early on py.py") - py.test.raises(TypeError, "p[0]") + py.test.raises((TypeError, RuntimeError), "p[0]") def test_iter(): BInt = new_primitive_type("int") @@ -3333,6 +3332,15 @@ check(4 | 8, "CHB", "GTB") check(4 | 16, "CHB", "ROB") +def test_dereference_null_ptr(): + BInt = new_primitive_type("int") + BIntPtr = new_pointer_type(BInt) + p = cast(BIntPtr, 0) + py.test.raises(RuntimeError, "p[0]") + py.test.raises(RuntimeError, "p[0] = 42") + py.test.raises(RuntimeError, "p[42]") + py.test.raises(RuntimeError, "p[42] = -1") + def test_version(): # this test is here mostly for PyPy assert __version__ == "1.1.2" _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit