From: Alexander Borisov Date: Mon, 7 Sep 2020 14:55:24 +0000 (+0300) Subject: Fixed TextDecoder.prototype.decode() with non-zero TypedArray offset. X-Git-Tag: 0.4.4~10 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=f9d4fa4466fea0a2d908ead78028ee1d2139fe15;p=njs.git Fixed TextDecoder.prototype.decode() with non-zero TypedArray offset. --- diff --git a/src/njs_encoding.c b/src/njs_encoding.c index 3cc7cb63..01d28af1 100644 --- a/src/njs_encoding.c +++ b/src/njs_encoding.c @@ -214,7 +214,7 @@ njs_text_encoder_encode_into(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, end = start + str.length; array = njs_typed_array(dest); - to = njs_typed_array_buffer(array)->u.u8; + to = njs_typed_array_start(array); to_end = to + array->byte_length; cp = 0; @@ -564,7 +564,7 @@ njs_text_decoder_decode(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, if (njs_is_typed_array(value)) { array = njs_typed_array(value); - start = array->buffer->u.u8; + start = njs_typed_array_start(array); end = start + array->byte_length; } else if (njs_is_array_buffer(value)) { diff --git a/src/njs_typed_array.c b/src/njs_typed_array.c index be73e508..e23fd596 100644 --- a/src/njs_typed_array.c +++ b/src/njs_typed_array.c @@ -610,8 +610,7 @@ njs_typed_array_prototype_byte_offset(njs_vm_t *vm, njs_value_t *args, array = njs_typed_array(this); - njs_set_number(&vm->retval, array->offset - * njs_typed_array_element_size(array->type)); + njs_set_number(&vm->retval, njs_typed_array_offset(array)); return NJS_OK; } diff --git a/src/njs_typed_array.h b/src/njs_typed_array.h index 7e31a7db..78db7665 100644 --- a/src/njs_typed_array.h +++ b/src/njs_typed_array.h @@ -55,6 +55,20 @@ njs_typed_array_length(const njs_typed_array_t *array) } +njs_inline uint32_t +njs_typed_array_offset(const njs_typed_array_t *array) +{ + return array->offset * njs_typed_array_element_size(array->type); +} + + +njs_inline u_char * +njs_typed_array_start(const njs_typed_array_t *array) +{ + return &array->buffer->u.u8[njs_typed_array_offset(array)]; +} + + njs_inline double njs_typed_array_prop(const njs_typed_array_t *array, uint32_t index) { diff --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c index ed59118a..4da0bc0b 100644 --- a/src/test/njs_unit_test.c +++ b/src/test/njs_unit_test.c @@ -18226,6 +18226,11 @@ static njs_unit_test_t njs_test[] = "var res = en.encodeInto('ααααα', utf8); njs.dump(res)"), njs_str("{read:5,written:10}") }, + { njs_str("var en = new TextEncoder();" + "var utf8 = new Uint8Array(10);" + "en.encodeInto('ααααα', utf8.subarray(2)); utf8[0]"), + njs_str("0") }, + { njs_str("var str = String.bytesFrom([0xCE]);" "var en = new TextEncoder();" "var utf8 = new Uint8Array(3);" @@ -18333,6 +18338,12 @@ static njs_unit_test_t njs_test[] = "var en = new TextEncoder();" "njs.dump(en.encode(de.decode(buf)))"), njs_str("Uint8Array [1,0,0,0,2,0,0,0,3,0,0,0]") }, + + { njs_str("var de = new TextDecoder();" + "var buf = new Uint32Array([1,2,3]).subarray(1,2);" + "var en = new TextEncoder();" + "njs.dump(en.encode(de.decode(buf)))"), + njs_str("Uint8Array [2,0,0,0]") }, };