Skip to content

Commit 0a4df74

Browse files
authored
fix the embedded null character issue
Merge pull request #492 from Distributive-Network/Xmader/fix-embedded-null-char
2 parents dcfc126 + e3410ed commit 0a4df74

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/StrType.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ PyObject *StrType::proxifyString(JSContext *cx, JS::HandleValue strVal) {
135135

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

139147
PY_UNICODE_OBJECT_DATA_ANY(pyString) = (void *)chars;
140148
PY_UNICODE_OBJECT_KIND(pyString) = PyUnicode_1BYTE_KIND;
@@ -189,6 +197,11 @@ PyObject *StrType::proxifyString(JSContext *cx, JS::HandleValue strVal) {
189197
Py_DECREF(pyString);
190198
return ucs4Obj;
191199
}
200+
if ((PY_VERSION_HEX) >= 0x030d0000) { // Python 3.13+, fix `ValueError: embedded null character`
201+
PyObject *copied = PyUnicode_FromKindAndData(PyUnicode_2BYTE_KIND, chars, length); // create a copy of the string buffer
202+
Py_DECREF(pyString);
203+
return copied;
204+
}
192205
}
193206

194207
return (PyObject *)pyString;

0 commit comments

Comments
 (0)