Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LRUCache throws "Invalid array length" as an error if float value is passed to it #165

Closed
trivikr opened this issue May 19, 2021 · 3 comments · Fixed by #166
Closed

LRUCache throws "Invalid array length" as an error if float value is passed to it #165

trivikr opened this issue May 19, 2021 · 3 comments · Fixed by #166

Comments

@trivikr
Copy link
Contributor

trivikr commented May 19, 2021

Describe the issue

LRUCache throws "Invalid array length" as an error if float value is passed to it.

Steps to reproduce

Run the following code with mnemonist@0.38.3

const LRUCache = require("mnemonist/lru-cache");
const cache = new LRUCache(1.01);

Observed behavior

The following error is thrown:

  this.K = typeof Keys === 'function' ? new Keys(capacity) : new Array(capacity);
                                                             ^

RangeError: Invalid array length
    at new LRUCache (/Users/trivikr/workspace/lru-cache/node_modules/mnemonist/lru-cache.js:47:62)
    at Object.<anonymous> (/Users/trivikr/workspace/lru-cache/index.js:3:15)
    at Module._compile (internal/modules/cjs/loader.js:1068:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Module.load (internal/modules/cjs/loader.js:933:32)
    at Function.Module._load (internal/modules/cjs/loader.js:774:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
    at internal/main/run_main_module.js:17:47

Expected behavior

A custom error can be thrown on this line instead after adding a check for integer:

mnemonist/lru-cache.js

Lines 40 to 41 in 825538a

if (typeof this.capacity !== 'number' || this.capacity <= 0)
throw new Error('mnemonist/lru-cache: capacity should be positive number.');

@trivikr
Copy link
Contributor Author

trivikr commented May 19, 2021

Example fix without changing the prior error:

  if (typeof this.capacity !== 'number' || this.capacity <= 0)
    if (!isFinite(this.capacity) || Math.floor(this.capacity) !== this.capacity)
      throw new Error('mnemonist/lru-cache: capacity should be a finite positive integer.');
    throw new Error('mnemonist/lru-cache: capacity should be positive number.');

@Yomguithereal
Copy link
Owner

Hello @trivikr. Looks good to me. Do you want to open a PR with this change?

@trivikr
Copy link
Contributor Author

trivikr commented May 20, 2021

Do you want to open a PR with this change?

PR posted at #166

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants