-
Notifications
You must be signed in to change notification settings - Fork 2k
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
net/sock_util: Accept NULL pointers in urlsplit #11677
net/sock_util: Accept NULL pointers in urlsplit #11677
Conversation
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.
Just a couple of details. Otherwise it is OK.
@@ -116,30 +117,35 @@ static char* _find_pathstart(const char *url) | |||
|
|||
int sock_urlsplit(const char *url, char *hostport, char *urlpath) | |||
{ | |||
assert(url); |
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.
assert(url); | |
assert(url != NULL); |
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.
please keep the original authors style where it doesn't go against coding conventions.
sys/net/sock/sock_util.c
Outdated
return -EOVERFLOW; | ||
} | ||
memcpy(hostport, hoststart, hostlen); | ||
*(hostport + hostlen) = '\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.
*(hostport + hostlen) = '\0'; | |
hostport[hostlen] = '\0'; |
Note: i know you didn't write that, but considering there are not many chances to do little fixes like this...
* terminator */ | ||
if (hostlen > SOCK_HOSTPORT_MAXLEN - 1) { | ||
return -EOVERFLOW; | ||
if (hostport) { |
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.
if (hostport) { | |
if (hostport != NULL) { |
if (pathlen) { | ||
if (pathlen > SOCK_URLPATH_MAXLEN - 1) { | ||
return -EOVERFLOW; | ||
if (urlpath) { |
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.
if (urlpath) { | |
if (urlpath != NULL) { |
sys/net/sock/sock_util.c
Outdated
return -EOVERFLOW; | ||
if (urlpath) { | ||
size_t pathlen = strlen(pathstart); | ||
if (pathlen) { |
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.
This outer "if" is not necessary.
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 removing the if (pathlen) {
statement introduces possible undefined behaviour with the memcpy()
(in the case pathlen
equals 0
) below, unless you can show that pathlen
here is always greater than 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 can't see the issue here, isn't it valid to pass 0 length to memcpy?
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.
Never mind, I think I confused myself here. IIRC it is undefined to call it with NULL
as one of the arguments, even when the passed length is 0, but that's not the case here.
sys/net/sock/sock_util.c
Outdated
} | ||
memcpy(urlpath, pathstart, pathlen); | ||
*(urlpath + pathlen) = '\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.
*(urlpath + pathlen) = '\0'; | |
urlpath[pathlen] = '\0'; |
same as above, since you are you are touching the code, why not fix this too.
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.
otherwise tested pre-ACK, works!
@@ -116,30 +117,33 @@ static char* _find_pathstart(const char *url) | |||
|
|||
int sock_urlsplit(const char *url, char *hostport, char *urlpath) | |||
{ | |||
assert(url != NULL); | |||
char *hoststart = _find_hoststart(url); | |||
if (!hoststart) { |
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.
as you changed the others this should be if (hoststart == NULL) {
accordingly, or change back as suggested by @kaspar030 - but be consistent
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.
Changed back to original style
sys/net/sock/sock_util.c
Outdated
@@ -116,30 +117,33 @@ static char* _find_pathstart(const char *url) | |||
|
|||
int sock_urlsplit(const char *url, char *hostport, char *urlpath) | |||
{ | |||
assert(url != NULL); |
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.
this addition should be reflected in the documentation as follows:
@param[in] url URL to split. Not Null.
[...]
@pre `url != NULL`
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.
Added
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.
re-tested and ACK again. Please squash and amend wording suggested by @miri64.
@jcarrano please have a look again |
6609ab9
to
864247d
Compare
ack and go |
Thanks for reviewing! |
Contribution description
This PR modifies the implementation of
sock_urlsplit
so it can accept NULL pointers for either the address and/or the path part. This way it's more flexible to use, as one does not have to allocate a buffer for both parts if one of them is not needed.There are also two new unit tests for testing this conditions.
Testing procedure
Run the unit tests:
Issues/PRs references
None