emmanueltouzery / prelude-ts

Functional programming, immutable collections and FP constructs for typescript and javascript
ISC License
377 stars 21 forks source link

"Cannot read property '0' of undefined" on repeated Vector.appendAll #17

Closed bwbuchanan closed 6 years ago

bwbuchanan commented 6 years ago

This code:

import { Vector } from 'prelude.ts';

let v = Vector.ofIterable(Array(15).fill(1));

for (let i = 0; i < 100; i += 1) {
  console.log(i);
  v = v.appendAll(Array(1000).fill(1));
}

Will fail after 86 iterations with:

TypeError: Cannot read property '0' of undefined
    at Vector.getLastNode (node_modules/prelude.ts/src/Vector.ts:279:31)
    at Vector.appendAllArrays (node_modules/prelude.ts/src/Vector.ts:457:34)
    at Vector.appendAll (node_modules/prelude.ts/src/Vector.ts:419:25)
bwbuchanan commented 6 years ago

Simpler test case:

import { Vector } from 'prelude.ts';

Vector.ofIterable(Array(86015).fill(1)).appendAll([1]);
bwbuchanan commented 6 years ago

3071 is another magic length that will break appendAll.

Both 3071 and 86015 have the property: N % 32 === 31 && Math.floor(N / 32) % 32 === 31

emmanueltouzery commented 6 years ago

Thank you for the report! I'll look into this tonight. You're not really lucky with prelude bugs.. note that I just added a fuzzer test on master exactly to detect such issues.

bwbuchanan commented 6 years ago

Well, I'm using it in a production system, so that tends to find all the edge cases! :)

emmanueltouzery commented 6 years ago

I released 0.7.13 with the fix! Thank you for the patience and the detailed bug reports!