Skip to content

Commit 19eeb76

Browse files
committed
catch exception if non-compliant mqtt message is published to message type topic
1 parent 2ac9b00 commit 19eeb76

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/MqttClient.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,12 +743,19 @@ void MqttClient::message_arrived(mqtt::const_message_ptr mqtt_msg) {
743743
char* non_const_payload = const_cast<char*>(&payload[0]);
744744
uint8_t* msg_type_buffer = reinterpret_cast<uint8_t*>(non_const_payload);
745745

746-
// TODO: might crash if payload by chance does not contain ros msg type
747746
// deserialize ROS message type
748747
RosMsgType ros_msg_type;
749748
ros::serialization::IStream msg_type_stream(msg_type_buffer,
750749
payload_length);
751-
ros::serialization::deserialize(msg_type_stream, ros_msg_type);
750+
try {
751+
ros::serialization::deserialize(msg_type_stream, ros_msg_type);
752+
} catch (ros::serialization::StreamOverrunException) {
753+
NODELET_ERROR(
754+
"Failed to deserialize ROS message type from MQTT message received on "
755+
"topic '%s', got message:\n%s",
756+
mqtt_topic.c_str(), mqtt_msg->to_string().c_str());
757+
return;
758+
}
752759

753760
// reconstruct corresponding MQTT data topic
754761
std::string mqtt_data_topic = mqtt_topic;

0 commit comments

Comments
 (0)