From d22d923931b1c2cd567fe34835439ed99a7b7314 Mon Sep 17 00:00:00 2001 From: brawner Date: Thu, 1 Oct 2020 16:25:16 -0700 Subject: [PATCH] Don't overwrite cur_ns pointer if reallocation fails (#780) (#783) Signed-off-by: Stephen Brawner --- rcl_yaml_param_parser/src/parser.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/rcl_yaml_param_parser/src/parser.c b/rcl_yaml_param_parser/src/parser.c index b7c8542..d294b5a 100644 --- a/rcl_yaml_param_parser/src/parser.c +++ b/rcl_yaml_param_parser/src/parser.c @@ -214,10 +214,11 @@ static rcutils_ret_t add_name_to_ns( tot_len = ns_len + sep_len + name_len + 1U; - cur_ns = allocator.reallocate(cur_ns, tot_len, allocator.state); - if (NULL == cur_ns) { + char * tmp_ns_ptr = allocator.reallocate(cur_ns, tot_len, allocator.state); + if (NULL == tmp_ns_ptr) { return RCUTILS_RET_BAD_ALLOC; } + cur_ns = tmp_ns_ptr; 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'; @@ -284,10 +285,11 @@ static rcutils_ret_t rem_name_from_ns( } 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) { + char * tmp_ns_ptr = allocator.reallocate(cur_ns, tot_len, allocator.state); + if (NULL == tmp_ns_ptr) { return RCUTILS_RET_BAD_ALLOC; } + cur_ns = tmp_ns_ptr; cur_ns[tot_len - 1U] = '\0'; } }