Commit 384c5ded67 for asterisk.org

commit 384c5ded679546b0d1a437496902eea573e45260
Author: phoneben <3232963@gmail.com>
Date:   Sat Oct 31 22:51:25 2026 +0200

    app_voicemail, callerid: Preserve caller ID numbers with long UTF-8 names

    Caller IDs with multibyte UTF-8 names, such as Hebrew or Arabic, use
    two or more bytes per character, so "name" <number> commonly exceeds
    79 bytes.  Fixed-size buffers then cut the value, often inside the
    number, leaving it without its closing '>', so ast_callerid_parse()
    treats the whole value as a name and the number is lost.

    * app_voicemail: odbc_retrieve_message() fetched every non-recording
      column into an 80 byte buffer, so envelope/saycid playback could not
      announce the number.  The buffer is now 512 bytes, enough for an 80
      character utf8mb4 column.

    * callerid: ast_callerid_merge() now shortens the name, rather than
      cutting the end of the string, when "name" <number> does not fit in
      the destination, so the full number is kept whenever <number> fits.
      The name is cut on a UTF-8 character boundary and never inside an
      escape sequence.  If there is no room for any of the name the result
      is <number>; if even that does not fit, the number is truncated as
      before.  Output is unchanged whenever the full value fits.

      The escaped name buffer is increased from 128 to 512 bytes, as it
      capped the name at 127 bytes regardless of the destination size.

    AI assistance: Claude (Anthropic)

    Resolves: #2263

diff --git a/apps/app_voicemail.c b/apps/app_voicemail.c
index 258d294471..329c34e8d9 100644
--- a/apps/app_voicemail.c
+++ b/apps/app_voicemail.c
@@ -4204,7 +4204,7 @@ static int odbc_retrieve_message(char *dir, int msgnum)
 	SQLULEN colsize;
 	SQLLEN colsize2;
 	FILE *f = NULL;
-	char rowdata[80];
+	char rowdata[512];
 	char *fn = MAKE_FILE_PTRA(dir, msgnum);
 	char *full_fn = MAKE_FILE_EXT_PTRA(dir, msgnum, "txt");
 	char msgnums[80];
diff --git a/main/callerid.c b/main/callerid.c
index 381e83a9b8..6f2cea49aa 100644
--- a/main/callerid.c
+++ b/main/callerid.c
@@ -40,6 +40,7 @@
 #include "asterisk/callerid.h"
 #include "asterisk/fskmodem.h"
 #include "asterisk/utils.h"
