Author: Armin Rigo <ar...@tunes.org> Branch: Changeset: r2197:856cf4804a52 Date: 2015-06-30 09:16 +0200 http://bitbucket.org/cffi/cffi/changeset/856cf4804a52/
Log: Issue #209: check for dereferencing NULL pointers diff --git a/c/_cffi_backend.c b/c/_cffi_backend.c --- a/c/_cffi_backend.c +++ b/c/_cffi_backend.c @@ -1898,11 +1898,21 @@ return NULL; if (cd->c_type->ct_flags & CT_POINTER) { - if (CDataOwn_Check(cd) && i != 0) { - PyErr_Format(PyExc_IndexError, - "cdata '%s' can only be indexed by 0", - cd->c_type->ct_name); - return NULL; + if (CDataOwn_Check(cd)) { + if (i != 0) { + PyErr_Format(PyExc_IndexError, + "cdata '%s' can only be indexed by 0", + cd->c_type->ct_name); + return NULL; + } + } + else { + if (cd->c_data == NULL) { + PyErr_Format(PyExc_RuntimeError, + "cannot dereference null pointer from cdata '%s'", + cd->c_type->ct_name); + return NULL; + } } } else if (cd->c_type->ct_flags & CT_ARRAY) { diff --git a/c/test_c.py b/c/test_c.py --- a/c/test_c.py +++ b/c/test_c.py @@ -2110,8 +2110,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") @@ -3344,6 +3343,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" diff --git a/doc/source/whatsnew.rst b/doc/source/whatsnew.rst --- a/doc/source/whatsnew.rst +++ b/doc/source/whatsnew.rst @@ -30,6 +30,11 @@ dict---assuming that ``lib`` has got no symbol called precisely ``__dict__``. (In general, it is safer to use ``dir(lib)``.) +* Issue #209: dereferencing NULL pointers now raises RuntimeError + instead of segfaulting. Meant as a debugging aid. The check is + only for NULL: if you dereference random or dead pointers you might + still get segfaults. + 1.1.2 ===== _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit