Commit 7cc3a64b authored by Armin Rigo's avatar Armin Rigo
Browse files

Issue #209: check for dereferencing NULL pointers

parent b8538812a48b
......@@ -1898,11 +1898,21 @@ _cdata_get_indexed_ptr(CDataObject *cd, PyObject *key)
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) {
......
......@@ -2110,8 +2110,7 @@ def test_cannot_dereference_void():
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 @@ def test_from_buffer_more_cases():
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"
......@@ -30,6 +30,11 @@ What's New
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
=====
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment