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

Infinite loop when generating local entropy #6

Open
josdejong opened this issue Apr 17, 2017 · 0 comments
Open

Infinite loop when generating local entropy #6

josdejong opened this issue Apr 17, 2017 · 0 comments

Comments

@josdejong
Copy link

I stumbled upon an issue where calling seed() causes an (almost) infinite recursive loop:

  • seed() is called without arguments, in which case it uses a local entropy. This entropy is generated (amongst others) from all globally defined objects.
  • seed() is called via a getter on an object which is globally defined (obj.random in the example).
  • hence, when creating the local entropy, seed calls the obj.random getter which calls seed again -> infinite loop

node.js example code:

var seed = require('seed-random');

var count = 0;

// create an object
var obj = {};

// expose the object globally
global.obj = obj;

Object.defineProperty(obj, 'random', {
  get: function () {
    console.log('obj.random getter');
    count++;

    // call seed inside a getter of the globally defined object
    return seed();
  },
  configurable: true,
  enumerable: true
});

// call the getter... will cause an (almost) infinite loop
var a = obj.random();
console.log('a', a);

console.log('number of recursive loops over obj.random (should be 1):', count); 
// outputs for example count = 1425

Some ideas for a solution:

  • Ignore properties which are getters when creating local entropy
  • Output an informative warning when this loop happens (check whether seed is called whilst it's being executed)
  • Throw an error when seed is called whilst it is being executed, and when generating local entropy, catch this error and silently ignore it.
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

No branches or pull requests

1 participant