Fix the dereference to NULL (#405)

* Fix the dereference to NULL
rmw_*_validation_result_string(validation_result) may return NULL,
and it's dereferenced by passing arg to NameValidationError

Signed-off-by: Ethan Gao <ethan.gao@linux.intel.com>

* address NULL case of undefined type

Signed-off-by: Ethan Gao <ethan.gao@linux.intel.com>

* Address the issue to deference to NULL with adapt
to the change of API rmw_*_validation_result_string

Signed-off-by: Ethan Gao <ethan.gao@linux.intel.com>

* revise the typo

* throw exception when valid rmw check but invalid rcl check

Signed-off-by: Ethan Gao <ethan.gao@linux.intel.com>
This commit is contained in:
Ethan Gao 2018-01-09 03:02:23 +08:00 committed by Mikael Arguedas
parent 2bf688827b
commit b81f55e5df
2 changed files with 50 additions and 29 deletions

View file

@ -102,20 +102,20 @@ rclcpp::expand_topic_or_service_name(
throw_from_rcl_error(ret); throw_from_rcl_error(ret);
} }
if (validation_result == RCL_TOPIC_NAME_VALID) { if (validation_result != RCL_TOPIC_NAME_VALID) {
const char * validation_message =
rcl_topic_name_validation_result_string(validation_result);
if (is_service) {
using rclcpp::exceptions::InvalidServiceNameError;
throw InvalidServiceNameError(name.c_str(), validation_message, invalid_index);
} else {
using rclcpp::exceptions::InvalidTopicNameError;
throw InvalidTopicNameError(name.c_str(), validation_message, invalid_index);
}
} else {
throw std::runtime_error("topic name unexpectedly valid"); throw std::runtime_error("topic name unexpectedly valid");
} }
const char * validation_message = rcl_topic_name_validation_result_string(validation_result);
if (!validation_message) {
throw std::runtime_error("unable to get validation error message");
}
if (is_service) {
using rclcpp::exceptions::InvalidServiceNameError;
throw InvalidServiceNameError(name.c_str(), validation_message, invalid_index);
} else {
using rclcpp::exceptions::InvalidTopicNameError;
throw InvalidTopicNameError(name.c_str(), validation_message, invalid_index);
}
// if invalid node name // if invalid node name
} else if (ret == RCL_RET_NODE_INVALID_NAME) { } else if (ret == RCL_RET_NODE_INVALID_NAME) {
rcl_reset_error(); // explicitly discard error from rcl_expand_topic_name() rcl_reset_error(); // explicitly discard error from rcl_expand_topic_name()
@ -133,10 +133,16 @@ rclcpp::expand_topic_or_service_name(
RCL_RET_ERROR, "failed to validate node name", RCL_RET_ERROR, "failed to validate node name",
rmw_get_error_state(), rmw_reset_error); rmw_get_error_state(), rmw_reset_error);
} }
throw rclcpp::exceptions::InvalidNodeNameError(
node_name.c_str(), if (validation_result != RMW_NODE_NAME_VALID) {
rmw_node_name_validation_result_string(validation_result), throw rclcpp::exceptions::InvalidNodeNameError(
invalid_index); node_name.c_str(),
rmw_node_name_validation_result_string(validation_result),
invalid_index);
} else {
throw std::runtime_error("invalid rcl node name but valid rmw node name");
}
// if invalid namespace // if invalid namespace
} else if (ret == RCL_RET_NODE_INVALID_NAMESPACE) { } else if (ret == RCL_RET_NODE_INVALID_NAMESPACE) {
rcl_reset_error(); // explicitly discard error from rcl_expand_topic_name() rcl_reset_error(); // explicitly discard error from rcl_expand_topic_name()
@ -154,10 +160,15 @@ rclcpp::expand_topic_or_service_name(
RCL_RET_ERROR, "failed to validate namespace", RCL_RET_ERROR, "failed to validate namespace",
rmw_get_error_state(), rmw_reset_error); rmw_get_error_state(), rmw_reset_error);
} }
throw rclcpp::exceptions::InvalidNamespaceError(
namespace_.c_str(), if (validation_result != RMW_NAMESPACE_VALID) {
rmw_namespace_validation_result_string(validation_result), throw rclcpp::exceptions::InvalidNamespaceError(
invalid_index); namespace_.c_str(),
rmw_namespace_validation_result_string(validation_result),
invalid_index);
} else {
throw std::runtime_error("invalid rcl namespace but valid rmw namespace");
}
// something else happened // something else happened
} else { } else {
throw_from_rcl_error(ret); throw_from_rcl_error(ret);
@ -179,6 +190,7 @@ rclcpp::expand_topic_or_service_name(
RCL_RET_ERROR, "failed to validate full topic name", RCL_RET_ERROR, "failed to validate full topic name",
rmw_get_error_state(), rmw_reset_error); rmw_get_error_state(), rmw_reset_error);
} }
if (validation_result != RMW_TOPIC_VALID) { if (validation_result != RMW_TOPIC_VALID) {
if (is_service) { if (is_service) {
throw rclcpp::exceptions::InvalidServiceNameError( throw rclcpp::exceptions::InvalidServiceNameError(

View file

@ -106,10 +106,15 @@ NodeBase::NodeBase(
} }
throw_from_rcl_error(RCL_RET_ERROR, "failed to validate node name"); throw_from_rcl_error(RCL_RET_ERROR, "failed to validate node name");
} }
throw rclcpp::exceptions::InvalidNodeNameError(
node_name.c_str(), if (validation_result != RMW_NODE_NAME_VALID) {
rmw_node_name_validation_result_string(validation_result), throw rclcpp::exceptions::InvalidNodeNameError(
invalid_index); node_name.c_str(),
rmw_node_name_validation_result_string(validation_result),
invalid_index);
} else {
throw std::runtime_error("valid rmw node name but invalid rcl node name");
}
} }
if (ret == RCL_RET_NODE_INVALID_NAMESPACE) { if (ret == RCL_RET_NODE_INVALID_NAMESPACE) {
@ -124,12 +129,16 @@ NodeBase::NodeBase(
} }
throw_from_rcl_error(RCL_RET_ERROR, "failed to validate namespace"); throw_from_rcl_error(RCL_RET_ERROR, "failed to validate namespace");
} }
throw rclcpp::exceptions::InvalidNamespaceError(
namespace_.c_str(),
rmw_namespace_validation_result_string(validation_result),
invalid_index);
}
if (validation_result != RMW_NAMESPACE_VALID) {
throw rclcpp::exceptions::InvalidNamespaceError(
namespace_.c_str(),
rmw_namespace_validation_result_string(validation_result),
invalid_index);
} else {
throw std::runtime_error("valid rmw node namespace but invalid rcl node namespace");
}
}
throw_from_rcl_error(ret, "failed to initialize rcl node"); throw_from_rcl_error(ret, "failed to initialize rcl node");
} }