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
6 changes: 6 additions & 0 deletions doc/source/whatsnew.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ v2.2.0.dev0
``FILE`` type. This could happen with an already-preprocessed system
header, which spells out the real struct. (`#281`_).

* Fixed building on CPython 3.16, which removed
``Py_FileSystemDefaultEncoding``. A file name with an embedded null
character passed to ``ffi.dlopen()`` now raises ``ValueError`` instead of
``TypeError``. (`#287`_).

.. _`#265`: https://github.com/python-cffi/cffi/pull/265
.. _`#281`: https://github.com/python-cffi/cffi/pull/281
.. _`#287`: https://github.com/python-cffi/cffi/pull/287

v2.1.0
======
Expand Down
10 changes: 4 additions & 6 deletions src/c/_cffi_backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -4537,14 +4537,13 @@ static void *b_do_dlopen(PyObject *args, const char **p_printable_filename,
}
PyErr_Clear();
#endif
if (!PyArg_ParseTuple(args, "et|i:load_library",
Py_FileSystemDefaultEncoding, &filename_or_null, &flags))
if (!PyArg_ParseTuple(args, "O&|i:load_library",
PyUnicode_FSConverter, p_temp, &flags))
return NULL;
*p_printable_filename = PyUnicode_AsUTF8(s);
if (*p_printable_filename == NULL) {
PyMem_Free(filename_or_null);
if (*p_printable_filename == NULL)
return NULL;
}
filename_or_null = PyBytes_AS_STRING(*p_temp);
}
if ((flags & (RTLD_NOW | RTLD_LAZY)) == 0)
flags |= RTLD_NOW;
Expand All @@ -4557,7 +4556,6 @@ static void *b_do_dlopen(PyObject *args, const char **p_printable_filename,
#endif

handle = dlopen(filename_or_null, flags);
PyMem_Free(filename_or_null);

#ifdef MS_WIN32
got_handle:
Expand Down
7 changes: 7 additions & 0 deletions src/c/test_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ def test_load_library():
x = find_and_load_library('c', RTLD_LAZY)
assert repr(x).startswith("<clibrary '")

def test_load_library_invalid_filename():
pytest.raises(TypeError, load_library, b"foo")
pytest.raises(TypeError, load_library, "foo", "not-an-int")
if sys.platform != 'win32':
# the Windows code path does not reject embedded null characters
pytest.raises(ValueError, load_library, "foo\x00bar")

def test_all_rtld_symbols():
import sys
FFI_DEFAULT_ABI # these symbols must be defined
Expand Down