stdlib-js / stdlib-js/stdlib

@stdlib/array/fixed-endian-factory allows misaligned byte offsets and fractional lengths

オープン 初心者向け
#15,193 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
JavaScript
スター
6k
フォーク
1.3k
平均マージ
1日 3時間
マージ済み PR(30日)
611

説明

### Description

I noticed that `@stdlib/array/fixed-endian-factory` accepts `ArrayBuffer` byte offsets that are not aligned to the element size.

For example, `Float32` values use 4 bytes per element, but the constructor currently accepts a `byteOffset` of `2`.

That can lead to fractional array lengths, which seems incorrect and also differs from native `TypedArray` behavior.

### Reproduction

```js
const Float32ArrayFE = require( '@stdlib/array/fixed-endian-float32' );

const buf = new ArrayBuffer( 16 );
const arr = new Float32ArrayFE( 'little-endian', buf, 2 );

console.log( arr.length );
console.log( arr.byteLength );
console.log( arr.byteOffset );
```

Output:

```text
3.5
14
2
```

The native equivalent:

```js
new Float32Array( new ArrayBuffer( 16 ), 2 );
```

throws a `RangeError` because the offset is not aligned to the 4-byte element size.

I also noticed a similar case when the offset itself is aligned, but the remaining number of bytes is not divisible by the element size:

```js
const Float32ArrayFE = require( '@stdlib/array/fixed-endian-float32' );

const arr = new Float32ArrayFE(
'little-endian',
new ArrayBuffer( 10 ),
4
);

console.log( arr.length );
```

This returns:

```text
1.5
```

while native `Float32Array` rejects the equivalent construction.

### Expected behavior

I would expect the constructor to reject byte offsets that are not aligned to `BYTES_PER_ELEMENT`.

And when no explicit length is provided, the remaining buffer size should also be divisible by `BYTES_PER_ELEMENT`.

That would prevent cases where the array ends up with a fractional `length`.

The relevant code seems to be in:

```text
lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js
```

Would matching native `TypedArray` alignment behavior be the expected behavior here?

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

Read lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js and reproduce the two Float32 examples from the issue. Compare the constructor behavior with native TypedArray alignment rules; done means invalid offsets and non-divisible remaining byte sizes are rejected instead of producing fractional lengths.

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript
領域
data
issue の種類
バグ
難易度
2/5
見積もり時間
1〜3時間
活発さ
活発
明瞭さ
明確に書かれている
初心者へのやさしさ
82/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。