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? |
| @@ -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.
I think it might even get a little clearer now if you make this unsigned
There was a problem hiding this comment.
Yes, makes sense. Changed.
| 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.
nit: the port > 0xffff could be moved into the for-loop head
There was a problem hiding this comment.
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? |
There was a problem hiding this comment.
I think the counter here is just failureTests.length, there is no need to hard-code this
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 <anna@addaleax.net> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
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 <anna@addaleax.net> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
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 <anna@addaleax.net> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
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