@stdlib/array/fixed-endian-factory allows misaligned byte offsets and fractional lengths
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 82/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- javascript
- Domain
- data
Research direction
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.
Written by the indexing model from the issue text.
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
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?
- Dominant language
- JavaScript
- Stars
- 6k
- Forks
- 1.3k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 559
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from stdlib-js/stdlib
-
Good First Issue
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
Good First Issue
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
Good First Issue
Difficulty 1/5 Under an hour Newbie friendliness 88/100
-
Good First Issue
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Fix C lint errors Open
Difficulty 1/5 Under an hour Newbie friendliness 85/100
All issues in stdlib-js/stdlib
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
HarperFast/skills#96 ·
-
[Block] Latest Posts [Type] Bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Automattic/studio#4908 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
sugarlabs/musicblocks#8847 ·