[RFC,v1] libcamera: base: signal: Fix invalid casts
diff mbox series

Message ID 20261124172056.1337374-1-barnabas.pocze@ideasonboard.com
State New
Headers show
Series
  • [RFC,v1] libcamera: base: signal: Fix invalid casts
Related show

Commit Message

Barnabás Pőcze Nov. 24, 2026, 5:20 p.m. UTC
Currently, the disconnection of a member function is problematic because
after checking if the receiver object matches, an unchecked cast to
`BoundMethodMember<...>` is carried out. There is a comment stating that
this is indeed correct, however, that comment was placed there before
58720e1dc981 ("libcamera: base: signal: Support connecting signals to functors")
and after that change, it is no longer true. Consider:

  s.connect(o, [] { ... });
  s.connect(o, &T::f);
  s.disconnect(o, &T::f);

Both have the same associated object, so both will match in that respect,
but the lambda is encapsulated in a `BoundMethodFunctor`, so the cast
will be invalid.

Given that the targets are polymorphic types, simply use `dynamic_cast` and
check if the cast has succeeded before proceeding.

While not necessary, the free function disconnection implementation is also
modified, primarily for "symmetry" and to avoid potential future problem
if the functionality is extended.

The "signal" test is also extended to test the disconnection of the
various possible types of slots.

Fixes: 58720e1dc981 ("libcamera: base: signal: Support connecting signals to functors")
Closes: https://gitlab.freedesktop.org/camera/libcamera/-/work_items/282
Signed-off-by: Barnabás Pőcze <barnabas.pocze@ideasonboard.com>
---
 include/libcamera/base/signal.h | 24 ++++++++----------------
 test/signal.cpp                 | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 16 deletions(-)

Patch
diff mbox series

diff --git a/include/libcamera/base/signal.h b/include/libcamera/base/signal.h
index 2bac5a9832..71d585712e 100644
--- a/include/libcamera/base/signal.h
+++ b/include/libcamera/base/signal.h
@@ -109,19 +109,12 @@  public:
 	void disconnect(T *obj, R (T::*func)(Args...))
 	{
 		SignalBase::disconnect([obj, func](SlotList::iterator &iter) {
-			BoundMethodArgs<R, Args...> *slot =
-				static_cast<BoundMethodArgs<R, Args...> *>(*iter);
-
-			if (!slot->match(obj))
+			if (!(*iter)->match(obj))
 				return false;
 
-			/*
-			 * If the object matches the slot, the slot is
-			 * guaranteed to be a member slot, so we can safely
-			 * cast it to BoundMethodMember<T, Args...> to match
-			 * func.
-			 */
-			return static_cast<BoundMethodMember<T, R, Args...> *>(slot)->match(func);
+			auto *slot = dynamic_cast<BoundMethodMember<T, R, Args...> *>(*iter);
+
+			return slot && slot->match(func);
 		});
 	}
 
@@ -129,13 +122,12 @@  public:
 	void disconnect(R (*func)(Args...))
 	{
 		SignalBase::disconnect([func](SlotList::iterator &iter) {
-			BoundMethodArgs<R, Args...> *slot =
-				static_cast<BoundMethodArgs<R, Args...> *>(*iter);
-
-			if (!slot->match(nullptr))
+			if (!(*iter)->match(nullptr))
 				return false;
 
-			return static_cast<BoundMethodStatic<R, Args...> *>(slot)->match(func);
+			auto *slot = dynamic_cast<BoundMethodStatic<R, Args...> *>(*iter);
+
+			return slot && slot->match(func);
 		});
 	}
 
diff --git a/test/signal.cpp b/test/signal.cpp
index 3f596b22db..f3d8d0cdb5 100644
--- a/test/signal.cpp
+++ b/test/signal.cpp
@@ -209,6 +209,39 @@  protected:
 			return TestFail;
 		}
 
+		/* Test disconnection of various types at once. */
+		{
+			static unsigned int staticCount = 0;
+
+			struct Receiver {
+				static void staticSlot() { staticCount += 1; }
+				void slot() { count += 1; }
+
+				unsigned int count = 0;
+			} o1, o2;
+
+			signalVoid_.disconnect();
+
+			signalVoid_.connect(&o1, [&] { o1.count += 1; });
+			signalVoid_.connect(&o1, &Receiver::slot);
+			signalVoid_.connect(&Receiver::staticSlot);
+
+			signalVoid_.connect(&o2, [&] { o2.count += 1; });
+			signalVoid_.connect(&o2, &Receiver::slot);
+			signalVoid_.connect(&Receiver::staticSlot);
+
+			signalVoid_.disconnect(&o1, &Receiver::slot);
+			signalVoid_.disconnect(&Receiver::staticSlot);
+			signalVoid_.disconnect(&o1);
+
+			signalVoid_.emit();
+
+			if (o1.count != 0 || o2.count != 2 || staticCount != 0) {
+				cout << "Signal disconnection failed" << endl;
+				return TestFail;
+			}
+		}
+
 		/* ----------------- Signal -> Object tests ----------------- */
 
 		/*