-
Notifications
You must be signed in to change notification settings - Fork 30k
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
dgram: prefer strict equality #8011
dgram: prefer strict equality #8011
Conversation
Strictly speaking, this would be semver-major... console.log(['udp4'] == 'udp4'); Now this will not work... |
@thefourtheye indeed, good point! All the tests passed on my machine, so I'm guessing there's no tests for that use case? In the docs here https://nodejs.org/api/dgram.html#dgram_dgram_createsocket_type_callback the type is "string". Are there any examples of |
@claudiorodriguez I don't have solid evidence that people use something like that. Perhaps the impact might not be that big and may probably be characterized as a bug-fix. I am not very sure though. @nodejs/collaborators thoughts? |
LGTM. I'm fine with semver patch. |
This would need to be a semver-major if it landed as is even if passing
|
-1 to adding an additional array check. We're already catering to undocumented/unsupported behavior here. I'd rather see it become a semver major change than to add code for a ridiculous edge case. |
Works for me as a semver-major On Monday, August 8, 2016, Colin Ihrig notifications@github.com wrote:
|
I just realized that there's also
but that would defeat the entire purpose of the strict equality. So I'm also +1 on major. |
Thinking about it further, we could always force a coercion... e.g. |
BTW, should we add cases to cover these strict equations? |
@yorkie it would make sense for |
LGTM |
LGTM with semver-major. |
a5ba154
to
8360593
Compare
While writing the test I noticed that currently |
8360593
to
a24d87d
Compare
@@ -78,7 +78,7 @@ exports._createSocketHandle = function(address, port, addressType, fd, flags) { | |||
function Socket(type, listener) { | |||
EventEmitter.call(this); | |||
|
|||
if (typeof type === 'object') { | |||
if (type && typeof type === 'object') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It actually is if the proper error message is to be thrown. Otherwise it fails with null
with a "cannot access property type of null" message (typeof null
is 'object'
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, +1 on your comment :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explicitly check for null
in this check.
LGTM |
CI failure seems unrelated. |
@mcollina @JacksonTian @cjihrig are you okay with the latest changes? |
Can you please squash the commits? |
a24d87d
to
9da93c0
Compare
Done |
Ok for me. @claudiorodriguez are you a collaborator? Otherwise I'll merge it. |
@mcollina ... do you know if there are any significant npm modules that depend on dgram that we can test with CITGM for this change? I'd be more comfortable signing off if we could get at least one good citgm run but I'm not sure any of our existing modules there actually use dgram. |
https://github.com/mafintosh/k-rpc-socket (powers the torrent modules) I'll also cc @mafintosh, a couple of his p2p modules can definitely be in there. I own coap, but it has a couple of flaky tests. |
LGTM with a comment. |
9da93c0
to
3e1e974
Compare
- Enforces strict comparisons in dgram - bindState should always be strictly equal to one of the defined constant states, and newHandle type is a string. - Check that the argument `type` in createSocket is not null when it is of type 'object', before using its `type` property. - Adds a test to check dgram.createSocket is properly validating its `type` argument. PR-URL: nodejs#8011
3e1e974
to
daf6284
Compare
@cjihrig thanks, changed |
@jasnell I will also include https://www.npmjs.com/package/multicast-dns. |
New CI: https://ci.nodejs.org/job/node-test-commit/4807/ Is there anything keeping this from landing? |
@jasnell said #8011 (comment). @addaleax IMHO we should land this in v7. |
@@ -78,7 +78,7 @@ exports._createSocketHandle = function(address, port, addressType, fd, flags) { | |||
function Socket(type, listener) { | |||
EventEmitter.call(this); | |||
|
|||
if (typeof type === 'object') { | |||
if (type !== null && typeof type === 'object') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think V8 likes it better to have typeof
checks come first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@addaleax do you have any docs on this? Not saying it's not the case - I don't really know myself, but a quick google search throws nothing, and in the lib codebase both cases seem to be equally prevalent:
Line 53 in be68b68
function createConnection(port, host, options) { |
Line 111 in 013d76c
if (hostname && typeof hostname !== 'string') { |
vs
Line 172 in 6979632
if (typeof v === 'number' && isFinite(v)) |
Line 63 in b7a8a69
if (typeof n !== 'number' || n < 0 || isNaN(n)) |
if we can confirm one case is better, this could mean future improvement PRs and more consistency over the codebase
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@claudiorodriguez I’ll try to look it up/benchmark it, all I could remember right now is emberjs/ember.js#14118 … might be a different case with !== null
, but even so I think I’d find the typeof
-first variants more readable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@claudiorodriguez This does indeed seem to be different with !== null
. You can feel free to disregard my comment :)
Expressions of the form a && typeof a === 'object'
definitely seem to be a reason for deopts, though.
ack, I’m attaching the milestone to it. |
@jasnell we are waiting for you, you said you wanted solid coverage in CITGM before landing. May I merge, or what else should I do to make this progress? Everybody is in agreement here. |
I think we should be good to go. We can verify the citgm stuff as we go. |
Cool – @claudiorodriguez do you want to go ahead and land this? |
Landed in e9b6fbb |
- Enforces strict comparisons in dgram - bindState should always be strictly equal to one of the defined constant states, and newHandle type is a string. - Check that the argument `type` in createSocket is not null when it is of type 'object', before using its `type` property. - Adds a test to check dgram.createSocket is properly validating its `type` argument. PR-URL: #8011 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Yorkie Liu <yorkiefixer@gmail.com> Reviewed-By: Jackson Tian <shvyo1987@gmail.com>
Checklist
make -j4 test
(UNIX), orvcbuild test nosign
(Windows) passesAffected core subsystem(s)
dgram
Description of change
Enforces strict comparisons in dgram.
bindState should always be strictly equal to one of the defined constant states, and newHandle type is a string.