Skip to content

Commit 5f1e543

Browse files
estevedirk-thomas
authored andcommitted
Avoid C4703 error on UWP (ros2#282)
Signed-off-by: Esteve Fernandez <esteve@apache.org>
1 parent 926da81 commit 5f1e543

File tree

1 file changed

+71
-79
lines changed

1 file changed

+71
-79
lines changed

rcl_yaml_param_parser/src/parser.c

Lines changed: 71 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ static rcutils_ret_t add_name_to_ns(
168168
size_t sep_len;
169169
size_t tot_len;
170170

171-
rcutils_ret_t ret = RCUTILS_RET_OK;
172171
switch (namespace_type) {
173172
case NS_TYPE_NODE:
174173
cur_ns = ns_tracker->node_ns;
@@ -181,54 +180,51 @@ static rcutils_ret_t add_name_to_ns(
181180
sep_str = PARAMETER_NS_SEPERATOR;
182181
break;
183182
default:
184-
ret = RCUTILS_RET_ERROR;
185-
break;
183+
return RCUTILS_RET_ERROR;
186184
}
187185

188-
if (RCUTILS_RET_OK == ret) {
189-
/// Add a name to ns
190-
if (NULL == name) {
191-
return RCUTILS_RET_INVALID_ARGUMENT;
186+
/// Add a name to ns
187+
if (NULL == name) {
188+
return RCUTILS_RET_INVALID_ARGUMENT;
189+
}
190+
if (0U == *cur_count) {
191+
cur_ns = rcutils_strdup(name, allocator);
192+
if (NULL == cur_ns) {
193+
return RCUTILS_RET_BAD_ALLOC;
194+
}
195+
} else {
196+
ns_len = strlen(cur_ns);
197+
name_len = strlen(name);
198+
sep_len = strlen(sep_str);
199+
// Check the last sep_len characters of the current NS against the separator string.
200+
if (strcmp(cur_ns + ns_len - sep_len, sep_str) == 0) {
201+
// Current NS already ends with the separator: don't put another separator in.
202+
sep_len = 0;
203+
sep_str = "";
192204
}
193-
if (0U == *cur_count) {
194-
cur_ns = rcutils_strdup(name, allocator);
195-
if (NULL == cur_ns) {
196-
return RCUTILS_RET_BAD_ALLOC;
197-
}
198-
} else {
199-
ns_len = strlen(cur_ns);
200-
name_len = strlen(name);
201-
sep_len = strlen(sep_str);
202-
// Check the last sep_len characters of the current NS against the separator string.
203-
if (strcmp(cur_ns + ns_len - sep_len, sep_str) == 0) {
204-
// Current NS already ends with the separator: don't put another separator in.
205-
sep_len = 0;
206-
sep_str = "";
207-
}
208205

209-
tot_len = ns_len + sep_len + name_len + 1U;
206+
tot_len = ns_len + sep_len + name_len + 1U;
210207

211-
if (tot_len > MAX_STRING_SIZE) {
212-
RCUTILS_SET_ERROR_MSG("New namespace string is exceeding max string size");
213-
return RCUTILS_RET_ERROR;
214-
}
215-
cur_ns = allocator.reallocate(cur_ns, tot_len, allocator.state);
216-
if (NULL == cur_ns) {
217-
return RCUTILS_RET_BAD_ALLOC;
218-
}
219-
memmove((cur_ns + ns_len), sep_str, sep_len);
220-
memmove((cur_ns + ns_len + sep_len), name, name_len);
221-
cur_ns[tot_len - 1U] = '\0';
208+
if (tot_len > MAX_STRING_SIZE) {
209+
RCUTILS_SET_ERROR_MSG("New namespace string is exceeding max string size");
210+
return RCUTILS_RET_ERROR;
222211
}
223-
*cur_count = (*cur_count + 1U);
224-
225-
if (NS_TYPE_NODE == namespace_type) {
226-
ns_tracker->node_ns = cur_ns;
227-
} else {
228-
ns_tracker->parameter_ns = cur_ns;
212+
cur_ns = allocator.reallocate(cur_ns, tot_len, allocator.state);
213+
if (NULL == cur_ns) {
214+
return RCUTILS_RET_BAD_ALLOC;
229215
}
216+
memmove((cur_ns + ns_len), sep_str, sep_len);
217+
memmove((cur_ns + ns_len + sep_len), name, name_len);
218+
cur_ns[tot_len - 1U] = '\0';
230219
}
231-
return ret;
220+
*cur_count = (*cur_count + 1U);
221+
222+
if (NS_TYPE_NODE == namespace_type) {
223+
ns_tracker->node_ns = cur_ns;
224+
} else {
225+
ns_tracker->parameter_ns = cur_ns;
226+
}
227+
return RCUTILS_RET_OK;
232228
}
233229

234230
///
@@ -245,7 +241,6 @@ static rcutils_ret_t rem_name_from_ns(
245241
size_t ns_len;
246242
size_t tot_len;
247243

248-
rcutils_ret_t ret = RCUTILS_RET_OK;
249244
switch (namespace_type) {
250245
case NS_TYPE_NODE:
251246
cur_ns = ns_tracker->node_ns;
@@ -258,50 +253,47 @@ static rcutils_ret_t rem_name_from_ns(
258253
sep_str = PARAMETER_NS_SEPERATOR;
259254
break;
260255
default:
261-
ret = RCUTILS_RET_ERROR;
262-
break;
256+
return RCUTILS_RET_ERROR;
263257
}
264258

265-
if (RCUTILS_RET_OK == ret) {
266-
/// Remove last name from ns
267-
if (*cur_count > 0U) {
268-
if (1U == *cur_count) {
269-
allocator.deallocate(cur_ns, allocator.state);
270-
cur_ns = NULL;
271-
} else {
272-
ns_len = strlen(cur_ns);
273-
char * last_idx = NULL;
274-
char * next_str = NULL;
275-
const char * end_ptr = (cur_ns + ns_len);
276-
277-
next_str = strstr(cur_ns, sep_str);
278-
while (NULL != next_str) {
279-
if (next_str > end_ptr) {
280-
RCUTILS_SET_ERROR_MSG("Internal error. Crossing arrau boundary");
281-
return RCUTILS_RET_ERROR;
282-
}
283-
last_idx = next_str;
284-
next_str = (next_str + strlen(sep_str));
285-
next_str = strstr(next_str, sep_str);
259+
/// Remove last name from ns
260+
if (*cur_count > 0U) {
261+
if (1U == *cur_count) {
262+
allocator.deallocate(cur_ns, allocator.state);
263+
cur_ns = NULL;
264+
} else {
265+
ns_len = strlen(cur_ns);
266+
char * last_idx = NULL;
267+
char * next_str = NULL;
268+
const char * end_ptr = (cur_ns + ns_len);
269+
270+
next_str = strstr(cur_ns, sep_str);
271+
while (NULL != next_str) {
272+
if (next_str > end_ptr) {
273+
RCUTILS_SET_ERROR_MSG("Internal error. Crossing arrau boundary");
274+
return RCUTILS_RET_ERROR;
286275
}
287-
if (NULL != last_idx) {
288-
tot_len = ((size_t)(last_idx - cur_ns) + 1U);
289-
cur_ns = allocator.reallocate(cur_ns, tot_len, allocator.state);
290-
if (NULL == cur_ns) {
291-
return RCUTILS_RET_BAD_ALLOC;
292-
}
293-
cur_ns[tot_len - 1U] = '\0';
276+
last_idx = next_str;
277+
next_str = (next_str + strlen(sep_str));
278+
next_str = strstr(next_str, sep_str);
279+
}
280+
if (NULL != last_idx) {
281+
tot_len = ((size_t)(last_idx - cur_ns) + 1U);
282+
cur_ns = allocator.reallocate(cur_ns, tot_len, allocator.state);
283+
if (NULL == cur_ns) {
284+
return RCUTILS_RET_BAD_ALLOC;
294285
}
286+
cur_ns[tot_len - 1U] = '\0';
295287
}
296-
*cur_count = (*cur_count - 1U);
297-
}
298-
if (NS_TYPE_NODE == namespace_type) {
299-
ns_tracker->node_ns = cur_ns;
300-
} else {
301-
ns_tracker->parameter_ns = cur_ns;
302288
}
289+
*cur_count = (*cur_count - 1U);
303290
}
304-
return ret;
291+
if (NS_TYPE_NODE == namespace_type) {
292+
ns_tracker->node_ns = cur_ns;
293+
} else {
294+
ns_tracker->parameter_ns = cur_ns;
295+
}
296+
return RCUTILS_RET_OK;
305297
}
306298

307299
///

0 commit comments

Comments
 (0)