From 1e700b7d3cd091b474c3f319e4996f321db1f5f7 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Tue, 13 Feb 2024 11:00:46 -0500 Subject: [PATCH] cpp-common/bt2: add const_cast when setting user data If the user data type is specified as a const type, `static_cast` will not work. Fix that by doing a static_cast to `const void *` then a const_cast to `void *`. Change-Id: Ic39c9c25da1899defe4edf8c8e800297fbf2fb2d Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/11799 Tested-by: jenkins Reviewed-by: Philippe Proulx --- src/cpp-common/bt2/self-component-port.hpp | 6 ++++-- src/cpp-common/bt2/self-message-iterator.hpp | 3 ++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/cpp-common/bt2/self-component-port.hpp b/src/cpp-common/bt2/self-component-port.hpp index 2551064e..5b34abfd 100644 --- a/src/cpp-common/bt2/self-component-port.hpp +++ b/src/cpp-common/bt2/self-component-port.hpp @@ -102,7 +102,8 @@ public: template void data(T& obj) const noexcept { - bt_self_component_set_data(this->libObjPtr(), static_cast(&obj)); + bt_self_component_set_data(this->libObjPtr(), + const_cast(static_cast(&obj))); } bt2::TraceClass::Shared createTraceClass() const @@ -148,7 +149,8 @@ protected: { LibPortT *libPortPtr; - const auto status = func(this->libObjPtr(), name, static_cast(data), &libPortPtr); + const auto status = func(this->libObjPtr(), name, + const_cast(static_cast(data)), &libPortPtr); switch (status) { case BT_SELF_COMPONENT_ADD_PORT_STATUS_OK: diff --git a/src/cpp-common/bt2/self-message-iterator.hpp b/src/cpp-common/bt2/self-message-iterator.hpp index e333cb1a..4460fe27 100644 --- a/src/cpp-common/bt2/self-message-iterator.hpp +++ b/src/cpp-common/bt2/self-message-iterator.hpp @@ -69,7 +69,8 @@ public: template void data(T& obj) const noexcept { - bt_self_message_iterator_set_data(this->libObjPtr(), static_cast(&obj)); + bt_self_message_iterator_set_data(this->libObjPtr(), + const_cast(static_cast(&obj))); } bt2::StreamBeginningMessage::Shared -- 2.34.1