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

Add deallocate calls to free strdup allocated memory #737

Merged
merged 4 commits into from
Aug 11, 2020
Merged
Changes from 2 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
30 changes: 25 additions & 5 deletions rcl/src/rcl/rmw_implementation_identifier_check.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ rcl_ret_t rcl_rmw_implementation_identifier_check(void)
// If the environment variable RMW_IMPLEMENTATION is set, or
// the environment variable RCL_ASSERT_RMW_ID_MATCHES is set,
// check that the result of `rmw_get_implementation_identifier` matches.
rcl_ret_t fail_state = RCL_RET_OK;
rcl_allocator_t allocator = rcl_get_default_allocator();
char * expected_rmw_impl = NULL;
bool expected_rmw_requires_free = false;
Blast545 marked this conversation as resolved.
Show resolved Hide resolved
const char * expected_rmw_impl_env = NULL;
const char * get_env_error_str = rcutils_get_env(
RMW_IMPLEMENTATION_ENV_VAR_NAME,
Expand All @@ -78,26 +80,31 @@ rcl_ret_t rcl_rmw_implementation_identifier_check(void)
RCL_SET_ERROR_MSG("allocation failed");
return RCL_RET_BAD_ALLOC;
}
expected_rmw_requires_free = true;
}

char * asserted_rmw_impl = NULL;
const char * asserted_rmw_impl_env = NULL;
bool asserted_rmw_requires_free = false;
Blast545 marked this conversation as resolved.
Show resolved Hide resolved
get_env_error_str = rcutils_get_env(
RCL_ASSERT_RMW_ID_MATCHES_ENV_VAR_NAME, &asserted_rmw_impl_env);
if (NULL != get_env_error_str) {
RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(
"Error getting env var '"
RCUTILS_STRINGIFY(RCL_ASSERT_RMW_ID_MATCHES_ENV_VAR_NAME) "': %s\n",
get_env_error_str);
return RCL_RET_ERROR;
fail_state = RCL_RET_ERROR;
goto fail_cleanup;
}
if (strlen(asserted_rmw_impl_env) > 0) {
// Copy the environment variable so it doesn't get over-written by the next getenv call.
asserted_rmw_impl = rcutils_strdup(asserted_rmw_impl_env, allocator);
if (!asserted_rmw_impl) {
RCL_SET_ERROR_MSG("allocation failed");
return RCL_RET_BAD_ALLOC;
fail_state = RCL_RET_BAD_ALLOC;
goto fail_cleanup;
}
asserted_rmw_requires_free = true;
}

// If both environment variables are set, and they do not match, print an error and exit.
Expand All @@ -107,7 +114,8 @@ rcl_ret_t rcl_rmw_implementation_identifier_check(void)
"variables do not match, exiting with %d.",
expected_rmw_impl, asserted_rmw_impl, RCL_RET_ERROR
);
return RCL_RET_ERROR;
fail_state = RCL_RET_ERROR;
goto fail_cleanup;
}

// Collapse the expected_rmw_impl and asserted_rmw_impl variables so only expected_rmw_impl needs
Expand All @@ -116,6 +124,7 @@ rcl_ret_t rcl_rmw_implementation_identifier_check(void)
// The strings at this point must be equal.
// No need for asserted_rmw_impl anymore, free the memory.
allocator.deallocate((char *)asserted_rmw_impl, allocator.state);
Blast545 marked this conversation as resolved.
Show resolved Hide resolved
asserted_rmw_requires_free = false;
Blast545 marked this conversation as resolved.
Show resolved Hide resolved
} else {
// One or none are set.
// If asserted_rmw_impl has contents, move it over to expected_rmw_impl.
Expand All @@ -137,7 +146,8 @@ rcl_ret_t rcl_rmw_implementation_identifier_check(void)
rmw_error_msg.str,
RCL_RET_ERROR
);
return RCL_RET_ERROR;
fail_state = RCL_RET_ERROR;
goto fail_cleanup;
}
if (strcmp(actual_rmw_impl_id, expected_rmw_impl) != 0) {
RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(
Expand All @@ -146,12 +156,22 @@ rcl_ret_t rcl_rmw_implementation_identifier_check(void)
actual_rmw_impl_id,
RCL_RET_MISMATCHED_RMW_ID
);
return RCL_RET_MISMATCHED_RMW_ID;
fail_state = RCL_RET_MISMATCHED_RMW_ID;
goto fail_cleanup;
}
// Free the memory now that all checking has passed.
allocator.deallocate((char *)expected_rmw_impl, allocator.state);
}
return RCL_RET_OK;

fail_cleanup:
if (expected_rmw_requires_free) {
allocator.deallocate((char *)expected_rmw_impl, allocator.state);
}
if (asserted_rmw_requires_free) {
allocator.deallocate((char *)asserted_rmw_impl, allocator.state);
}
return fail_state;
ivanpauno marked this conversation as resolved.
Show resolved Hide resolved
}

INITIALIZER(initialize) {
Expand Down