stdlib-js / stdlib-js/stdlib

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

Ouverte Adaptée aux débutants
#15,193 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Langage dominant
JavaScript
Étoiles
6k
Forks
1.3k
Merge moyen
1 j 3 h
PR mergées (30 j)
611

Description

### 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?

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Lisez lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js et reproduisez les deux exemples Float32 de l’issue. Comparez le comportement du constructeur aux règles natives d’alignement de TypedArray ; le travail est terminé lorsque les offsets invalides et les tailles d’octets restantes non divisibles sont rejetés au lieu de produire des longueurs fractionnaires.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
javascript
Domaine
data
Type d'issue
Bug
Difficulté
2/5
Temps estimé
1-3 heures
Activité
Active
Clarté
Clairement spécifiée
Accessibilité débutants
82/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.