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

net/sock_util: Accept NULL pointers in urlsplit #11677

Merged
merged 2 commits into from
Jun 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions sys/include/net/sock/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@ int sock_udp_ep_fmt(const sock_udp_ep_t *endpoint, char *addr_str, uint16_t *por
*
* @note Caller has to make sure hostport and urlpath can hold the results!
* Make sure to provide space for @ref SOCK_HOSTPORT_MAXLEN respectively
* @ref SOCK_URLPATH_MAXLEN bytes.
* @ref SOCK_URLPATH_MAXLEN bytes, if pointers are not NULL.
* Scheme part of the URL is limited to @ref SOCK_SCHEME_MAXLEN length.
*
* @param[in] url URL to split
* @param[out] hostport where to write host:port
* @param[out] urlpath where to write url path
* @pre `url != NULL`
*
* @param[in] url URL to split. Must not be NULL.
* @param[out] hostport where to write host:port. Can be NULL.
* @param[out] urlpath where to write url path. Can be NULL.
*
* @returns 0 on success
* @returns <0 otherwise
Expand Down
24 changes: 14 additions & 10 deletions sys/net/sock/sock_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "net/sock/udp.h"
#include "net/sock/util.h"
Expand Down Expand Up @@ -116,30 +117,33 @@ static char* _find_pathstart(const char *url)

int sock_urlsplit(const char *url, char *hostport, char *urlpath)
{
assert(url);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
assert(url);
assert(url != NULL);

Copy link
Contributor

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.

char *hoststart = _find_hoststart(url);
if (!hoststart) {
Copy link
Member

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

Copy link
Contributor Author

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

return -EINVAL;
}

char *pathstart = _find_pathstart(hoststart);

size_t hostlen = pathstart - hoststart;
/* hostlen must be smaller SOCK_HOSTPORT_MAXLEN to have space for the null
* terminator */
if (hostlen > SOCK_HOSTPORT_MAXLEN - 1) {
return -EOVERFLOW;
if (hostport) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (hostport) {
if (hostport != NULL) {

size_t hostlen = pathstart - hoststart;
/* hostlen must be smaller SOCK_HOSTPORT_MAXLEN to have space for the null
* terminator */
if (hostlen > SOCK_HOSTPORT_MAXLEN - 1) {
return -EOVERFLOW;
}
memcpy(hostport, hoststart, hostlen);
hostport[hostlen] = '\0';
}
memcpy(hostport, hoststart, hostlen);
*(hostport + hostlen) = '\0';

size_t pathlen = strlen(pathstart);
if (pathlen) {
if (urlpath) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (urlpath) {
if (urlpath != NULL) {

size_t pathlen = strlen(pathstart);
if (pathlen > SOCK_URLPATH_MAXLEN - 1) {
return -EOVERFLOW;
}
memcpy(urlpath, pathstart, pathlen);
urlpath[pathlen] = '\0';
}
*(urlpath + pathlen) = '\0';
return 0;
}

Expand Down
14 changes: 14 additions & 0 deletions tests/unittests/tests-sock_util/tests-sock_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@ static void test_sock_util_urlsplit__urlpath_too_long(void)
sock_urlsplit(TEST_URL_LONG_URLPATH, addr, urlpath));
}

static void test_sock_util_urlsplit__null_addr_buffer(void)
{
TEST_ASSERT_EQUAL_INT(0, sock_urlsplit(TEST_URL, addr, NULL));
TEST_ASSERT_EQUAL_STRING(TEST_URL_HOSTPART, (char*)addr);
}

static void test_sock_util_urlsplit__null_path_buffer(void)
{
TEST_ASSERT_EQUAL_INT(0, sock_urlsplit(TEST_URL, NULL, urlpath));
TEST_ASSERT_EQUAL_STRING(TEST_URL_LOCALPART, (char*)urlpath);
}

static void test_sock_util_str2ep__ipv6_noport(void)
{
sock_udp_ep_t ep;
Expand Down Expand Up @@ -210,6 +222,8 @@ Test *tests_sock_util_all(void)
new_TestFixture(test_sock_util_urlsplit__no_schema),
new_TestFixture(test_sock_util_urlsplit__hostport_too_long),
new_TestFixture(test_sock_util_urlsplit__urlpath_too_long),
new_TestFixture(test_sock_util_urlsplit__null_addr_buffer),
new_TestFixture(test_sock_util_urlsplit__null_path_buffer),
new_TestFixture(test_sock_util_str2ep__ipv6_noport),
new_TestFixture(test_sock_util_str2ep__ipv4_noport),
new_TestFixture(test_sock_util_str2ep__ipv4_port),
Expand Down