Changeset f4396c4 for irc_send.c


Ignore:
Timestamp:
2015-12-06T00:43:14Z (5 years ago)
Author:
dequis <dx@…>
Branches:
master
Children:
c54bb11
Parents:
ed431c3
Message:

irc_send_names: refactor to use GString instead of the stack

Because modifying this code was making me REALLY uncomfortable.

This still only allocates memory once. Needing to extend the string
would be a bug in the length checks, but at least that's harmless now.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • irc_send.c

    red431c3 rf4396c4  
    202202}
    203203
     204#define IRC_NAMES_LEN 385
     205
    204206void irc_send_names(irc_channel_t *ic)
    205207{
    206208        GSList *l;
    207         char namelist[385] = "";
     209        GString *namelist = g_string_sized_new(IRC_NAMES_LEN);
    208210
    209211        /* RFCs say there is no error reply allowed on NAMES, so when the
     
    212214                irc_channel_user_t *icu = l->data;
    213215                irc_user_t *iu = icu->iu;
    214 
    215                 if (strlen(namelist) + strlen(iu->nick) > sizeof(namelist) - 4) {
    216                         irc_send_num(ic->irc, 353, "= %s :%s", ic->name, namelist);
    217                         *namelist = 0;
    218                 }
    219 
    220                 namelist[strlen(namelist) + 1] = '\0';
    221                 namelist[strlen(namelist)] = irc_channel_user_get_prefix(icu);
    222 
    223                 strcat(namelist, iu->nick);
    224                 strcat(namelist, " ");
    225         }
    226 
    227         if (*namelist) {
    228                 irc_send_num(ic->irc, 353, "= %s :%s", ic->name, namelist);
     216                size_t extra_len = strlen(iu->nick);
     217                char prefix;
     218
     219                if (namelist->len + extra_len > IRC_NAMES_LEN - 4) {
     220                        irc_send_num(ic->irc, 353, "= %s :%s", ic->name, namelist->str);
     221                        g_string_truncate(namelist, 0);
     222                }
     223
     224                if ((prefix = irc_channel_user_get_prefix(icu))) {
     225                        g_string_append_c(namelist, prefix);
     226                }
     227
     228                g_string_append(namelist, iu->nick);
     229                g_string_append_c(namelist, ' ');
     230        }
     231
     232        if (namelist->len) {
     233                irc_send_num(ic->irc, 353, "= %s :%s", ic->name, namelist->str);
    229234        }
    230235
    231236        irc_send_num(ic->irc, 366, "%s :End of /NAMES list", ic->name);
     237
     238        g_string_free(namelist, TRUE);
    232239}
    233240
Note: See TracChangeset for help on using the changeset viewer.