Skip to content

setUint8 throws when ArrayBuffer is resizable #988

Closed
@guest271314

Description

@guest271314

This should work

import * as std from "qjs:std";
import * as os from "qjs:os";

const buffer = new ArrayBuffer(0, {maxByteLength: 16384});
const view = new DataView(buffer);
const arr = [97, 98, 99];
const file = os.open("test.txt", os.O_RDWR | os.O_CREAT | os.O_TRUNC, 0o764);
buffer.resize(arr.length);
console.log(buffer.byteLength);
for (let i = 0; i < arr.length; i++) {
  console.log(`setUint8(${i}, ${arr[i]})`);
  view.setUint8(i, arr[i]);
}
console.log(os.write(file, buffer, 0, buffer.byteLength));
console.log(os.close(file));

I get

3
setUint8(0, 97)
Possibly unhandled promise rejection: RangeError: out of bound
    at setUint8 (native)
    at <anonymous> (test.js:15:17)

Looks like a bug in DataView reading resizable ArrayBuffer.

This works as expected

import * as std from "qjs:std";
import * as os from "qjs:os";

const buffer = new ArrayBuffer(3);
const view = new DataView(buffer);
const arr = [97, 98, 99];
const file = os.open("test.txt", os.O_RDWR | os.O_CREAT | os.O_TRUNC, 0o764);
//buffer.resize(arr.length);
console.log(buffer.byteLength);
for (let i = 0; i < arr.length; i++) {
  console.log(`setUint8(${i}, ${arr[i]})`);
  view.setUint8(i, arr[i]);
}
console.log(os.write(file, buffer, 0, buffer.byteLength));
console.log(os.close(file));
3
setUint8(0, 97)
setUint8(1, 98)
setUint8(2, 99)
3
0
ls -l test.txt
-rwxr--r-- 1 user user 3 Mar 20 05:09 test.txt

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