Fix silent failure of sizeof for STRBUF_INIT()

This commit is contained in:
Hamish Coleman 2023-06-25 13:40:00 +01:00
parent 42a716ae7b
commit 0d005d189d
4 changed files with 9 additions and 9 deletions

View File

@ -381,7 +381,7 @@ static void handleMgmtJson (mgmt_req_t *req, char *udp_buf, const int recvlen) {
traceEvent(TRACE_DEBUG, "mgmt json %s", cmdlinebuf); traceEvent(TRACE_DEBUG, "mgmt json %s", cmdlinebuf);
/* we reuse the buffer already on the stack for all our strings */ /* we reuse the buffer already on the stack for all our strings */
STRBUF_INIT(buf, udp_buf); STRBUF_INIT(buf, udp_buf, N2N_SN_PKTBUF_SIZE);
mgmt_req_init2(req, buf, (char *)&cmdlinebuf); mgmt_req_init2(req, buf, (char *)&cmdlinebuf);
@ -476,7 +476,7 @@ void readFromMgmtSocket (n2n_edge_t *eee) {
if((0 == memcmp(udp_buf, "help", 4)) || (0 == memcmp(udp_buf, "?", 1))) { if((0 == memcmp(udp_buf, "help", 4)) || (0 == memcmp(udp_buf, "?", 1))) {
strbuf_t *buf; strbuf_t *buf;
STRBUF_INIT(buf, &udp_buf); STRBUF_INIT(buf, &udp_buf, sizeof(udp_buf));
msg_len = snprintf(buf->str, buf->size, msg_len = snprintf(buf->str, buf->size,
"Help for edge management console:\n" "Help for edge management console:\n"
"\tstop | Gracefully exit edge\n" "\tstop | Gracefully exit edge\n"
@ -505,7 +505,7 @@ void readFromMgmtSocket (n2n_edge_t *eee) {
traceEvent(TRACE_NORMAL, "+verb traceLevel=%u", (unsigned int) getTraceLevel()); traceEvent(TRACE_NORMAL, "+verb traceLevel=%u", (unsigned int) getTraceLevel());
strbuf_t *buf; strbuf_t *buf;
STRBUF_INIT(buf, &udp_buf); STRBUF_INIT(buf, &udp_buf, sizeof(udp_buf));
msg_len = snprintf(buf->str, buf->size, msg_len = snprintf(buf->str, buf->size,
"> +OK traceLevel=%u\n", (unsigned int) getTraceLevel()); "> +OK traceLevel=%u\n", (unsigned int) getTraceLevel());
@ -516,7 +516,7 @@ void readFromMgmtSocket (n2n_edge_t *eee) {
if(0 == memcmp(udp_buf, "-verb", 5)) { if(0 == memcmp(udp_buf, "-verb", 5)) {
strbuf_t *buf; strbuf_t *buf;
STRBUF_INIT(buf, &udp_buf); STRBUF_INIT(buf, &udp_buf, sizeof(udp_buf));
if(getTraceLevel() > 0) { if(getTraceLevel() > 0) {
setTraceLevel(getTraceLevel() - 1); setTraceLevel(getTraceLevel() - 1);

View File

@ -102,7 +102,7 @@ void mgmt_event_post2 (enum n2n_event_topic topic, int data0, void *data1, mgmt_
char buf_space[100]; char buf_space[100];
strbuf_t *buf; strbuf_t *buf;
STRBUF_INIT(buf, buf_space); STRBUF_INIT(buf, buf_space, sizeof(buf_space));
char *tag; char *tag;
if(sub->type == N2N_MGMT_SUB) { if(sub->type == N2N_MGMT_SUB) {

View File

@ -240,7 +240,7 @@ static void handleMgmtJson (mgmt_req_t *req, char *udp_buf, const int recvlen) {
/* we reuse the buffer already on the stack for all our strings */ /* we reuse the buffer already on the stack for all our strings */
// xx // xx
STRBUF_INIT(buf, udp_buf); STRBUF_INIT(buf, udp_buf, N2N_SN_PKTBUF_SIZE);
mgmt_req_init2(req, buf, (char *)&cmdlinebuf); mgmt_req_init2(req, buf, (char *)&cmdlinebuf);

View File

@ -14,10 +14,10 @@ typedef struct strbuf {
} strbuf_t; } strbuf_t;
// Initialise the strbuf pointer buf to point at the storage area p // Initialise the strbuf pointer buf to point at the storage area p
// p must be a known sized object // of size buflen
#define STRBUF_INIT(buf,p) do { \ #define STRBUF_INIT(buf,p,buflen) do { \
buf = (void *)p; \ buf = (void *)p; \
buf->size = sizeof(*p) - sizeof(size_t); \ buf->size = buflen - sizeof(size_t); \
} while(0) } while(0)