From 32bcd0d76072d717332fdb4579dbeb14fea4747c Mon Sep 17 00:00:00 2001 From: Mikael Arguedas Date: Thu, 27 Oct 2016 19:00:06 -0700 Subject: [PATCH] Zero init topic names and types (#85) * initialize topic_names_and_type struct in rcl * sanity check in get_topic_names_and_types * uncrustify * initialize all fields... * address documentation request * typo --- rcl/include/rcl/graph.h | 10 ++++++++-- rcl/include/rcl/node.h | 2 +- rcl/src/rcl/graph.c | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/rcl/include/rcl/graph.h b/rcl/include/rcl/graph.h index c5395ff..ad12326 100644 --- a/rcl/include/rcl/graph.h +++ b/rcl/include/rcl/graph.h @@ -32,13 +32,19 @@ extern "C" typedef rmw_topic_names_and_types_t rcl_topic_names_and_types_t; + +/// Return a rcl_topic_names_and_types_t struct with members initialized to NULL. +RCL_PUBLIC +RCL_WARN_UNUSED +rcl_topic_names_and_types_t +rcl_get_zero_initialized_topic_names_and_types(void); + /// Return a list of topic names and their types. /* This function returns a list of topic names in the ROS graph and their types. * * The node parameter must not be NULL, and must point to a valid node. * - * The topic_names_and_types parameter must not be NULL, and must point to an - * already allocated rcl_topic_names_and_types_t struct. + * The topic_names_and_types parameter must be allocated and zero initialized. * The topic_names_and_types is the output for this function, and contains * allocated memory. * Therefore, it should be passed to rcl_destroy_topic_names_and_types() when diff --git a/rcl/include/rcl/node.h b/rcl/include/rcl/node.h index 2357531..0bcbaa1 100644 --- a/rcl/include/rcl/node.h +++ b/rcl/include/rcl/node.h @@ -80,7 +80,7 @@ rcl_get_zero_initialized_node(void); * This function will create those external parameter interfaces even if * parameters are not used later. * - * The rcl_node_t given must be allocated and zero initalized. + * The rcl_node_t given must be allocated and zero initialized. * Passing an rcl_node_t which has already had this function called on it, more * recently than rcl_node_fini, will fail. * An allocated rcl_node_t with uninitialized memory is undefined behavior. diff --git a/rcl/src/rcl/graph.c b/rcl/src/rcl/graph.c index 4b62830..494ab63 100644 --- a/rcl/src/rcl/graph.c +++ b/rcl/src/rcl/graph.c @@ -21,6 +21,13 @@ extern "C" #include "./common.h" +rcl_topic_names_and_types_t +rcl_get_zero_initialized_topic_names_and_types(void) +{ + const rcl_topic_names_and_types_t null_topic_names_and_types = {0, NULL, NULL}; + return null_topic_names_and_types; +} + rcl_ret_t rcl_get_topic_names_and_types( const rcl_node_t * node, @@ -31,6 +38,18 @@ rcl_get_topic_names_and_types( return RCL_RET_NODE_INVALID; } RCL_CHECK_ARGUMENT_FOR_NULL(topic_names_and_types, RCL_RET_INVALID_ARGUMENT); + if (topic_names_and_types->topic_count != 0) { + RCL_SET_ERROR_MSG("topic count is not zero"); + return RCL_RET_INVALID_ARGUMENT; + } + if (topic_names_and_types->topic_names) { + RCL_SET_ERROR_MSG("topic names is not null"); + return RCL_RET_INVALID_ARGUMENT; + } + if (topic_names_and_types->type_names) { + RCL_SET_ERROR_MSG("type names is not null"); + return RCL_RET_INVALID_ARGUMENT; + } return rmw_get_topic_names_and_types( rcl_node_get_rmw_handle(node), topic_names_and_types