Skip to content

[Bug]: inserting Uint8Array subarray view in a BLOB misbehaves #398

@Stypox

Description

@Stypox

What happened?

When inserting a subarray of an Uint8Array in a BLOB field, the whole original array gets stored, instead of the subarray. This can cause high confusion especially in combination with other libraries that operate on binary data and return subarrays of the input data (e.g. cbor2).

I feel like the problem lies in to_variant(): the memcpy range should probably be changed to take into account the array buffer view's range over the original allocation. https://github.com/OP-Engineering/op-sqlite/blob/main/cpp/utils.cpp#L108

The following code, which typechecks in TypeScript and runs without errors, should print [98, 99] but instead prints [97, 98, 99, 100]. You can also reproduce with the attached Minimum Reproducible Example.

const db = open(/* ... */)
await db.executeRaw("CREATE TABLE MyTable (data BLOB NOT NULL) STRICT")

const arr = new Uint8Array([97,98,99,100])
const subarr = arr.subarray(1,3)

console.log(subarr) // [98, 99]
await db.executeRaw("INSERT INTO MyTable (data) VALUES (?)", [subarr])
const res = (await db.executeRaw("SELECT data FROM MyTable"))[0][0]
console.log(new Uint8Array(res)) // [97, 98, 99, 100]

A temporary workaround to achieve the correct behavior is to insert the wrapped new Uint8Array(subarr) instead of the plain subarr.

Finally, thank you for your efforts on this library!

op-sqlite version

15.2.11 with sqlcipher on Android

React Native version

19.1.0

Reproducible Example

https://github.com/user-attachments/files/26775317/project.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions