Skip to content

Commit

Permalink
Add message verification after message down casts
Browse files Browse the repository at this point in the history
Signed-off-by: Ramir Sultanov <[email protected]>
  • Loading branch information
ramir0-sultanov committed Oct 26, 2024
1 parent 8986a68 commit ff01a7f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
24 changes: 12 additions & 12 deletions include/gz/transport/RepHandler.hh
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ namespace gz
google::protobuf::DynamicCastMessage<Req>(&_msgReq);
auto msgRep =
google::protobuf::DynamicCastMessage<Rep>(&_msgRep);
#elif GOOGLE_PROTOBUF_VERSION >= 4022000
auto msgReq =
google::protobuf::internal::DownCast<const Req*>(&_msgReq);
auto msgRep = google::protobuf::internal::DownCast<Rep*>(&_msgRep);
#elif GOOGLE_PROTOBUF_VERSION > 2999999
auto msgReq = google::protobuf::down_cast<const Req*>(&_msgReq);
auto msgRep = google::protobuf::down_cast<Rep*>(&_msgRep);
#else
auto msgReq =
google::protobuf::internal::down_cast<const Req*>(&_msgReq);
auto msgRep = google::protobuf::internal::down_cast<Rep*>(&_msgRep);
#endif

// Verify the dynamically casted messages are valid
if (msgReq == nullptr || msgRep == nullptr)
Expand Down Expand Up @@ -185,18 +197,6 @@ namespace gz
std::cerr.flush();
return false;
}
#elif GOOGLE_PROTOBUF_VERSION >= 4022000
auto msgReq =
google::protobuf::internal::DownCast<const Req*>(&_msgReq);
auto msgRep = google::protobuf::internal::DownCast<Rep*>(&_msgRep);
#elif GOOGLE_PROTOBUF_VERSION > 2999999
auto msgReq = google::protobuf::down_cast<const Req*>(&_msgReq);
auto msgRep = google::protobuf::down_cast<Rep*>(&_msgRep);
#else
auto msgReq =
google::protobuf::internal::down_cast<const Req*>(&_msgReq);
auto msgRep = google::protobuf::internal::down_cast<Rep*>(&_msgRep);
#endif

return this->cb(*msgReq, *msgRep);
}
Expand Down
14 changes: 7 additions & 7 deletions include/gz/transport/SubscriptionHandler.hh
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ namespace gz

#if GOOGLE_PROTOBUF_VERSION >= 5028000
auto msgPtr = google::protobuf::DynamicCastMessage<T>(&_msg);
#elif GOOGLE_PROTOBUF_VERSION >= 4022000
auto msgPtr = google::protobuf::internal::DownCast<const T*>(&_msg);
#elif GOOGLE_PROTOBUF_VERSION >= 3000000
auto msgPtr = google::protobuf::down_cast<const T*>(&_msg);
#else
auto msgPtr = google::protobuf::internal::down_cast<const T*>(&_msg);
#endif

// Verify the dynamically casted message is valid
if (msgPtr == nullptr)
Expand All @@ -236,13 +243,6 @@ namespace gz
std::cerr.flush();
return false;
}
#elif GOOGLE_PROTOBUF_VERSION >= 4022000
auto msgPtr = google::protobuf::internal::DownCast<const T*>(&_msg);
#elif GOOGLE_PROTOBUF_VERSION >= 3000000
auto msgPtr = google::protobuf::down_cast<const T*>(&_msg);
#else
auto msgPtr = google::protobuf::internal::down_cast<const T*>(&_msg);
#endif

this->cb(*msgPtr, _info);
return true;
Expand Down

0 comments on commit ff01a7f

Please sign in to comment.