Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bufferify doesn't handle Uint8Array properly #86

Open
mjbrisebois opened this issue Aug 3, 2023 · 0 comments · May be fixed by #87
Open

bufferify doesn't handle Uint8Array properly #86

mjbrisebois opened this issue Aug 3, 2023 · 0 comments · May be fixed by #87

Comments

@mjbrisebois
Copy link

https://github.com/merkletreejs/merkletreejs/blob/master/src/Base.ts#L173

The code assumes the buffer only contains the bytes of the ArrayBuffer view, but the Uint8Array buffer could be larger than the view. Buffer.from should also include the offset and length.

       } else if (value instanceof Uint8Array) {
-        return Buffer.from(value.buffer)
+        return Buffer.from(value.buffer, value.byteOffset, value.byteLength)
       } else if (typeof value === 'number') {

Example of Buffer.from(arrayBuffer[, byteOffset[, length]]) from https://nodejs.org/api/buffer.html#static-method-bufferfromarraybuffer-byteoffset-length

import { Buffer } from 'node:buffer';

const arrA = Uint8Array.from([0x63, 0x64, 0x65, 0x66]); // 4 elements
const arrB = new Uint8Array(arrA.buffer, 1, 2); // 2 elements
console.log(arrA.buffer === arrB.buffer); // true

const buf = Buffer.from(arrB.buffer);
console.log(buf);
// Prints: <Buffer 63 64 65 66>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant