[pypy-commit] cffi default: Test and fix: it's inconvenient that str(x) crashes with a segfault
arigo
noreply at buildbot.pypy.org
Tue Jun 26 11:15:09 CEST 2012
Author: Armin Rigo <arigo at tunes.org>
Branch:
Changeset: r521:68ad27bb619c
Date: 2012-06-26 11:12 +0200
http://bitbucket.org/cffi/cffi/changeset/68ad27bb619c/
Log: Test and fix: it's inconvenient that str(x) crashes with a segfault
if x is <cdata 'char *' NULL>. Detect this case and raise
RuntimeError.
diff --git a/c/_ffi_backend.c b/c/_ffi_backend.c
--- a/c/_ffi_backend.c
+++ b/c/_ffi_backend.c
@@ -1063,8 +1063,19 @@
if (end != NULL)
length = end - start;
}
- else
+ else {
+ if (cd->c_data == NULL) {
+ PyObject *s = cdata_repr(cd);
+ if (s != NULL) {
+ PyErr_Format(PyExc_RuntimeError,
+ "cannot use str() on %s",
+ PyString_AS_STRING(s));
+ Py_DECREF(s);
+ }
+ return NULL;
+ }
length = strlen(cd->c_data);
+ }
return PyString_FromStringAndSize(cd->c_data, length);
}
diff --git a/c/test_c.py b/c/test_c.py
--- a/c/test_c.py
+++ b/c/test_c.py
@@ -1075,6 +1075,7 @@
assert str(a) == "hello"
p = a + 2
assert str(p) == "llo"
+ py.test.raises(RuntimeError, str, cast(BCharP, 0))
def test_bug_convert_to_ptr():
BChar = new_primitive_type("char")
More information about the pypy-commit
mailing list