Changeset 124a071 in code


Ignore:
Timestamp:
Aug 30, 2017, 12:11:44 AM (8 years ago)
Author:
Mike Belopuhov <mike@…>
Branches:
master
Children:
614260f
Parents:
d8c3967
Message:

Use the same pattern when creating Status and Error messages

File:
1 edited

Legend:

Unmodified
Added
Removed
  • icb.c

    rd8c3967 r124a071  
    401401        va_list ap;
    402402        char buf[ICB_MSGSIZE];
    403         int res, buflen = 1;
     403        int res, buflen;
    404404        static const struct {
    405405                int              type;
     
    429429                return;
    430430        }
    431         buflen += MIN((size_t)res, sizeof buf - 1);
    432         if ((size_t)buflen >= sizeof buf) {
     431        if ((size_t)res >= sizeof buf) {
    433432                icbd_log(NULL, LOG_ERR, "Status buffer too small");
    434433                return;
    435434        }
     435        buflen = MIN((size_t)res + 1, sizeof buf - 1);
    436436        va_start(ap, fmt);
    437         res = vsnprintf(&buf[buflen], sizeof buf - buflen, fmt, ap);
     437        res = vsnprintf(&buf[buflen], sizeof buf - buflen - 1, fmt, ap);
    438438        va_end(ap);
     439        buflen--; /* buf[buflen] was overwritten */
    439440        if (res < 0) {
    440                 icbd_log(NULL, LOG_ERR, "Format error in %s", __func__);
    441                 return;
    442         }
    443         buflen += MIN((size_t)res, sizeof buf - buflen);
     441                icbd_log(NULL, LOG_ERR, "Message format error in %s", __func__);
     442                return;
     443        }
     444        if ((size_t)res + buflen >= sizeof buf) {
     445                icbd_log(NULL, LOG_ERR, "Status message too long");
     446                return;
     447        }
     448        buflen += MIN((size_t)res + 1, sizeof buf - buflen - 1);
    444449        buf[0] = buflen;
    445450        icbd_send(is, buf, buflen + 1);
     
    478483        char buf[ICB_MSGSIZE];
    479484        va_list ap;
    480         int res, buflen = 1;
     485        int res, buflen;
    481486
    482487        va_start(ap, fmt);
     
    487492                return;
    488493        }
    489         buflen += MIN((size_t)res, sizeof buf - 2);
     494        buflen = MIN((size_t)res + 1, sizeof buf - 2);
    490495        buf[0] = ++buflen; /* account for ICB_M_ERROR */
    491496        buf[1] = ICB_M_ERROR;
Note: See TracChangeset for help on using the changeset viewer.