pretty_mask(): Use explicit lengths instead of temporarily writing '\0'.

This is slightly simpler and should fix Coverity warnings.
This commit is contained in:
Jilles Tjoelker 2014-10-10 23:05:41 +02:00
parent 7db54a1f97
commit f4e893b515

View file

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