Skip to content

Commit 6849db8

Browse files
committed
fix the embedded null character issue
SpiderMonkey doesn't store the extra null character while some Python APIs assume the string buffer is null-terminated. The issue hasn't been a problem before because it somehow didn't allocate the buffer that follows, making the string buffer null-terminated effectively.
1 parent c5b0dda commit 6849db8

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/StrType.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@ 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 (chars[length] != 0) { // not a null-terminated string
139+
// most Python C APIs assume the string buffer is null-terminated, so we need to create a copy
140+
PyObject *copied = PyUnicode_FromObject(pyString); // create a copy when it's not a true Unicode object
141+
Py_DECREF(pyString);
142+
return copied;
143+
}
138144

139145
PY_UNICODE_OBJECT_DATA_ANY(pyString) = (void *)chars;
140146
PY_UNICODE_OBJECT_KIND(pyString) = PyUnicode_1BYTE_KIND;
@@ -157,6 +163,11 @@ PyObject *StrType::proxifyString(JSContext *cx, JS::HandleValue strVal) {
157163
}
158164
else { // utf16 spidermonkey, ucs2 python
159165
const char16_t *chars = JS::GetTwoByteLinearStringChars(nogc, lstr);
166+
if (chars[length] != 0) { // not a null-terminated string
167+
PyObject *copied = PyUnicode_FromObject(pyString);
168+
Py_DECREF(pyString);
169+
return copied;
170+
}
160171

161172
PY_UNICODE_OBJECT_DATA_ANY(pyString) = (void *)chars;
162173
PY_UNICODE_OBJECT_KIND(pyString) = PyUnicode_2BYTE_KIND;

0 commit comments

Comments
 (0)