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

Guard against infinite loops #39

Merged
merged 1 commit into from
Oct 20, 2015
Merged

Guard against infinite loops #39

merged 1 commit into from
Oct 20, 2015

Conversation

ForbesLindesay
Copy link
Contributor

This guards against infinite loops using the halting-problem module.

It catches simple cases like the following statically:

export default function ({Plugin, types: t}) {
  return new Plugin('ast-transform', {
    visitor: {
      Identifier(node) {
        var n = 100;
        while (true) n++;
        return t.identifier(node.name.split('').reverse().join(''));
      }
    }
  });
}

results in

while statement on line 6 has a constant test. Either it is always false (making the statement redundant) or it is an infinite loop.

It can catch more subtle infinite loops like the following by checking for a timeout (of say 5 seconds) in all loops. In this case it will just give you a message of "Infinite loop detected on line 6"

export default function ({Plugin, types: t}) {
  return new Plugin('ast-transform', {
    visitor: {
      Identifier(node) {
        var n = 100;
        while (n > 0) n++;
        return t.identifier(node.name.split('').reverse().join(''));
      }
    }
  });
}

@fkling
Copy link
Owner

fkling commented Oct 20, 2015

This is great, thank you!

@fkling fkling merged commit d5b75e9 into fkling:master Oct 20, 2015
@kyldvs
Copy link

kyldvs commented Oct 20, 2015

Awesome!

@cpojer
Copy link
Contributor

cpojer commented Oct 20, 2015

This is awesome, thank you @ForbesLindesay !

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 this pull request may close these issues.

None yet

4 participants