oven-sh/bun

CString.byteLength & CString.byteOffset are undefined

Opened this issue · 3 comments

What version of Bun is running?

1.2.23

What platform is your computer?

Microsoft Windows NT 10.0.26100.0 x64

What steps can reproduce the bug?

const buffer = Buffer.from('Hello world!');
const bufferPtr = ptr(buffer);

const cString = new CString(bufferPtr);

console.log(cString.byteLength, cString.byteOffset); // undefined undefined

What is the expected behavior?

console.log(cString.byteLength, cString.byteOffset); // 12 0

What do you see instead?

console.log(cString.byteLength, cString.byteOffset); // undefined undefined

Additional information

I don't believe that CString.byteLength and CString.byteOffset being undefined is intended behavior. As far as I know, CString is the only struct that has this odd behavior out of the following:

type Scratch = BigInt64Array | BigUint64Array | Buffer | CString | Float32Array | Float64Array | DataView | Int16Array | Int32Array | Int8Array | Uint16Array | Uint8Array | Uint8ClampedArray | Uint32Array;

This is incorrect usage of CString. The buffer needs to have a null terminator:

-const buffer = Buffer.from('Hello world!');
+const buffer = Buffer.from('Hello world!\x00');
const bufferPtr = ptr(buffer);

const cString = new CString(bufferPtr);

The missing byteOffset and byteLength properties are a bug, currently we only set those if they are defined in the constructur but instead we should unconditionally set those properties and update the type definition file to make them non-nullable.

Thank you for the pointer, @pfgithub! I didn't realize I had to provide the null. That explains some odd behaviour I was seeing with my code last night!

Thanks again for the clarification about the bug!