# HG changeset patch # User Daniel Atallah # Date 1222735793 0 # Node ID 9340e1db0f46b839cecbe690e0f054e1cb19380b # Parent 23cffa95216dbf1cd491a520ca493434d4fdeac7 Prevent DNS-SD TXT record strings from exceeding the maximum length. Fixes #7242 diff -r 23cffa95216d -r 9340e1db0f46 libpurple/protocols/bonjour/mdns_common.c --- a/libpurple/protocols/bonjour/mdns_common.c Mon Sep 29 17:17:38 2008 +0000 +++ b/libpurple/protocols/bonjour/mdns_common.c Tue Sep 30 00:49:53 2008 +0000 @@ -46,6 +46,30 @@ g_free(data); } +#define MAX_TXT_CONSTITUENT_LEN 255 + +/* Make sure that the value isn't longer than it is supposed to be */ +static const char* +get_max_txt_record_value(const char *key, const char *value) +{ + /* "each constituent string of a DNS TXT record is limited to 255 bytes" + * This includes the key and the '=' + */ + static char buffer[MAX_TXT_CONSTITUENT_LEN + 1]; + gchar *end_valid = NULL; + int len = MIN(strlen(value), MAX_TXT_CONSTITUENT_LEN - (strlen(key) + 2)); + + strncpy(buffer, value, len); + + buffer[len] = '\0'; + + /* If we've cut part of a utf-8 character, kill it */ + if (!g_utf8_validate(buffer, -1, (const gchar **)&end_valid)) + *end_valid = '\0'; + + return buffer; +} + static GSList *generate_presence_txt_records(BonjourDnsSd *data) { GSList *ret = NULL; PurpleKeyValuePair *kvp; @@ -62,13 +86,20 @@ #define _M_ADD_R(k, v) \ kvp = g_new0(PurpleKeyValuePair, 1); \ kvp->key = g_strdup(k); \ - kvp->value = g_strdup(v); \ + kvp->value = g_strdup(get_max_txt_record_value(k, v)); \ ret = g_slist_prepend(ret, kvp); \ /* We should try to follow XEP-0174, but some clients have "issues", so we humor them. * See http://telepathy.freedesktop.org/wiki/SalutInteroperability */ + /* Large TXT records are problematic. + * While it is technically possible for this to exceed a standard 512-byte + * DNS message, it shouldn't happen unless we get wacky data entered for + * some of the freeform fields. It is even less likely to exceed the + * recommended maximum of 1300 bytes. + */ + /* Needed by iChat */ _M_ADD_R("txtvers", "1") /* Needed by Gaim/Pidgin <= 2.0.1 (remove at some point) */