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

Abierto Apto para principiantes
#15,193 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
82/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
javascript
Área
data

Línea de trabajo

Lee lib/node_modules/@stdlib/array/fixed-endian-factory/lib/main.js y reproduce los dos ejemplos de Float32 del issue. Compara el comportamiento del constructor con las reglas nativas de alineación de TypedArray; la tarea está terminada cuando se rechazan los offsets no válidos y los tamaños de bytes restantes que no son divisibles, en lugar de producir longitudes fraccionarias.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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
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:

3.5
14
2

The native equivalent:

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:

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

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

console.log( arr.length );

This returns:

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:

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

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

Lenguaje dominante
JavaScript
Estrellas
6k
Forks
1.3k
Merge medio
1 d 1 h
PR fusionados (30 d)
559

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de stdlib-js/stdlib

Todos los issues de stdlib-js/stdlib

Issues similares

Más issues de JavaScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.