From c9cf8b5f189d6d45bfc50bf2eaeb34ee6cdeb7c2 Mon Sep 17 00:00:00 2001
From: Simon Morlat <simon.morlat@linphone.org>
Date: Mon, 11 Sep 2023 14:21:22 +0200
Subject: [PATCH] Fix API inconsistencies and memory faults

---
 include/linphone/api/c-event-log.h            |  4 ++--
 src/c-wrapper/api/c-event-log.cpp             |  2 +-
 src/chat/chat-room/client-group-chat-room.cpp |  2 +-
 .../conference/conference-security-event.cpp  |  2 +-
 .../conference/conference-security-event.h    |  2 +-
 .../push-notification-message.cpp             | 18 +++++++--------
 .../push-notification-message.h               | 22 +++++++++----------
 7 files changed, 26 insertions(+), 26 deletions(-)

diff --git a/include/linphone/api/c-event-log.h b/include/linphone/api/c-event-log.h
index 714d7ddeee..5b6d76ec2f 100644
--- a/include/linphone/api/c-event-log.h
+++ b/include/linphone/api/c-event-log.h
@@ -165,9 +165,9 @@ LINPHONE_PUBLIC LinphoneSecurityEventType linphone_event_log_get_security_event_
 /**
  * Returns the faulty device address of a conference security event.
  * @param event_log A #LinphoneEventLog object. @notnil
- * @return The #LinphoneAddress of the faulty device. @maybenil @tobefreed
+ * @return The #LinphoneAddress of the faulty device. @maybenil
  */
-LINPHONE_PUBLIC LinphoneAddress *
+LINPHONE_PUBLIC const LinphoneAddress *
 linphone_event_log_get_security_event_faulty_device_address(const LinphoneEventLog *event_log);
 
 // -----------------------------------------------------------------------------
diff --git a/src/c-wrapper/api/c-event-log.cpp b/src/c-wrapper/api/c-event-log.cpp
index 59bb56b9ba..8452a71c70 100644
--- a/src/c-wrapper/api/c-event-log.cpp
+++ b/src/c-wrapper/api/c-event-log.cpp
@@ -325,7 +325,7 @@ linphone_event_log_get_security_event_type(const LinphoneEventLog *event_log) {
 	return static_cast<LinphoneSecurityEventType>(securityEvent->getSecurityEventType());
 }
 
