Changeset 65b48ee in code


Ignore:
Timestamp:
Apr 22, 2015, 5:29:05 PM (10 years ago)
Author:
Mike Belopuhov <mike@…>
Branches:
master
Children:
8b1683e
Parents:
7ff6405
Message:

Improve [v]snprintf return value handling

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icb.c

    r7ff6405 r65b48ee  
    256256        struct icb_group *ig = is->group;
    257257        struct icb_session *s;
    258         int buflen = 1;
     258        int res, buflen = 1;
    259259
    260260        if (strlen(msg) == 0) {
     
    263263        }
    264264
    265         buflen += snprintf(&buf[1], sizeof buf - 1, "%c%s%c%s", ICB_M_OPEN,
     265        res = snprintf(&buf[1], sizeof buf - 1, "%c%s%c%s", ICB_M_OPEN,
    266266            is->nick, ICB_M_SEP, msg);
     267        if (res < 0) {
     268                icb_error(is, "Format error");
     269                return;
     270        }
     271        buflen += MIN((size_t)res, sizeof buf - 1);
    267272        buf[0] = buflen;
    268273
     
    373378        va_list ap;
    374379        char buf[ICB_MSGSIZE];
    375         int buflen = 1;
     380        int res, buflen = 1;
    376381        static const struct {
    377382                int              type;
     
    395400        if (type < 0 || type > (int)nitems(msgtab) - 1)
    396401                return;
     402        res = snprintf(&buf[1], sizeof buf - 1, "%c%s%c", ICB_M_STATUS,
     403            msgtab[type].msg, ICB_M_SEP);
     404        if (res < 0) {
     405                icbd_log(NULL, LOG_ERR, "Format error in %s", __func__);
     406                return;
     407        }
     408        buflen += MIN((size_t)res, sizeof buf - 1);
     409        if ((size_t)buflen >= sizeof buf) {
     410                icbd_log(NULL, LOG_ERR, "Status buffer too small");
     411                return;
     412        }
    397413        va_start(ap, fmt);
    398         buflen += snprintf(&buf[1], sizeof buf - 1, "%c%s%c", ICB_M_STATUS,
    399             msgtab[type].msg, ICB_M_SEP);
    400         buflen += vsnprintf(&buf[buflen], sizeof buf - buflen, fmt, ap);
     414        res = vsnprintf(&buf[buflen], sizeof buf - buflen, fmt, ap);
     415        va_end(ap);
     416        if (res < 0) {
     417                icbd_log(NULL, LOG_ERR, "Format error in %s", __func__);
     418                return;
     419        }
     420        buflen += MIN((size_t)res, sizeof buf - buflen);
    401421        buf[0] = buflen;
    402         va_end(ap);
    403422        icbd_send(is, buf, buflen + 1);
    404423}
     
    436455        char buf[ICB_MSGSIZE];
    437456        va_list ap;
    438         int buflen = 1;
     457        int res, buflen = 1;
    439458
    440459        va_start(ap, fmt);
    441         buflen += vsnprintf(&buf[2], sizeof buf - 2, fmt, ap);
     460        res = vsnprintf(&buf[2], sizeof buf - 2, fmt, ap);
    442461        va_end(ap);
     462        if (res < 0) {
     463                icbd_log(NULL, LOG_ERR, "Format error");
     464                return;
     465        }
     466        buflen += MIN((size_t)res, sizeof buf - 2);
    443467        buf[0] = ++buflen; /* account for ICB_M_ERROR */
    444468        buf[1] = ICB_M_ERROR;
     
    625649        char buf[ICB_MSGSIZE];
    626650        va_list ap;
    627         int buflen = 1;
     651        int res, buflen = 1;
    628652
    629653        va_start(ap, fmt);
    630         buflen += vsnprintf(&buf[1], sizeof buf - 1, fmt, ap);
     654        res = vsnprintf(&buf[1], sizeof buf - 1, fmt, ap);
    631655        va_end(ap);
     656        if (res < 0) {
     657                icbd_log(NULL, LOG_ERR, "Format error in %s", __func__);
     658                return;
     659        }
     660        buflen += MIN((size_t)res, sizeof buf - 1);
    632661        buf[0] = buflen;
    633662        icbd_send(is, buf, buflen + 1);
Note: See TracChangeset for help on using the changeset viewer.