From 9452be1f0ae605daaf7c3fe2c6304fcbdf1cffda Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fran=C3=A7ois-Simon=20Fauteux-Chapleau?=
 <francois-simon.fauteux-chapleau@savoirfairelinux.com>
Date: Fri, 18 Oct 2024 11:37:15 -0400
Subject: [PATCH] mapping: fix bug when NotifyCallback is null

The change made in commit 41d80670d96b431f3358c4ac8906847d3e62ee40 to
prevent a mapping's NotifyCallback from being called more than once
unnecessarily didn't correctly handle the case where the callback is
null. As a result, the callback would sometimes not be called at all in
situations where it should have been.
https://git.jami.net/savoirfairelinux/jami-client-ios/-/issues/410

Change-Id: Icce78210b8be873be244e93e430d8c6123619db9
---
 src/upnp/protocol/mapping.cpp | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/upnp/protocol/mapping.cpp b/src/upnp/protocol/mapping.cpp
index 6e47ae6..f5c0516 100644
--- a/src/upnp/protocol/mapping.cpp
+++ b/src/upnp/protocol/mapping.cpp
@@ -299,6 +299,8 @@ Mapping::notify(sharedPtr_t mapping)
     NotifyCallback cb;
     {
         std::lock_guard lock(mapping->mutex_);
+        if (!mapping->notifyCb_)
+            return;
         if (mapping->state_ != mapping->lastNotifiedState_) {
             mapping->lastNotifiedState_ = mapping->state_;
             cb = mapping->notifyCb_;
@@ -320,6 +322,13 @@ Mapping::setNotifyCallback(NotifyCallback cb)
 {
     std::lock_guard lock(mutex_);
     notifyCb_ = std::move(cb);
+    if (!notifyCb_) {
+        // When a mapping is released by a controller, its NotifyCallback is set
+        // to null (see UPnPContext::releaseMapping). We need to reset
+        // lastNotifiedState_ when this happens to make sure the mapping isn't
+        // in a incorrect state if it's later reused by a different controller.
+        lastNotifiedState_ = std::nullopt;
+    }
 }
 
 void
-- 
GitLab