Skip to content

DataView dw in encode could incorporate .byteLength & .byteOffset #2

@gaconnet

Description

@gaconnet

Hi. Nice library!

Encodings on Uint8Array subarrays yield incorrect results:

> encode(new Uint8Array([0x00, 0x86, 0x4F, 0xD2, 0x6F, 0xB5, 0x59, 0xF7, 0x5B]).slice(1)) // ✅
< 'HelloWorld'
> encode(new Uint8Array([0x00, 0x86, 0x4F, 0xD2, 0x6F, 0xB5, 0x59, 0xF7, 0x5B]).subarray(1)) // ❌
< '0esp$z)-:x'

I think if you pass the ui8a.byteOffset and ui8a.byteLength through to the DataView dw it will work for both use cases.

This can happen when z85 encoding multiple results from cbor-x encoding, which returns subarrays into a larger underlying encoding buffer:

> CBOR.encode('Hello')
< Uint8Array(6) [101, 72, 101, 108, 108, 111, buffer: ArrayBuffer(8192), byteLength: 6, byteOffset: 4674,
> CBOR.encode('World')
< Uint8Array(6) [101, 87, 111, 114, 108, 100, buffer: ArrayBuffer(8192), byteLength: 6, byteOffset: 4680,

I won't share how long it took me to figure out exactly what was going on here… 😳🙊

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions