Commit 815bbf8cfa for freeswitch.com

commit 815bbf8cfa0cc2c03a05410e5dc54ca3e8288b99
Author: Andrey Volk <andywolk@gmail.com>
Date:   Thu Nov 26 15:50:36 2026 +0300

    [Core] Stop partner uuid lookups from growing the session pool, with a unit-test (#3311)

    switch_channel_get_partner_uuid_copy() read signal_bond and originate_signal_bond with dup set, so every call
    copied the uuid into the session pool before copying it into the caller's buffer. The read path calls it for
    each comfort-noise frame of a bridged leg, so the pool grew by 40 bytes per frame until hangup. The lookups now
    return the channel's own value, which stays valid under profile_mutex while it is copied.

    The copy used strncpy(), which leaves the buffer without a terminating NUL when the value is as long as the
    buffer or longer. switch_copy_string() cuts the value to fit and always terminates it.

    With RTCP passthru, every received RTCP packet looked the partner up with switch_channel_get_partner_uuid(), which
    copies the uuid into the session pool, so the pool grew with each packet until hangup. The lookup now copies the
    uuid into a stack buffer with switch_channel_get_partner_uuid_copy().

diff --git a/src/switch_channel.c b/src/switch_channel.c
index d360c8a951..9798ec0cbb 100644
--- a/src/switch_channel.c
+++ b/src/switch_channel.c
@@ -4851,12 +4851,12 @@ SWITCH_DECLARE(const char *) switch_channel_get_partner_uuid_copy(switch_channel
 	const char *uuid = NULL;

 	switch_mutex_lock(channel->profile_mutex);
-	if (!(uuid = switch_channel_get_variable_dup(channel, SWITCH_SIGNAL_BOND_VARIABLE, SWITCH_TRUE, -1))) {
-		uuid = switch_channel_get_variable_dup(channel, SWITCH_ORIGINATE_SIGNAL_BOND_VARIABLE, SWITCH_TRUE, -1);
+	if (!(uuid = switch_channel_get_variable_dup(channel, SWITCH_SIGNAL_BOND_VARIABLE, SWITCH_FALSE, -1))) {
+		uuid = switch_channel_get_variable_dup(channel, SWITCH_ORIGINATE_SIGNAL_BOND_VARIABLE, SWITCH_FALSE, -1);
 	}

 	if (uuid) {
-		strncpy(buf, uuid, blen);
+		switch_copy_string(buf, uuid, blen);
 		uuid = (const char *) buf;
 	}
 	switch_mutex_unlock(channel->profile_mutex);
diff --git a/src/switch_rtp.c b/src/switch_rtp.c
index 7fe7c66e07..804f46011a 100644
--- a/src/switch_rtp.c
+++ b/src/switch_rtp.c
@@ -8105,7 +8105,8 @@ static int rtp_common_read(switch_rtp_t *rtp_session, switch_payload_t *payload_

 					if (rtp_session->flags[SWITCH_RTP_FLAG_RTCP_PASSTHRU]) {
 						switch_channel_t *channel = switch_core_session_get_channel(rtp_session->session);
-						const char *uuid = switch_channel_get_partner_uuid(channel);
+						char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1];
+						const char *uuid = switch_channel_get_partner_uuid_copy(channel, uuid_str, sizeof(uuid_str));

 						if (uuid) {
 							switch_core_session_t *other_session;
diff --git a/tests/unit/switch_core_session.c b/tests/unit/switch_core_session.c
index f5f6f51ba7..f1595397b0 100644
--- a/tests/unit/switch_core_session.c
+++ b/tests/unit/switch_core_session.c
@@ -31,6 +31,31 @@
 #include <switch.h>
 #include <test/switch_test.h>

+/* Looks up the session's partner uuid with switch_channel_get_partner_uuid_copy() the given number of times and
+   returns the last result. *pool_moved is set when the session pool handed out memory during the lookups.
+   The check relies on a 0-byte switch_core_session_alloc() returning the pool's next free address without using it up,
+   so the same address comes back until something else is allocated. A debug pool (APR_POOL_DEBUG, enabled by
+   --enable-pool-sanitizer) mallocs every allocation separately, so two 0-byte allocations in a row return different
+   addresses. The check is skipped then, and *pool_moved stays 0. */
+static const char *partner_uuid_copy_repeat(switch_core_session_t *session, char *buf, switch_size_t buflen, int lookups, int *pool_moved)
+{
+	switch_channel_t *channel = switch_core_session_get_channel(session);
+	void *pool_mark = switch_core_session_alloc(session, 0);
+	const char *uuid = NULL;
+	int i;
+
+	if (switch_core_session_alloc(session, 0) != pool_mark) {
+		pool_mark = NULL;
+	}
+
+	for (i = 0; i < lookups; i++) {
+		uuid = switch_channel_get_partner_uuid_copy(channel, buf, buflen);
+	}
+
+	*pool_moved = pool_mark && switch_core_session_alloc(session, 0) != pool_mark;
+
+	return uuid;
+}

 FST_CORE_BEGIN("./conf")
 {
@@ -71,6 +96,49 @@ FST_CORE_BEGIN("./conf")
 			fst_check(session == NULL);
 		}
 		FST_SESSION_END()
+
+		FST_SESSION_BEGIN(partner_uuid_copy)
+		{
+			char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1] = "";
+			char long_bond[SWITCH_UUID_FORMATTED_LENGTH + 10];
+			const char *partner_uuid = switch_core_session_get_uuid(fst_session);
+			const char *uuid = NULL;
+			int pool_moved = 0;
+
+			/* no bond variable: no partner */
+			uuid = switch_channel_get_partner_uuid_copy(fst_channel, uuid_str, sizeof(uuid_str));
+			fst_check(uuid == NULL);
+
+			/* signal_bond: every lookup copies the uuid into uuid_str, and 1000 lookups take nothing from the session pool */
+			switch_channel_set_variable(fst_channel, SWITCH_SIGNAL_BOND_VARIABLE, partner_uuid);
+			uuid = partner_uuid_copy_repeat(fst_session, uuid_str, sizeof(uuid_str), 1000, &pool_moved);
+			fst_check(uuid == uuid_str);
+			fst_check_string_equals(uuid_str, partner_uuid);
+			fst_check(!pool_moved);
+
+			/* originate_signal_bond is used when signal_bond is unset, with the same outcome */
+			switch_channel_set_variable(fst_channel, SWITCH_SIGNAL_BOND_VARIABLE, NULL);
+			switch_channel_set_variable(fst_channel, SWITCH_ORIGINATE_SIGNAL_BOND_VARIABLE, partner_uuid);
+			memset(uuid_str, 0, sizeof(uuid_str));
+			uuid = partner_uuid_copy_repeat(fst_session, uuid_str, sizeof(uuid_str), 1000, &pool_moved);
+			fst_check(uuid == uuid_str);
+			fst_check_string_equals(uuid_str, partner_uuid);
+			fst_check(!pool_moved);
+
+			/* a signal_bond longer than uuid_str: the copy is cut to SWITCH_UUID_FORMATTED_LENGTH bytes and ends with a NUL */
+			memset(long_bond, 'a', sizeof(long_bond) - 1);
+			long_bond[sizeof(long_bond) - 1] = '\0';
+			switch_channel_set_variable(fst_channel, SWITCH_SIGNAL_BOND_VARIABLE, long_bond);
+			memset(uuid_str, 'x', sizeof(uuid_str));
+			uuid = switch_channel_get_partner_uuid_copy(fst_channel, uuid_str, sizeof(uuid_str));
+			fst_check(uuid == uuid_str);
+			fst_check(!memcmp(uuid_str, long_bond, SWITCH_UUID_FORMATTED_LENGTH));
+			fst_check(uuid_str[SWITCH_UUID_FORMATTED_LENGTH] == '\0');
+
+			switch_channel_set_variable(fst_channel, SWITCH_SIGNAL_BOND_VARIABLE, NULL);
+			switch_channel_set_variable(fst_channel, SWITCH_ORIGINATE_SIGNAL_BOND_VARIABLE, NULL);
+		}
+		FST_SESSION_END()
 	}
 	FST_SUITE_END()
 }