From patchwork Tue Nov 24 17:20:56 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= X-Patchwork-Id: 29085 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 224A0C3304 for ; Tue, 24 Nov 2026 17:21:04 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 3718D6A9D9; Tue, 24 Nov 2026 18:21:02 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="KCzjoc1c"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 469376A9D2 for ; Tue, 24 Nov 2026 18:21:00 +0100 (CET) Received: from pb-laptop.local (185.182.215.119.nat.pool.zt.hu [185.182.215.119]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 36FBAB5 for ; Tue, 24 Nov 2026 18:18:27 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1795540707; bh=zBG3V6PScRUYacLMJfviWQ1+8nyLgGMcsGpp9LTBo5M=; h=From:To:Subject:Date:From; b=KCzjoc1cfQKBFJ7espXHaE6sh4ft00aCOaOJaqZonFZyr+hH7KAnQtI++RuF7Pnc6 LDzuSbDDUDHqtzAey2nc0IXTRloi3IWMPNYL+CbPBmBKSHYgU+pmfZtgZrtyURr51j P7UW4tqhu601vdiHnVN14pbjAukn5OFzL8Uu9uMo= From: =?utf-8?q?Barnab=C3=A1s_P=C5=91cze?= To: libcamera-devel@lists.libcamera.org Subject: [RFC PATCH v1] libcamera: base: signal: Fix invalid casts Date: Tue, 24 Nov 2026 18:20:56 +0100 Message-ID: <20261124172056.1337374-1-barnabas.pocze@ideasonboard.com> X-Mailer: git-send-email 2.56.1 MIME-Version: 1.0 X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" 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 --- include/libcamera/base/signal.h | 24 ++++++++---------------- test/signal.cpp | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 16 deletions(-) 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 *slot = - static_cast *>(*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 to match - * func. - */ - return static_cast *>(slot)->match(func); + auto *slot = dynamic_cast *>(*iter); + + return slot && slot->match(func); }); } @@ -129,13 +122,12 @@ public: void disconnect(R (*func)(Args...)) { SignalBase::disconnect([func](SlotList::iterator &iter) { - BoundMethodArgs *slot = - static_cast *>(*iter); - - if (!slot->match(nullptr)) + if (!(*iter)->match(nullptr)) return false; - return static_cast *>(slot)->match(func); + auto *slot = dynamic_cast *>(*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 ----------------- */ /*