Commit 9d101d12 authored by George Nash's avatar George Nash Committed by Kishen Maloor

String to endpoint fix parse error if uri is NULL

The start of the uri path must still be calculated even if uri is
NULL (i.e. user is not interested in parsing the path component of
the endpoint). This must be calculated since this value is used
in later calculations for finding the port number.

See lines 421-425 the `u` variable must be calculated for the code
to properly calculate `p`. Variable `p` must be accurate or the
address_len will be incorrect resulting in an incorrect string
being sent for DNS lookup. Resulting in an error even for valid
endpoint strings.

With out this fix:
`coap://` and
`coaps+tcp://` would both return errors
when the uri param was NULL.

With this fix those endpoint strings are properly parsed.
Signed-off-by: George Nash's avatarGeorge Nash <>
parent d2546a95
Pipeline #94 passed with stage
in 46 seconds
......@@ -383,8 +383,8 @@ oc_parse_endpoint_string(oc_string_t *endpoint_str, oc_endpoint_t *endpoint,
/* Extract a uri path if requested and available */
const char *u = NULL;
u = memchr(address, '/', len);
if (uri) {
u = memchr(address, '/', len);
if (u) {
oc_new_string(uri, u, (len - (u - address)));
......@@ -154,5 +154,21 @@ TEST(OCEndpoints, StringToEndpoint)
// test dns lookup when uri is NULL
const char *spu3[4] = { "coap://",
"coaps://" };
for (int i = 0; i < 4; i++) {
oc_string_t s;
oc_new_string(&s, spu3[i], strlen(spu[i]));
oc_endpoint_t ep;
memset(&ep, 0, sizeof(oc_endpoint_t));
int ret = oc_string_to_endpoint(&s, &ep, NULL);
EXPECT_EQ(ret, 0) << "spu3[" << i << "] " << spu3[i];
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment