Skip to content

Commit

Permalink
Avoid C4703 error on UWP (#282)
Browse files Browse the repository at this point in the history
Signed-off-by: Esteve Fernandez <esteve@apache.org>
  • Loading branch information
esteve authored and dirk-thomas committed Aug 1, 2019
1 parent 926da81 commit 5f1e543
Showing 1 changed file with 71 additions and 79 deletions.
150 changes: 71 additions & 79 deletions rcl_yaml_param_parser/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ static rcutils_ret_t add_name_to_ns(
size_t sep_len;
size_t tot_len;

rcutils_ret_t ret = RCUTILS_RET_OK;
switch (namespace_type) {
case NS_TYPE_NODE:
cur_ns = ns_tracker->node_ns;
Expand All @@ -181,54 +180,51 @@ static rcutils_ret_t add_name_to_ns(
sep_str = PARAMETER_NS_SEPERATOR;
break;
default:
ret = RCUTILS_RET_ERROR;
break;
return RCUTILS_RET_ERROR;
}

if (RCUTILS_RET_OK == ret) {
/// Add a name to ns
if (NULL == name) {
return RCUTILS_RET_INVALID_ARGUMENT;
/// Add a name to ns
if (NULL == name) {
return RCUTILS_RET_INVALID_ARGUMENT;
}
if (0U == *cur_count) {
cur_ns = rcutils_strdup(name, allocator);
if (NULL == cur_ns) {
return RCUTILS_RET_BAD_ALLOC;
}
} else {
ns_len = strlen(cur_ns);
name_len = strlen(name);
sep_len = strlen(sep_str);
// Check the last sep_len characters of the current NS against the separator string.
if (strcmp(cur_ns + ns_len - sep_len, sep_str) == 0) {
// Current NS already ends with the separator: don't put another separator in.
sep_len = 0;
sep_str = "";
}
if (0U == *cur_count) {
cur_ns = rcutils_strdup(name, allocator);
if (NULL == cur_ns) {
return RCUTILS_RET_BAD_ALLOC;
}
} else {
ns_len = strlen(cur_ns);
name_len = strlen(name);
sep_len = strlen(sep_str);
// Check the last sep_len characters of the current NS against the separator string.
if (strcmp(cur_ns + ns_len - sep_len, sep_str) == 0) {
// Current NS already ends with the separator: don't put another separator in.
sep_len = 0;
sep_str = "";
}

tot_len = ns_len + sep_len + name_len + 1U;
tot_len = ns_len + sep_len + name_len + 1U;

if (tot_len > MAX_STRING_SIZE) {
RCUTILS_SET_ERROR_MSG("New namespace string is exceeding max string size");
return RCUTILS_RET_ERROR;
}
cur_ns = allocator.reallocate(cur_ns, tot_len, allocator.state);
if (NULL == cur_ns) {
return RCUTILS_RET_BAD_ALLOC;
}
memmove((cur_ns + ns_len), sep_str, sep_len);
memmove((cur_ns + ns_len + sep_len), name, name_len);
cur_ns[tot_len - 1U] = '\0';
if (tot_len > MAX_STRING_SIZE) {
RCUTILS_SET_ERROR_MSG("New namespace string is exceeding max string size");
return RCUTILS_RET_ERROR;
}
*cur_count = (*cur_count + 1U);

if (NS_TYPE_NODE == namespace_type) {
ns_tracker->node_ns = cur_ns;
} else {
ns_tracker->parameter_ns = cur_ns;
cur_ns = allocator.reallocate(cur_ns, tot_len, allocator.state);
if (NULL == cur_ns) {
return RCUTILS_RET_BAD_ALLOC;
}
memmove((cur_ns + ns_len), sep_str, sep_len);
memmove((cur_ns + ns_len + sep_len), name, name_len);
cur_ns[tot_len - 1U] = '\0';
}
return ret;
*cur_count = (*cur_count + 1U);

if (NS_TYPE_NODE == namespace_type) {
ns_tracker->node_ns = cur_ns;
} else {
ns_tracker->parameter_ns = cur_ns;
}
return RCUTILS_RET_OK;
}

///
Expand All @@ -245,7 +241,6 @@ static rcutils_ret_t rem_name_from_ns(
size_t ns_len;
size_t tot_len;

rcutils_ret_t ret = RCUTILS_RET_OK;
switch (namespace_type) {
case NS_TYPE_NODE:
cur_ns = ns_tracker->node_ns;
Expand All @@ -258,50 +253,47 @@ static rcutils_ret_t rem_name_from_ns(
sep_str = PARAMETER_NS_SEPERATOR;
break;
default:
ret = RCUTILS_RET_ERROR;
break;
return RCUTILS_RET_ERROR;
}

if (RCUTILS_RET_OK == ret) {
/// Remove last name from ns
if (*cur_count > 0U) {
if (1U == *cur_count) {
allocator.deallocate(cur_ns, allocator.state);
cur_ns = NULL;
} else {
ns_len = strlen(cur_ns);
char * last_idx = NULL;
char * next_str = NULL;
const char * end_ptr = (cur_ns + ns_len);

next_str = strstr(cur_ns, sep_str);
while (NULL != next_str) {
if (next_str > end_ptr) {
RCUTILS_SET_ERROR_MSG("Internal error. Crossing arrau boundary");
return RCUTILS_RET_ERROR;
}
last_idx = next_str;
next_str = (next_str + strlen(sep_str));
next_str = strstr(next_str, sep_str);
/// Remove last name from ns
if (*cur_count > 0U) {
if (1U == *cur_count) {
allocator.deallocate(cur_ns, allocator.state);
cur_ns = NULL;
} else {
ns_len = strlen(cur_ns);
char * last_idx = NULL;
char * next_str = NULL;
const char * end_ptr = (cur_ns + ns_len);

next_str = strstr(cur_ns, sep_str);
while (NULL != next_str) {
if (next_str > end_ptr) {
RCUTILS_SET_ERROR_MSG("Internal error. Crossing arrau boundary");
return RCUTILS_RET_ERROR;
}
if (NULL != last_idx) {
tot_len = ((size_t)(last_idx - cur_ns) + 1U);
cur_ns = allocator.reallocate(cur_ns, tot_len, allocator.state);
if (NULL == cur_ns) {
return RCUTILS_RET_BAD_ALLOC;
}
cur_ns[tot_len - 1U] = '\0';
last_idx = next_str;
next_str = (next_str + strlen(sep_str));
next_str = strstr(next_str, sep_str);
}
if (NULL != last_idx) {
tot_len = ((size_t)(last_idx - cur_ns) + 1U);
cur_ns = allocator.reallocate(cur_ns, tot_len, allocator.state);
if (NULL == cur_ns) {
return RCUTILS_RET_BAD_ALLOC;
}
cur_ns[tot_len - 1U] = '\0';
}
*cur_count = (*cur_count - 1U);
}
if (NS_TYPE_NODE == namespace_type) {
ns_tracker->node_ns = cur_ns;
} else {
ns_tracker->parameter_ns = cur_ns;
}
*cur_count = (*cur_count - 1U);
}
return ret;
if (NS_TYPE_NODE == namespace_type) {
ns_tracker->node_ns = cur_ns;
} else {
ns_tracker->parameter_ns = cur_ns;
}
return RCUTILS_RET_OK;
}

///
Expand Down

0 comments on commit 5f1e543

Please sign in to comment.