Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 62 additions & 10 deletions src/c/_cffi_backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -2164,9 +2164,15 @@ static PyObject *cdata_repr(CDataObject *cd)
extra = " &";
else
extra = "";

const char *s_utf8 = PyText_AsUTF8(s);
if (s_utf8 == NULL) {
Py_DECREF(s);
return NULL;
}
result = PyUnicode_FromFormat("<cdata '%s%s' %s>",
cd->c_type->ct_name, extra,
PyUnicode_AsUTF8(s));
s_utf8);
Py_DECREF(s);
return result;
}
Expand All @@ -2176,8 +2182,14 @@ static PyObject *_cdata_repr2(CDataObject *cd, char *text, PyObject *x)
PyObject *res, *s = PyObject_Repr(x);
if (s == NULL)
return NULL;

const char *s_utf8 = PyText_AsUTF8(s);
if (s_utf8 == NULL) {
Py_DECREF(s);
return NULL;
}
res = PyUnicode_FromFormat("<cdata '%s' %s %s>",
cd->c_type->ct_name, text, PyUnicode_AsUTF8(s));
cd->c_type->ct_name, text, s_utf8);
Py_DECREF(s);
return res;
}
Expand Down Expand Up @@ -4488,7 +4500,14 @@ static void *b_do_dlopen(PyObject *args, const char **p_printable_filename,
return NULL;
}
*p_temp = PyUnicode_FromFormat("%p", handle);
if (*p_temp == NULL)
return NULL;
*p_printable_filename = PyUnicode_AsUTF8(*p_temp);
if (*p_printable_filename == NULL) {
Py_DECREF(*p_temp);
*p_temp = NULL;
return NULL;
}
*auto_close = 0;
return handle;
}
Expand Down Expand Up @@ -5069,8 +5088,12 @@ _add_field(PyObject *interned_fields, PyObject *fname, CTypeDescrObject *ftype,
return NULL;

if (PyDict_Size(interned_fields) != prev_size + 1) {
const char *name_utf8 = PyUnicode_AsUTF8(fname);
if (name_utf8 == NULL) {
return NULL;
}
PyErr_Format(PyExc_KeyError, "duplicate field name '%s'",
PyUnicode_AsUTF8(fname));
name_utf8);
return NULL;
}
return cf; /* borrowed reference */
Expand Down Expand Up @@ -5217,9 +5240,13 @@ static PyObject *b_complete_struct_or_union_lock_held(CTypeDescrObject *ct,
ct->ct_flags_mut |= CT_WITH_VAR_ARRAY;
}
else {
const char *fname_utf8 = PyText_AS_UTF8(fname);
if (fname_utf8 == NULL) {
goto finally;
}
PyErr_Format(PyExc_TypeError,
"field '%s.%s' has ctype '%s' of unknown size",
ct->ct_name, PyUnicode_AsUTF8(fname),
ct->ct_name, fname_utf8,
ftype->ct_name);
goto finally;
}
Expand Down Expand Up @@ -5287,9 +5314,13 @@ static PyObject *b_complete_struct_or_union_lock_held(CTypeDescrObject *ct,
if (foffset >= 0) {
/* a forced field position: ignore the offset just computed,
except to know if we must set CT_CUSTOM_FIELD_POS */
const char *fname_utf8 = PyUnicode_AsUTF8(fname);
if (fname_utf8 == NULL) {
goto finally;
}
if (detect_custom_layout(ct, sflags, byteoffset, foffset,
"wrong offset for field '",
PyUnicode_AsUTF8(fname), "'") < 0)
fname_utf8, "'") < 0)
goto finally;
byteoffset = foffset;
}
Expand Down Expand Up @@ -5333,27 +5364,40 @@ static PyObject *b_complete_struct_or_union_lock_held(CTypeDescrObject *ct,
int bits_already_occupied, bitshift;

if (foffset >= 0) {
const char *fname_utf8 = PyUnicode_AsUTF8(fname);
if (fname_utf8 == NULL) {
goto finally;
}

PyErr_Format(PyExc_TypeError,
"field '%s.%s' is a bitfield, "
"but a fixed offset is specified",
ct->ct_name, PyUnicode_AsUTF8(fname));
ct->ct_name, fname_utf8);
goto finally;
}

