[3.12] gh-127208: Reject null character in _imp.create_dynamic() (#127400) (#127419)

gh-127208: Reject null character in _imp.create_dynamic() (#127400)

_imp.create_dynamic() now rejects embedded null characters in the
path and in the module name.

Backport also the _PyUnicode_AsUTF8NoNUL() function.

(cherry picked from commit b14fdadc6c)
This commit is contained in:
Victor Stinner 2024-11-29 17:03:24 +01:00 committed by GitHub
parent fc0564b365
commit e7ab97862d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 33 additions and 3 deletions

View file

@ -3991,6 +3991,18 @@ PyUnicode_AsUTF8(PyObject *unicode)
return PyUnicode_AsUTF8AndSize(unicode, NULL);
}
const char *
_PyUnicode_AsUTF8NoNUL(PyObject *unicode)
{
Py_ssize_t size;
const char *s = PyUnicode_AsUTF8AndSize(unicode, &size);
if (s && strlen(s) != (size_t)size) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
return NULL;
}
return s;
}
/*
PyUnicode_GetSize() has been deprecated since Python 3.3
because it returned length of Py_UNICODE.