+#include "asterisk/utf8.h"
 #include "asterisk/format_cache.h"

 struct callerid_state {
@@ -1272,13 +1273,43 @@ int ast_callerid_callwaiting_full_tz_generate(unsigned char *buf, const char *na

 char *ast_callerid_merge(char *buf, int bufsiz, const char *name, const char *num, const char *unknown)
 {
+	if (bufsiz <= 0) {
+		return buf;
+	}
 	if (!unknown)
 		unknown = "<unknown>";
 	if (name && num) {
-		char name_buf[128];
+		char name_buf[512];
+		/* Room left for the name after the number, quotes, " <", ">" and NUL */
+		int room = bufsiz - (int) strlen(num) - 6;
+
+		if (room > 0) {
+			size_t len;
+
+			ast_escape_quoted(name, name_buf, sizeof(name_buf));
+			len = strlen(name_buf);
+			if (len > (size_t) room || len == sizeof(name_buf) - 1) {
+				char escaped[512];
+
+				/*
+				 * Preserve the full number when it fits, shortening the name.
+				 * ast_escape_quoted() never splits an escape sequence and
+				 * ast_utf8_copy_string() never splits a UTF-8 character.
+				 */
+				ast_escape_quoted(name, escaped, MIN((int) sizeof(escaped), room + 1));
+				ast_utf8_copy_string(name_buf, escaped, sizeof(name_buf));
+			}
+		}

-		ast_escape_quoted(name, name_buf, sizeof(name_buf));
-		snprintf(buf, bufsiz, "\"%s\" <%s>", name_buf, num);
+		if (room > 0 && (!ast_strlen_zero(name_buf) || ast_strlen_zero(name))) {
+			snprintf(buf, bufsiz, "\"%s\" <%s>", name_buf, num);
+		} else if (bufsiz >= (int) strlen(num) + 3) {
+			/* No room for any of the name, keep the number in its brackets */
+			snprintf(buf, bufsiz, "<%s>", num);
+		} else {
+			/* Not even <number> fits, truncate the number as before */
+			ast_copy_string(buf, num, bufsiz);
+		}
 	} else if (name) {
 		ast_copy_string(buf, name, bufsiz);
 	} else if (num) {
diff --git a/tests/test_callerid.c b/tests/test_callerid.c
index c7aafacf9c..efc699b689 100644
--- a/tests/test_callerid.c
+++ b/tests/test_callerid.c
@@ -150,10 +150,134 @@ AST_TEST_DEFINE(parse_off_nominal)
 	return AST_TEST_PASS;
 }

+struct cid_merge_set {
+	int bufsiz;
+	const char *name;
+	const char *number;
+	const char *expected;
+};
+
+/* Each Hebrew letter below is two bytes in UTF-8 */
+#define HEB_SHALOM "\xd7\xa9\xd7\x9c\xd7\x95\xd7\x9d"   /* shin lamed vav mem */
+#define HEB_SH_L   "\xd7\xa9\xd7\x9c"                   /* shin lamed */
+
+AST_TEST_DEFINE(merge)
+{
+	static const struct cid_merge_set cid_sets[] = {
+		/* Everything fits, output is unchanged */
+		{80, "name", "1234", "\"name\" <1234>"},
+		{80, "na\"me", "1234", "\"na\\\"me\" <1234>"},
+		{80, "", "1234", "\"\" <1234>"},
+		{80, HEB_SHALOM, "0501234567", "\"" HEB_SHALOM "\" <0501234567>"},
+		/* Exactly fits: 4 + 10 + 6 = 20 */
+		{20, "abcd", "0501234567", "\"abcd\" <0501234567>"},
+		/* Name is shortened so the full number is kept */
+		{20, "abcdefghijklmnopqrstuvwxyz", "0501234567", "\"abcd\" <0501234567>"},
+		/* Name is cut on a UTF-8 character boundary (room for 5 bytes) */
+		{21, HEB_SHALOM, "0501234567", "\"" HEB_SH_L "\" <0501234567>"},
+		/* Name is never cut inside an escape sequence (room for 3 bytes) */
+		{19, "ab\"cd", "0501234567", "\"ab\" <0501234567>"},
+		/* Room for one byte, which cannot hold a whole character */
+		{17, HEB_SHALOM, "0501234567", "<0501234567>"},
+		/* No room for any of the name, <number> just fits */
+		{13, "name", "0501234567", "<0501234567>"},
+		/* Not even <number> fits, the number is truncated */
+		{8, "name", "0501234567", "0501234"},
+		/* Name or number only */
+		{80, "name", NULL, "name"},
+		{80, NULL, "1234", "1234"},
+		{80, NULL, NULL, "<unknown>"},
+	};
+	/* Guard bytes before and after the destination catch any overrun */
+	char buf[1 + 600 + 16];
+	char *dst = buf + 1;
+	char long_name[256];
+	char expected[600];
+	int i;
+
+	switch (cmd) {
+	case TEST_INIT:
+		info->name = "merge";
+		info->category = "/main/callerid/";
+		info->summary = "Callerid merge unit test";
+		info->description =
+			"This tests merging of callerid name and number, including "
+			"shortening of long UTF-8 names so that the number is kept.";
+		return AST_TEST_NOT_RUN;
+	case TEST_EXECUTE:
+		break;
+	}
+
+	for (i = 0; i < ARRAY_LEN(cid_sets); i++) {
+		int j;
+
+		memset(buf, 'X', sizeof(buf));
+		ast_callerid_merge(dst, cid_sets[i].bufsiz, cid_sets[i].name, cid_sets[i].number, NULL);
+		if (strcmp(dst, cid_sets[i].expected)) {
+			ast_test_status_update(test,
+				"Case %d: expected '%s' instead of '%s'\n",
+				i, cid_sets[i].expected, dst);
+			return AST_TEST_FAIL;
+		}
+		if (buf[0] != 'X') {
+			ast_test_status_update(test, "Case %d: wrote before the buffer\n", i);
+			return AST_TEST_FAIL;
+		}
+		for (j = cid_sets[i].bufsiz; j < 600; j++) {
+			if (dst[j] != 'X') {
+				ast_test_status_update(test, "Case %d: wrote past bufsiz %d\n",
+					i, cid_sets[i].bufsiz);
+				return AST_TEST_FAIL;
+			}
+		}
+	}
+
+	/* A zero sized buffer must not be written to */
+	memset(buf, 'X', sizeof(buf));
+	ast_callerid_merge(dst, 0, "name", "1234", NULL);
+	ast_callerid_merge(dst, 0, "name", NULL, NULL);
+	ast_callerid_merge(dst, 0, NULL, "1234", NULL);
+	ast_callerid_merge(dst, 0, NULL, NULL, NULL);
+	for (i = 0; i < sizeof(buf); i++) {
+		if (buf[i] != 'X') {
+			ast_test_status_update(test, "Zero bufsiz: wrote at offset %d\n", i - 1);
+			return AST_TEST_FAIL;
+		}
+	}
+
+	/*
+	 * A 200 byte UTF-8 name, longer than the old 128 byte name buffer,
+	 * is kept whole when the destination is large enough (e.g. CDR clid).
+	 */
+	long_name[0] = '\0';
+	for (i = 0; i < 25; i++) {
+		strcat(long_name, HEB_SHALOM);
+	}
+	snprintf(expected, sizeof(expected), "\"%s\" <0501234567>", long_name);
+	ast_callerid_merge(dst, 256, long_name, "0501234567", NULL);
+	if (strcmp(dst, expected)) {
+		ast_test_status_update(test, "Long name: expected '%s' instead of '%s'\n",
+			expected, dst);
+		return AST_TEST_FAIL;
+	}
+
+	/* The same name in an 80 byte destination keeps the number */
+	ast_callerid_merge(dst, 80, long_name, "0501234567", NULL);
+	if (strlen(dst) >= 80 || !ast_ends_with(dst, "\" <0501234567>")
+		|| strncmp(dst + 1, long_name, strlen(dst) - 15)
+		|| (strlen(dst) - 15) % 2) {
+		ast_test_status_update(test, "Long name in 80 bytes: unexpected '%s'\n", dst);
+		return AST_TEST_FAIL;
+	}
+
+	return AST_TEST_PASS;
+}
+
 static int unload_module(void)
 {
 	AST_TEST_UNREGISTER(parse_nominal);
 	AST_TEST_UNREGISTER(parse_off_nominal);
+	AST_TEST_UNREGISTER(merge);
 	return 0;
 }

@@ -161,6 +285,7 @@ static int load_module(void)
 {
 	AST_TEST_REGISTER(parse_nominal);
 	AST_TEST_REGISTER(parse_off_nominal);
+	AST_TEST_REGISTER(merge);
 	return AST_MODULE_LOAD_SUCCESS;
 }