Skip to content

Commit

Permalink
Guard against returning NULL or empty node names (#570)
Browse files Browse the repository at this point in the history
Return an error instead.

Signed-off-by: Jacob Perron <[email protected]>

* Update documentation

Signed-off-by: Jacob Perron <[email protected]>

* Separate error messages for null vs empty

Signed-off-by: Jacob Perron <[email protected]>

* Guard against null namespaces

Signed-off-by: Jacob Perron <[email protected]>

* Use more specific error codes

Signed-off-by: Jacob Perron <[email protected]>
  • Loading branch information
jacobperron authored Mar 26, 2021
1 parent 98f47ae commit b33feb1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
3 changes: 2 additions & 1 deletion rcl/include/rcl/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,6 @@ rcl_names_and_types_fini(rcl_names_and_types_t * names_and_types);
*
* The `node_names` parameter must be allocated and zero initialized.
* `node_names` is the output for this function, and contains allocated memory.
* Note that entries in the array might contain `NULL` values.
* Use rcutils_get_zero_initialized_string_array() for initializing an empty
* rcutils_string_array_t struct.
* This `node_names` struct should therefore be passed to rcutils_string_array_fini()
Expand Down Expand Up @@ -445,6 +444,8 @@ rcl_names_and_types_fini(rcl_names_and_types_t * names_and_types);
* \return #RCL_RET_OK if the query was successful, or
* \return #RCL_RET_BAD_ALLOC if an error occurred while allocating memory, or
* \return #RCL_RET_INVALID_ARGUMENT if any arguments are invalid, or
* \return #RCL_RET_NODE_INVALID_NAME if a node with an invalid name is detected, or
* \return #RCL_RET_NODE_INVALID_NAMESPACE if a node with an invalid namespace is detected, or
* \return #RCL_RET_ERROR if an unspecified error occurs.
*/
RCL_PUBLIC
Expand Down
25 changes: 24 additions & 1 deletion rcl/src/rcl/graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,30 @@ rcl_get_node_names(
rcl_node_get_rmw_handle(node),
node_names,
node_namespaces);
return rcl_convert_rmw_ret_to_rcl_ret(rmw_ret);

if (RMW_RET_OK != rmw_ret) {
return rcl_convert_rmw_ret_to_rcl_ret(rmw_ret);
}

// Check that none of the node names are NULL or empty
for (size_t i = 0u; i < node_names->size; ++i) {
if (!node_names->data[i]) {
RCL_SET_ERROR_MSG("NULL node name returned by the RMW layer");
return RCL_RET_NODE_INVALID_NAME;
}
if (!strcmp(node_names->data[i], "")) {
RCL_SET_ERROR_MSG("empty node name returned by the RMW layer");
return RCL_RET_NODE_INVALID_NAME;
}
}
// Check that none of the node namespaces are NULL
for (size_t i = 0u; i < node_namespaces->size; ++i) {
if (!node_namespaces->data[i]) {
RCL_SET_ERROR_MSG("NULL node namespace returned by the RMW layer");
return RCL_RET_NODE_INVALID_NAMESPACE;
}
}
return RCL_RET_OK;
}

rcl_ret_t
Expand Down

0 comments on commit b33feb1

Please sign in to comment.