if (!(ftype->ct_flags & (CT_PRIMITIVE_SIGNED |
CT_PRIMITIVE_UNSIGNED |
CT_PRIMITIVE_CHAR))) {
const char *fname_utf8 = PyUnicode_AsUTF8(fname);
if (fname_utf8 == NULL) {
goto finally;
}
PyErr_Format(PyExc_TypeError,
"field '%s.%s' declared as '%s' cannot be a bit field",
ct->ct_name, PyUnicode_AsUTF8(fname),
ct->ct_name, fname_utf8,
ftype->ct_name);
goto finally;
}
if (fbitsize > 8 * ftype->ct_size) {
const char *fname_utf8 = PyUnicode_AsUTF8(fname);
if (fname_utf8 == NULL) {
goto finally;
}
PyErr_Format(PyExc_TypeError,
"bit field '%s.%s' is declared '%s:%d', which "
"exceeds the width of the type",
ct->ct_name, PyUnicode_AsUTF8(fname),
ct->ct_name, fname_utf8,
ftype->ct_name, fbitsize);
goto finally;
}
Expand All @@ -5366,9 +5410,13 @@ static PyObject *b_complete_struct_or_union_lock_held(CTypeDescrObject *ct,

if (fbitsize == 0) {
if (PyUnicode_GetLength(fname) > 0) {
const char *fname_utf8 = PyUnicode_AsUTF8(fname);
if (fname_utf8 == NULL) {
goto finally;
}
PyErr_Format(PyExc_TypeError,
"field '%s.%s' is declared with :0",
ct->ct_name, PyUnicode_AsUTF8(fname));
ct->ct_name, fname_utf8);
goto finally;
}
if (!(sflags & SF_MSVC_BITFIELDS)) {
Expand Down Expand Up @@ -5405,10 +5453,14 @@ static PyObject *b_complete_struct_or_union_lock_held(CTypeDescrObject *ct,
allowed position */
if ((sflags & SF_PACKED) &&
(bits_already_occupied & 7)) {
const char *fname_utf8 = PyUnicode_AsUTF8(fname);
if (fname_utf8 == NULL) {
goto finally;
}
PyErr_Format(PyExc_NotImplementedError,
"with 'packed', gcc would compile field "
"'%s.%s' to reuse some bits in the previous "
"field", ct->ct_name, PyUnicode_AsUTF8(fname));
"field", ct->ct_name, fname_utf8);
goto finally;
}
field_offset_bytes += falign;
Expand Down
8 changes: 7 additions & 1 deletion src/c/cglob.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,14 @@ static void *fetch_global_var_addr(GlobSupportObject *gs)
Py_END_ALLOW_THREADS
}
if (data == NULL) {

const char *name_utf8 = PyText_AS_UTF8(gs->gs_name);
if (name_utf8 == NULL) {
return NULL;
}
PyErr_Format(FFIError, "global variable '%s' is at address NULL",
PyUnicode_AsUTF8(gs->gs_name));
name_utf8);

return NULL;
}
return data;
Expand Down
3 changes: 3 additions & 0 deletions src/c/ffi_obj.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ static CTypeDescrObject *_ffi_type(FFIObject *ffi, PyObject *arg,

if (x == NULL) {
const char *input_text = PyUnicode_AsUTF8(arg);
if (input_text == NULL) {
return NULL;
}
struct _cffi_parse_info_s info;
info.ctx = &ffi->types_builder.ctx;
info.output_size = FFI_COMPLEXITY_OUTPUT;
Expand Down