-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
url: fix port overflow checking #15794
Conversation
This patch adds (port > 0xffff) check after each digit in the loop and prevents integer overflow.
Maybe only bother checking if the buffer length is > 4? I think that will avoid having to check for most common ports. |
@mscdex I think your proposal complicates code a bit, and a gain is minimal - only three comparisons less for 4-digit port : int port = 0;
if (buffer.size() <= 4) {
// port overflow will not occur
for (size_t i = 0; i < buffer.size(); i++)
port = port * 10 + buffer[i] - '0';
} else {
for (size_t i = 0; i < buffer.size(); i++) {
port = port * 10 + buffer[i] - '0';
// prevent integer overflow
if (port > 0xffff) break;
}
} What do you think? |
src/node_url.cc
Outdated
@@ -1598,9 +1598,12 @@ void URL::Parse(const char* input, | |||
special_back_slash) { | |||
if (buffer.size() > 0) { | |||
int port = 0; |
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 it might even get a little clearer now if you make this unsigned
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.
Yes, makes sense. Changed.
src/node_url.cc
Outdated
port = port * 10 + buffer[i] - '0'; | ||
if (port < 0 || port > 0xffff) { | ||
// prevent integer overflow | ||
if (port > 0xffff) break; |
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.
nit: the port > 0xffff
could be moved into the for
-loop head
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.
Seems reasonable, moved.
There are CI failures like this:
I guess that’s just a mismatched test counter that needs to be updated or something like that? |
@@ -26,7 +26,7 @@ const failureTests = tests.filter((test) => test.failure).concat([ | |||
]); | |||
|
|||
const expectedError = common.expectsError( | |||
{ code: 'ERR_INVALID_URL', type: TypeError }, 110); | |||
{ code: 'ERR_INVALID_URL', type: TypeError }, 113); |
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 the counter here is just failureTests.length
, there is no need to hard-code this
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.
agree, done.
f43d979
to
5d3cc27
Compare
Landed in 92146e0. |
This patch adds (port > 0xffff) check after each digit in the loop and prevents integer overflow. PR-URL: #15794 Refs: web-platform-tests/wpt#7602 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Daijiro Wachi <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
This patch adds (port > 0xffff) check after each digit in the loop and prevents integer overflow. PR-URL: nodejs/node#15794 Refs: web-platform-tests/wpt#7602 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Daijiro Wachi <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
This patch adds (port > 0xffff) check after each digit in the loop and prevents integer overflow. PR-URL: #15794 Refs: web-platform-tests/wpt#7602 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Daijiro Wachi <[email protected]> Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Joyee Cheung <[email protected]>
This patch adds
(port > 0xffff)
check after each digit in the loop and prevents integer overflow.In the current implementation a result can be incorrect if an integer overflow occurs. For example:
Refs: web-platform-tests/wpt#7602
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
url