From ff01a7f5bf40eee4a8d451dc7e8baf9a703186b6 Mon Sep 17 00:00:00 2001 From: Ramir Sultanov Date: Sat, 26 Oct 2024 14:08:01 +0300 Subject: [PATCH] Add message verification after message down casts Signed-off-by: Ramir Sultanov --- include/gz/transport/RepHandler.hh | 24 ++++++++++----------- include/gz/transport/SubscriptionHandler.hh | 14 ++++++------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/include/gz/transport/RepHandler.hh b/include/gz/transport/RepHandler.hh index 0ae1e634..ad5329f8 100644 --- a/include/gz/transport/RepHandler.hh +++ b/include/gz/transport/RepHandler.hh @@ -146,6 +146,18 @@ namespace gz google::protobuf::DynamicCastMessage(&_msgReq); auto msgRep = google::protobuf::DynamicCastMessage(&_msgRep); +#elif GOOGLE_PROTOBUF_VERSION >= 4022000 + auto msgReq = + google::protobuf::internal::DownCast(&_msgReq); + auto msgRep = google::protobuf::internal::DownCast(&_msgRep); +#elif GOOGLE_PROTOBUF_VERSION > 2999999 + auto msgReq = google::protobuf::down_cast(&_msgReq); + auto msgRep = google::protobuf::down_cast(&_msgRep); +#else + auto msgReq = + google::protobuf::internal::down_cast(&_msgReq); + auto msgRep = google::protobuf::internal::down_cast(&_msgRep); +#endif // Verify the dynamically casted messages are valid if (msgReq == nullptr || msgRep == nullptr) @@ -185,18 +197,6 @@ namespace gz std::cerr.flush(); return false; } -#elif GOOGLE_PROTOBUF_VERSION >= 4022000 - auto msgReq = - google::protobuf::internal::DownCast(&_msgReq); - auto msgRep = google::protobuf::internal::DownCast(&_msgRep); -#elif GOOGLE_PROTOBUF_VERSION > 2999999 - auto msgReq = google::protobuf::down_cast(&_msgReq); - auto msgRep = google::protobuf::down_cast(&_msgRep); -#else - auto msgReq = - google::protobuf::internal::down_cast(&_msgReq); - auto msgRep = google::protobuf::internal::down_cast(&_msgRep); -#endif return this->cb(*msgReq, *msgRep); } diff --git a/include/gz/transport/SubscriptionHandler.hh b/include/gz/transport/SubscriptionHandler.hh index e85eacc1..ed705ad9 100644 --- a/include/gz/transport/SubscriptionHandler.hh +++ b/include/gz/transport/SubscriptionHandler.hh @@ -216,6 +216,13 @@ namespace gz #if GOOGLE_PROTOBUF_VERSION >= 5028000 auto msgPtr = google::protobuf::DynamicCastMessage(&_msg); +#elif GOOGLE_PROTOBUF_VERSION >= 4022000 + auto msgPtr = google::protobuf::internal::DownCast(&_msg); +#elif GOOGLE_PROTOBUF_VERSION >= 3000000 + auto msgPtr = google::protobuf::down_cast(&_msg); +#else + auto msgPtr = google::protobuf::internal::down_cast(&_msg); +#endif // Verify the dynamically casted message is valid if (msgPtr == nullptr) @@ -236,13 +243,6 @@ namespace gz std::cerr.flush(); return false; } -#elif GOOGLE_PROTOBUF_VERSION >= 4022000 - auto msgPtr = google::protobuf::internal::DownCast(&_msg); -#elif GOOGLE_PROTOBUF_VERSION >= 3000000 - auto msgPtr = google::protobuf::down_cast(&_msg); -#else - auto msgPtr = google::protobuf::internal::down_cast(&_msg); -#endif this->cb(*msgPtr, _info); return true;