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

Don't die when user adds enumerable properties to Array.prototype #1

Conversation

@maxedmands
Copy link

maxedmands commented May 30, 2014

I was wondering why Joi was failing randomly in some code I was working on, and then I found this hidden away in the depths:

Array.prototype.toArray = function() { return this; }

Topo was tripping up on it while enumerating over an array (it wasn't expecting to find a the toArray property!).

I know, it's a code smell to add enumerable properties to global objects, but it happens sometimes. This pull request will make Topo a little bit more resilient in the wide world of Javascript userland.

I know, nobody should be doing this in the first place, but it still
happens sometimes. This pull request will make Topo a little bit
more resilient in the wide world of Javascript userland.
@@ -90,6 +90,7 @@ internals.Topo.prototype._sort = function () {

var expandedGroups = [];
for (var groupIndex in graph[node]) {
if(!graph[node].propertyIsEnumerable(groupIndex)) break;

This comment has been minimized.

Copy link
@arb

arb Jul 31, 2014

Contributor

I think the standard way of doing this is like graph[node].hasOwnproperty(groupIndex).Does that have the same effect?

Also, break will terminate the loop early. You need continue. Finally, I think the normal approach for for ... in loop is

if (object.hasOwnproperty(key) {...}
@hueniverse hueniverse closed this in 42213ae Aug 3, 2014
@hueniverse hueniverse added the bug label Aug 3, 2014
@hueniverse hueniverse added this to the 1.0.1 milestone Aug 3, 2014
@hueniverse hueniverse self-assigned this Aug 3, 2014
@maxedmands

This comment has been minimized.

Copy link
Author

maxedmands commented Aug 3, 2014

Hooray, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.