From fed4f766c05e44e99917909b266c99c052ed9c3e Mon Sep 17 00:00:00 2001 From: dequis Date: Fri, 16 Jan 2015 16:50:25 -0300 Subject: Fix UTF8 nick truncation issues When nicks exceeded the length limit, they were cut at 24 bytes and that sometimes left invalid utf8 at the end, which made the nick_ok() validation fail and often broke those nicks completely. This adds a truncate_utf8 function to cut the string at a safe place Also, the method to deduplicate nicks when there's no more place to add underscores was changed to add "_XX" at the end, where XX are two random hex chars. The previous method in those cases was increasing the value of the first character of the nick... which leads to silly and confusing results (i.e. FacebookUser -> GacebookUser) --- nick.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'nick.c') diff --git a/nick.c b/nick.c index 63140042..2c3f9a66 100644 --- a/nick.c +++ b/nick.c @@ -226,7 +226,7 @@ char *nick_gen( bee_user_t *bu ) if( ok && rets && *rets ) { nick_strip( irc, rets ); - rets[MAX_NICK_LENGTH] = '\0'; + truncate_utf8( rets, MAX_NICK_LENGTH ); return rets; } g_free( rets ); @@ -251,7 +251,12 @@ void nick_dedupe( bee_user_t *bu, char nick[MAX_NICK_LENGTH+1] ) } else { - nick[0] ++; + /* We've got no more space for underscores, + so truncate it and replace the last three + chars with a random "_XX" suffix */ + int len = truncate_utf8( nick, MAX_NICK_LENGTH - 3 ); + nick[len] = '_'; + g_snprintf(nick + len + 1, 3, "%2x", rand() ); } if( inf_protection-- == 0 ) @@ -399,8 +404,7 @@ int nick_lc( irc_t *irc, char *nick ) gchar *down = g_utf8_strdown( nick, -1 ); if( strlen( down ) > strlen( nick ) ) { - /* Well crap. Corrupt it if we have to. */ - down[strlen(nick)] = '\0'; + truncate_utf8( down, strlen(nick) ); } strcpy( nick, down ); g_free( down ); -- cgit v1.2.3