Skip to content

fix the embedded null character issue #492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 19, 2025
Merged
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
13 changes: 13 additions & 0 deletions src/StrType.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ PyObject *StrType::proxifyString(JSContext *cx, JS::HandleValue strVal) {

if (JS::LinearStringHasLatin1Chars(lstr)) { // latin1 spidermonkey, latin1 python
const JS::Latin1Char *chars = JS::GetLatin1LinearStringChars(nogc, lstr);
if ((PY_VERSION_HEX) >= 0x030d0000) { // Python version is greater than 3.13
// Short path to temporarily fix the issue with Python 3.13+ compact unicode representation.
// It would error with `ValueError: embedded null character`, which is caused by the fact that
// most Python C APIs assume the string buffer is null-terminated, so we need to create a copy.
PyObject *copied = PyUnicode_FromKindAndData(PyUnicode_1BYTE_KIND, chars, length);
Py_DECREF(pyString);
return copied;
}

PY_UNICODE_OBJECT_DATA_ANY(pyString) = (void *)chars;
PY_UNICODE_OBJECT_KIND(pyString) = PyUnicode_1BYTE_KIND;
Expand Down Expand Up @@ -189,6 +197,11 @@ PyObject *StrType::proxifyString(JSContext *cx, JS::HandleValue strVal) {
Py_DECREF(pyString);
return ucs4Obj;
}
if ((PY_VERSION_HEX) >= 0x030d0000) { // Python 3.13+, fix `ValueError: embedded null character`
PyObject *copied = PyUnicode_FromKindAndData(PyUnicode_2BYTE_KIND, chars, length); // create a copy of the string buffer
Py_DECREF(pyString);
return copied;
}
}

return (PyObject *)pyString;
Expand Down
Loading