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

cb may be undefined as well #11848

Closed
wants to merge 1 commit into from
Closed

cb may be undefined as well #11848

wants to merge 1 commit into from

Conversation

vincent-tr
Copy link

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
Affected core subsystem(s)

net (Socket.protype.connect)

@nodejs-github-bot nodejs-github-bot added the net Issues and PRs related to the net subsystem. label Mar 14, 2017
@@ -954,7 +954,7 @@ Socket.prototype.connect = function() {
initSocketHandle(this);
}

if (cb !== null) {
if (cb) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the best approach is probably to check if typeof cb === 'function'.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was also going to suggest that. Yes.

Copy link
Author

@vincent-tr vincent-tr Mar 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to check if cb is undefined, to respect API contract: net.connect(options[, connectListener])

I don't think that net.connect(options, 42) or net.connect(options, 'anything')
should silently ignore the second parameter without failure, what do you think ?

@joyeecheung
Copy link
Member

Duplicate of #11762?

@joyeecheung
Copy link
Member

BTW: normalizeArgs is supposed to always normalize the callback to null if it's not defined.

@Trott
Copy link
Member

Trott commented Mar 15, 2017

If we do go with this, it needs a test I think.

@Trott
Copy link
Member

Trott commented Mar 15, 2017

@vincent-tr Thanks for taking the time to submit this PR!

@vincent-tr and @joyeecheung: Since #11762 landed, does that mean this PR obsolete should be closed?

@cjihrig
Copy link
Contributor

cjihrig commented Mar 15, 2017

Since #11762 landed, does that mean this PR obsolete should be closed?

I think changing to an explicit typeof === 'function' check is still the correct thing to do. It's future proof, in case normalizeArgs() changes.

@joyeecheung
Copy link
Member

joyeecheung commented Mar 15, 2017

@Trott #11762 also added a test, I don't think there are more tests this PR can add if it's only to make sure cb can be undefined (because now it's always normalized, so when it gets to this point cb can not be undefined anymore).

If it changes to typeof === 'function' as @cjihrig suggests then this is still worth merging, IMO. But I still can't see any more tests this PR need..

@vincent-tr
Copy link
Author

@Trott @joyeecheung having a look at #11762 it handles indeed this case.
But I still think net.connect(options, 42) ornet.connect(options, 'anything') should fail instead of ignore the second parameter.

@joyeecheung
Copy link
Member

joyeecheung commented Mar 15, 2017

@vincent-tr I agree letting things like net.connect(options, 42) pass is a bit weird, but I suspect there are people doing .apply or .call(...args) on it/server.listen() without much checking, so if we enforce these signatures the breakage could be bigger than expected..right now any arguments before cb in (options, cb), (port[, host], cb), (path, cb), or placed as the last one but are not functions, are ignored.

https://github.com/search?l=JavaScript&q=net.connect.apply&type=Code&utf8=%E2%9C%93
https://github.com/search?l=JavaScript&q=server.listen.apply&type=Code&utf8=%E2%9C%93

@vincent-tr
Copy link
Author

True. Closing then, Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
net Issues and PRs related to the net subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants