From 5a5bdad67854af27273087fc1e8103ed70420bd9 Mon Sep 17 00:00:00 2001 From: Sami Vaarala Date: Fri, 12 Jun 2020 00:16:14 +0300 Subject: [PATCH] Fix CBOR.encode() assertion failure --- src-input/duk_bi_cbor.c | 8 +++----- .../test-bug-assert-cbor-encode-gh2316.js | 13 +++++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) create mode 100644 tests/ecmascript/test-bug-assert-cbor-encode-gh2316.js diff --git a/src-input/duk_bi_cbor.c b/src-input/duk_bi_cbor.c index f12f7952..021e5ccb 100644 --- a/src-input/duk_bi_cbor.c +++ b/src-input/duk_bi_cbor.c @@ -487,7 +487,7 @@ DUK_LOCAL void duk__cbor_encode_object(duk_cbor_encode_context *enc_ctx) { duk__cbor_encode_uint32(enc_ctx, (duk_uint32_t) len, 0x40U); duk__cbor_encode_ensure(enc_ctx, len); p = enc_ctx->ptr; - duk_memcpy((void *) p, (const void *) buf, len); + duk_memcpy_unsafe((void *) p, (const void *) buf, len); p += len; enc_ctx->ptr = p; } else { @@ -542,7 +542,7 @@ DUK_LOCAL void duk__cbor_encode_buffer(duk_cbor_encode_context *enc_ctx) { duk__cbor_encode_uint32(enc_ctx, (duk_uint32_t) len, 0x40U); duk__cbor_encode_ensure(enc_ctx, len); p = enc_ctx->ptr; - duk_memcpy((void *) p, (const void *) buf, len); + duk_memcpy_unsafe((void *) p, (const void *) buf, len); p += len; enc_ctx->ptr = p; } @@ -945,9 +945,7 @@ DUK_LOCAL void duk__cbor_decode_join_buffers(duk_cbor_decode_context *dec_ctx, d buf_data = (duk_uint8_t *) duk_require_buffer(dec_ctx->thr, idx, &buf_size); if (p != NULL) { - if (buf_size > 0U) { - duk_memcpy((void *) p, (const void *) buf_data, buf_size); - } + duk_memcpy_unsafe((void *) p, (const void *) buf_data, buf_size); p += buf_size; } else { total_size += buf_size; diff --git a/tests/ecmascript/test-bug-assert-cbor-encode-gh2316.js b/tests/ecmascript/test-bug-assert-cbor-encode-gh2316.js new file mode 100644 index 00000000..30793ab8 --- /dev/null +++ b/tests/ecmascript/test-bug-assert-cbor-encode-gh2316.js @@ -0,0 +1,13 @@ +// https://github.com/svaarala/duktape/issues/2316 + +/*=== +done +===*/ + +function main() { + var v0 = []; + var v4 = Duktape.dec("base64",v0); + var v5 = CBOR.encode(v4); +} +main(); +print('done');