Fix crash in DOMJIT call with empty input

This commit is contained in:
Jarred Sumner
2023-01-23 04:58:42 -08:00
parent f9306ff815
commit 45296570ce
2 changed files with 30 additions and 0 deletions

View File

@@ -1868,6 +1868,10 @@ JSC__JSValue ZigString__to16BitValue(const ZigString* arg0, JSC__JSGlobalObject*
JSC__JSValue ZigString__toExternalU16(const uint16_t* arg0, size_t len, JSC__JSGlobalObject* global)
{
if (len == 0) {
return JSC::JSValue::encode(JSC::jsEmptyString(global->vm()));
}
auto ref = String(ExternalStringImpl::create(reinterpret_cast<const UChar*>(arg0), len, reinterpret_cast<void*>(const_cast<uint16_t*>(arg0)), free_global_string));
return JSC::JSValue::encode(JSC::JSValue(JSC::jsString(
@@ -1876,7 +1880,12 @@ JSC__JSValue ZigString__toExternalU16(const uint16_t* arg0, size_t len, JSC__JSG
// This must be a globally allocated string
JSC__JSValue ZigString__toExternalValue(const ZigString* arg0, JSC__JSGlobalObject* arg1)
{
ZigString str = *arg0;
if (str.len == 0) {
return JSC::JSValue::encode(JSC::jsEmptyString(arg1->vm()));
}
if (Zig::isTaggedUTF16Ptr(str.ptr)) {
auto ref = String(ExternalStringImpl::create(reinterpret_cast<const UChar*>(Zig::untag(str.ptr)), str.len, Zig::untagVoid(str.ptr), free_global_string));

View File

@@ -14,6 +14,27 @@ const getByteLength = (str) => {
};
describe("TextDecoder", () => {
it("should not crash on empty text", () => {
const decoder = new TextDecoder();
gcTrace(true);
const fixtures = [
new Uint8Array(),
new Uint8Array([]),
new Buffer(0),
new ArrayBuffer(0),
];
for (let input of fixtures) {
expect(decoder.decode(input)).toBe("");
}
// DOMJIT test
for (let i = 0; i < 90000; i++) {
decoder.decode(fixtures[0]);
}
gcTrace(true);
});
it("should decode ascii text", () => {
const decoder = new TextDecoder("latin1");
gcTrace(true);