Skip to content

Commit

Permalink
Segmentation error to dereference nullptr (#180)
Browse files Browse the repository at this point in the history
* Segmentation error to dereference nullptr

getEDPReaders is possible to return nullptr

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

* Avoid null dereference to application crash

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

*  change the way not equal to nullptr

* reflect the failure to get EDP readers

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

* check either edp_reader

Signed-off-by: Ethan Gao <ethan.gao@linux.intel.com>
  • Loading branch information
gaoethan authored and Karsten1987 committed Dec 19, 2017
1 parent 640d7de commit 97a0998
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
19 changes: 17 additions & 2 deletions rmw_fastrtps_cpp/src/rmw_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ create_node(
node_impl->secondaryPubListener = tnat_2;

edp_readers = participant->getEDPReaders();
if (!edp_readers.first) {
RMW_SET_ERROR_MSG("edp_readers.first is null");
goto fail;
}

if (!edp_readers.second) {
RMW_SET_ERROR_MSG("edp_readers.second is null");
goto fail;
}

if (!(edp_readers.first->setListener(tnat_1) & edp_readers.second->setListener(tnat_2))) {
RMW_SET_ERROR_MSG("Failed to attach ROS related logic to the Participant");
goto fail;
Expand Down Expand Up @@ -272,12 +282,17 @@ rmw_destroy_node(rmw_node_t * node)

// Begin deleting things in the same order they were created in rmw_create_node().
std::pair<StatefulReader *, StatefulReader *> edp_readers = participant->getEDPReaders();
if (!edp_readers.first->setListener(nullptr)) {
if (!edp_readers.first || !edp_readers.second) {
RMW_SET_ERROR_MSG("failed to get EDPReader listener");
result_ret = RMW_RET_ERROR;
}

if (edp_readers.first && !edp_readers.first->setListener(nullptr)) {
RMW_SET_ERROR_MSG("failed to unset EDPReader listener");
result_ret = RMW_RET_ERROR;
}
delete impl->secondarySubListener;
if (!edp_readers.second->setListener(nullptr)) {
if (edp_readers.second && !edp_readers.second->setListener(nullptr)) {
RMW_SET_ERROR_MSG("failed to unset EDPReader listener");
result_ret = RMW_RET_ERROR;
}
Expand Down
6 changes: 5 additions & 1 deletion rmw_fastrtps_cpp/src/rmw_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ rmw_create_service(
}

rmw_service = rmw_service_allocate();
if (!rmw_service) {
RMW_SET_ERROR_MSG("failed to allocate memory for service");
goto fail;
}
rmw_service->implementation_identifier = eprosima_fastrtps_identifier;
rmw_service->data = info;
rmw_service->service_name = reinterpret_cast<const char *>(
Expand Down Expand Up @@ -237,7 +241,7 @@ rmw_create_service(
delete info;
}

if (rmw_service->service_name != nullptr) {
if (rmw_service && rmw_service->service_name) {
rmw_free(const_cast<char *>(rmw_service->service_name));
rmw_service->service_name = nullptr;
}
Expand Down

0 comments on commit 97a0998

Please sign in to comment.