Previously, qjs_text_encoder_encode_into() extracted the destination
buffer pointer and length with qjs_typed_array_data() before calling
qjs_is_uint8_array(). Because qjs_is_uint8_array() looks up
globalThis.Uint8Array and calls JS_IsInstanceOf(), a getter or a
Symbol.hasInstance handler could detach or transfer the underlying
ArrayBuffer, so the subsequent UTF-8 encoding loop wrote through a
stale pointer. In addition, an exception thrown during the check was
ignored: encodeInto() returned a result with the exception left
pending.
Perform the Uint8Array check and propagate its exceptions before
extracting the destination buffer pointer and length.
The issue was introduced in
447d66d4 (0.8.10).
This closes #1177 issue on Github.
qjs_text_encoder_encode_into(JSContext *cx, JSValueConst this_val, int argc,
JSValueConst *argv)
{
- int read, written;
+ int rc, read, written;
void *te;
size_t size;
u_char *to, *to_end;
return JS_ThrowTypeError(cx, "The input argument must be a string");
}
+ rc = qjs_is_uint8_array(cx, argv[1]);
+ if (rc <= 0) {
+ if (rc == 0) {
+ return JS_ThrowTypeError(cx, "The output argument must be a"
+ " Uint8Array");
+ }
+
+ return JS_EXCEPTION;
+ }
+
ret = qjs_typed_array_data(cx, argv[1], &dst);
if (JS_IsException(ret)) {
return ret;
}
- if (!qjs_is_uint8_array(cx, argv[1])) {
- return JS_ThrowTypeError(cx, "The output argument must be a"
- " Uint8Array");
- }
-
utf8.start = (u_char *) JS_ToCStringLen(cx, &utf8.length, argv[0]);
if (utf8.start == NULL) {
return JS_EXCEPTION;
T: async (params) => {
let td = new TextEncoder();
- let uint8 = new Uint8Array(10);
-
- detach(uint8.buffer);
+ let uint8 = new Uint8Array(params.size || 10);
+
+ if (params.hasInstance) {
+ Object.defineProperty(Uint8Array, Symbol.hasInstance, {
+ value: () => {
+ if (params.throws) {
+ throw new TypeError('hasInstance');
+ }
+
+ detach(uint8.buffer);
+ return true;
+ },
+ configurable: true
+ });
+
+ } else {
+ detach(uint8.buffer);
+ }
try {
td.encodeInto("test", uint8);
} else {
throw e;
}
+
+ } finally {
+ delete Uint8Array[Symbol.hasInstance];
}
throw Error('Expected TypeError not thrown');
tests: [
{ },
+ { hasInstance: true, size: 4096,
+ skip: () => (typeof njs == 'undefined' || njs.engine != 'QuickJS') },
+ { hasInstance: true, throws: true,
+ skip: () => (typeof njs == 'undefined' || njs.engine != 'QuickJS') },
],
};