Changeset fed4f76


Ignore:
Timestamp:
2015-01-16T19:50:25Z (9 years ago)
Author:
dequis <dx@…>
Branches:
master
Children:
ecbd22a
Parents:
4cff28f
Message:

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)

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • lib/misc.c

    r4cff28f rfed4f76  
    780780        return NULL;
    781781}
     782
     783/* Takes a string, truncates it where it's safe, returns the new length */
     784int truncate_utf8( char *string, int maxlen )
     785{
     786        char *end;
     787        g_utf8_validate( (const gchar *) string, maxlen, (const gchar **) &end );
     788        *end = '\0';
     789        return end - string;
     790}
  • lib/misc.h

    r4cff28f rfed4f76  
    150150G_MODULE_EXPORT char **split_command_parts( char *command, int limit );
    151151G_MODULE_EXPORT char *get_rfc822_header( const char *text, const char *header, int len );
     152G_MODULE_EXPORT int truncate_utf8( char *string, int maxlen );
    152153
    153154#endif
  • nick.c

    r4cff28f rfed4f76  
    227227        {
    228228                nick_strip( irc, rets );
    229                 rets[MAX_NICK_LENGTH] = '\0';
     229                truncate_utf8( rets, MAX_NICK_LENGTH );
    230230                return rets;
    231231        }
     
    252252                else
    253253                {
    254                         nick[0] ++;
     254                        /* We've got no more space for underscores,
     255                           so truncate it and replace the last three
     256                           chars with a random "_XX" suffix */
     257                        int len = truncate_utf8( nick, MAX_NICK_LENGTH - 3 );
     258                        nick[len] = '_';
     259                        g_snprintf(nick + len + 1, 3, "%2x", rand() );
    255260                }
    256261               
     
    400405                if( strlen( down ) > strlen( nick ) )
    401406                {
    402                         /* Well crap. Corrupt it if we have to. */
    403                         down[strlen(nick)] = '\0';
     407                        truncate_utf8( down, strlen(nick) );
    404408                }
    405409                strcpy( nick, down );
Note: See TracChangeset for help on using the changeset viewer.