From ef8f25d1dd8905e56f8f20adf0bda4ecd4e19e40 Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch
Date: Mon, 29 Aug 2022 14:40:06 +0200 Subject: xC: deal with any identifier conflicts Invalid UTF-8 converted to UTF-8 may conflict with that which was valid UTF-8 in the first place. --- xC.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/xC.c b/xC.c index 9664cb7..5b8dda2 100644 --- a/xC.c +++ b/xC.c @@ -4622,11 +4622,24 @@ irc_make_buffer_name (struct server *s, const char *target) if (!target) return xstrdup (s->name); - // XXX: this may be able to trigger the uniqueness assertion with non-UTF-8 char *target_utf8 = irc_to_utf8 (target); - char *result = xstrdup_printf ("%s.%s", s->name, target_utf8); + char *name = xstrdup_printf ("%s.%s", s->name, target_utf8); free (target_utf8); - return result; + + struct buffer *conflict = buffer_by_name (s->ctx, name); + if (!conflict) + return name; + + hard_assert (conflict->server == s); + + // Fix up any conflicts. Note that while parentheses aren't allowed + // in IRC nicknames, they may occur in channel names. + int i = 0; + char *unique = xstrdup_printf ("%s(%d)", name, ++i); + while (buffer_by_name (s->ctx, unique)) + cstr_set (&unique, xstrdup_printf ("%s(%d)", name, ++i)); + free (name); + return unique; } static void -- cgit v1.2.3-70-g09d2