-LINPHONE_PUBLIC LinphoneAddress *
+LINPHONE_PUBLIC const LinphoneAddress *
 linphone_event_log_get_security_event_faulty_device_address(const LinphoneEventLog *event_log) {
 	if (!isConferenceSecurityType(linphone_event_log_get_type(event_log))) return nullptr;
 
diff --git a/src/chat/chat-room/client-group-chat-room.cpp b/src/chat/chat-room/client-group-chat-room.cpp
index d8e7385e68..5c52cc648b 100644
--- a/src/chat/chat-room/client-group-chat-room.cpp
+++ b/src/chat/chat-room/client-group-chat-room.cpp
@@ -1160,7 +1160,7 @@ void ClientGroupChatRoom::onSecurityEvent(const shared_ptr<ConferenceSecurityEve
 	shared_ptr<ConferenceSecurityEvent> cleanEvent = nullptr;
 
 	// Remove faulty device if its address is invalid
-	std::shared_ptr<Address> faultyDevice = event->getFaultyDeviceAddress();
+	auto faultyDevice = event->getFaultyDeviceAddress();
 	if (!faultyDevice || !faultyDevice->isValid()) {
 		cleanEvent = make_shared<ConferenceSecurityEvent>(event->getCreationTime(), event->getConferenceId(),
 		                                                  event->getSecurityEventType());
diff --git a/src/event-log/conference/conference-security-event.cpp b/src/event-log/conference/conference-security-event.cpp
index de8e89464d..db9fa973ed 100644
--- a/src/event-log/conference/conference-security-event.cpp
+++ b/src/event-log/conference/conference-security-event.cpp
@@ -60,7 +60,7 @@ ConferenceSecurityEvent::SecurityEventType ConferenceSecurityEvent::getSecurityE
 	return d->securityEventType;
 }
 
-const std::shared_ptr<Address> &ConferenceSecurityEvent::getFaultyDeviceAddress() const {
+std::shared_ptr<const Address> ConferenceSecurityEvent::getFaultyDeviceAddress() const {
 	L_D();
 	return d->faultyDevice;
 }
diff --git a/src/event-log/conference/conference-security-event.h b/src/event-log/conference/conference-security-event.h
index af4e2b5595..6c9a2deeea 100644
--- a/src/event-log/conference/conference-security-event.h
+++ b/src/event-log/conference/conference-security-event.h
@@ -61,7 +61,7 @@ public:
 	ConferenceSecurityEvent(time_t creationTime, const ConferenceId &conferenceId, SecurityEventType securityEventType);
 
 	SecurityEventType getSecurityEventType() const;
-	const std::shared_ptr<Address> &getFaultyDeviceAddress() const;
+	std::shared_ptr<const Address> getFaultyDeviceAddress() const;
 
 private:
 	L_DECLARE_PRIVATE(ConferenceSecurityEvent);
diff --git a/src/push-notification-message/push-notification-message.cpp b/src/push-notification-message/push-notification-message.cpp
index 2e48eccf76..29ca164ca7 100644
--- a/src/push-notification-message/push-notification-message.cpp
+++ b/src/push-notification-message/push-notification-message.cpp
@@ -55,9 +55,9 @@ void PushNotificationMessage::init(const std::string &callId,
 	mIsText = isText;
 	mTextContent = textContent;
 	mSubject = subject;
-	mFromAddr = fromAddr;
-	mLocalAddr = localAddr;
-	mPeerAddr = peerAddr;
+	mFromAddr = (new Address(fromAddr))->toSharedPtr();
+	mLocalAddr = (new Address(localAddr))->toSharedPtr();
+	mPeerAddr = (new Address(peerAddr))->toSharedPtr();
 	mIsIcalendar = isIcalendar;
 	mIsConferenceInvitationNew = isConferenceInvitationNew;
 	mIsConferenceInvitationUpdate = isConferenceInvitationUpdate;
@@ -80,15 +80,15 @@ const string &PushNotificationMessage::getSubject() const {
 	return mSubject;
 }
 
-shared_ptr<Address> PushNotificationMessage::getFromAddr() const {
-	return make_shared<Address>(mFromAddr);
+shared_ptr<const Address> PushNotificationMessage::getFromAddr() const {
+	return mFromAddr;
 }
-shared_ptr<Address> PushNotificationMessage::getLocalAddr() const {
-	return make_shared<Address>(mLocalAddr);
+shared_ptr<const Address> PushNotificationMessage::getLocalAddr() const {
+	return mLocalAddr;
 }
 
-shared_ptr<Address> PushNotificationMessage::getPeerAddr() const {
-	return make_shared<Address>(mPeerAddr);
+shared_ptr<const Address> PushNotificationMessage::getPeerAddr() const {
+	return mPeerAddr;
 }
 
 bool PushNotificationMessage::isIcalendar() const {
diff --git a/src/push-notification-message/push-notification-message.h b/src/push-notification-message/push-notification-message.h
index 524d8be625..b512afa642 100644
--- a/src/push-notification-message/push-notification-message.h
+++ b/src/push-notification-message/push-notification-message.h
@@ -57,9 +57,9 @@ public:
 	bool isText() const;
 	const std::string &getTextContent() const;
 	const std::string &getSubject() const;
-	std::shared_ptr<Address> getFromAddr() const;
-	std::shared_ptr<Address> getLocalAddr() const;
-	std::shared_ptr<Address> getPeerAddr() const;
+	std::shared_ptr<const Address> getFromAddr() const;
+	std::shared_ptr<const Address> getLocalAddr() const;
+	std::shared_ptr<const Address> getPeerAddr() const;
 	bool isIcalendar() const;
 	bool isConferenceInvitationNew() const;
 	bool isConferenceInvitationUpdate() const;
@@ -69,16 +69,16 @@ public:
 
 private:
 	std::string mCallId;
-	bool mIsText;
+	bool mIsText = false;
 	std::string mTextContent;
 	std::string mSubject;
-	std::string mFromAddr;
-	std::string mLocalAddr;
-	std::string mPeerAddr;
-	bool mIsIcalendar;
-	bool mIsConferenceInvitationNew;
-	bool mIsConferenceInvitationUpdate;
-	bool mIsConferenceInvitationCancellation;
+	std::shared_ptr<Address> mFromAddr;
+	std::shared_ptr<Address> mLocalAddr;
+	std::shared_ptr<Address> mPeerAddr;
+	bool mIsIcalendar = false;
+	bool mIsConferenceInvitationNew = false;
+	bool mIsConferenceInvitationUpdate = false;
+	bool mIsConferenceInvitationCancellation = false;
 };
 
 LINPHONE_END_NAMESPACE
-- 
GitLab