]> jfr.im git - solanum.git/commitdiff
pretty_mask(): Use explicit lengths instead of temporarily writing '\0'.
authorJilles Tjoelker <redacted>
Fri, 10 Oct 2014 21:05:41 +0000 (23:05 +0200)
committerJilles Tjoelker <redacted>
Thu, 15 Jan 2015 22:38:50 +0000 (23:38 +0100)
This is slightly simpler and should fix Coverity warnings.

src/chmode.c

index 062cda58873a215a685b264f216a714072b3e250..7774529a5068ac4ec9099ffda77a6a219f9f7489 100644 (file)
@@ -374,18 +374,17 @@ pretty_mask(const char *idmask)
 {
        static char mask_buf[BUFSIZE];
        int old_mask_pos;
-       char *nick, *user, *host, *forward = NULL;
-       char splat[] = "*";
+       const char *nick, *user, *host, *forward = NULL;
        char *t, *at, *ex, *ex2;
-       char ne = 0, ue = 0, he = 0, fe = 0;    /* save values at nick[NICKLEN], et all */
-       char e2 = 0;                            /* save value that delimits forward channel */
+       int nl, ul, hl, fl;
+       char e2 = 0;
        char *mask;
 
        mask = LOCAL_COPY(idmask);
        mask = check_string(mask);
        collapse(mask);
 
-       nick = user = host = splat;
+       nick = user = host = "*";
 
        if((size_t) BUFSIZE - mask_pos < strlen(mask) + 5)
                return NULL;
@@ -457,31 +456,29 @@ pretty_mask(const char *idmask)
        }
 
        /* truncate values to max lengths */
-       if(strlen(nick) > NICKLEN - 1)
-       {
-               ne = nick[NICKLEN - 1];
-               nick[NICKLEN - 1] = '\0';
-       }
-       if(strlen(user) > USERLEN)
-       {
-               ue = user[USERLEN];
-               user[USERLEN] = '\0';
-       }
-       if(strlen(host) > HOSTLEN)
-       {
-               he = host[HOSTLEN];
-               host[HOSTLEN] = '\0';
+       nl = strlen(nick);
+       if(nl > NICKLEN - 1)
+               nl = NICKLEN - 1;
+       ul = strlen(user);
+       if(ul > USERLEN)
+               ul = USERLEN;
+       hl = strlen(host);
+       if(hl > HOSTLEN)
+               hl = HOSTLEN;
+       fl = forward ? strlen(forward) : 0;
+       if(fl > CHANNELLEN)
+               fl = CHANNELLEN;
+
+       memcpy(mask_buf + mask_pos, nick, nl), mask_pos += nl;
+       mask_buf[mask_pos++] = '!';
+       memcpy(mask_buf + mask_pos, user, ul), mask_pos += ul;
+       mask_buf[mask_pos++] = '@';
+       memcpy(mask_buf + mask_pos, host, hl), mask_pos += hl;
+       if (forward) {
+               mask_buf[mask_pos++] = '$';
+               memcpy(mask_buf + mask_pos, forward, fl), mask_pos += fl;
        }
-       if(forward && strlen(forward) > CHANNELLEN)
-       {
-               fe = forward[CHANNELLEN];
-               forward[CHANNELLEN] = '\0';
-       }
-
-       if (forward)
-               mask_pos += rb_sprintf(mask_buf + mask_pos, "%s!%s@%s$%s", nick, user, host, forward) + 1;
-       else
-               mask_pos += rb_sprintf(mask_buf + mask_pos, "%s!%s@%s", nick, user, host) + 1;
+       mask_buf[mask_pos++] = '\0';
 
        /* restore mask, since we may need to use it again later */
        if(at)
@@ -490,14 +487,6 @@ pretty_mask(const char *idmask)
                *ex = '!';
        if(ex2)
                *ex2 = e2;
-       if(ne)
-               nick[NICKLEN - 1] = ne;
-       if(ue)
-               user[USERLEN] = ue;
-       if(he)
-               host[HOSTLEN] = he;
-       if(fe)
-               forward[CHANNELLEN] = fe;
 
        return mask_buf + old_mask_pos;
 }