handle possible memory trashes on mg_send() and mg_iobuf_add() failures

This commit is contained in:
Sergio R. Caprile 2024-07-09 14:55:47 -03:00
parent ad3acf355e
commit 7f7c1ff6c2
2 changed files with 16 additions and 16 deletions

View File

@ -16603,9 +16603,9 @@ size_t mg_ws_send(struct mg_connection *c, const void *buf, size_t len,
int op) {
uint8_t header[14];
size_t header_len = mkhdr(len, op, c->is_client, header);
mg_send(c, header, header_len);
if (!mg_send(c, header, header_len)) return 0;
if (!mg_send(c, buf, len)) return header_len;
MG_VERBOSE(("WS out: %d [%.*s]", (int) len, (int) len, buf));
mg_send(c, buf, len);
mg_ws_mask(c, len);
return header_len + len;
}
@ -16757,12 +16757,12 @@ size_t mg_ws_wrap(struct mg_connection *c, size_t len, int op) {
size_t header_len = mkhdr(len, op, c->is_client, header);
// NOTE: order of operations is important!
mg_iobuf_add(&c->send, c->send.len, NULL, header_len);
if (mg_iobuf_add(&c->send, c->send.len, NULL, header_len) != 0) {
p = &c->send.buf[c->send.len - len]; // p points to data
memmove(p, p - header_len, len); // Shift data
memcpy(p - header_len, header, header_len); // Prepend header
mg_ws_mask(c, len); // Mask data
}
return c->send.len;
}

View File

@ -133,9 +133,9 @@ size_t mg_ws_send(struct mg_connection *c, const void *buf, size_t len,
int op) {
uint8_t header[14];
size_t header_len = mkhdr(len, op, c->is_client, header);
mg_send(c, header, header_len);
if (!mg_send(c, header, header_len)) return 0;
if (!mg_send(c, buf, len)) return header_len;
MG_VERBOSE(("WS out: %d [%.*s]", (int) len, (int) len, buf));
mg_send(c, buf, len);
mg_ws_mask(c, len);
return header_len + len;
}
@ -287,11 +287,11 @@ size_t mg_ws_wrap(struct mg_connection *c, size_t len, int op) {
size_t header_len = mkhdr(len, op, c->is_client, header);
// NOTE: order of operations is important!
mg_iobuf_add(&c->send, c->send.len, NULL, header_len);
if (mg_iobuf_add(&c->send, c->send.len, NULL, header_len) != 0) {
p = &c->send.buf[c->send.len - len]; // p points to data
memmove(p, p - header_len, len); // Shift data
memcpy(p - header_len, header, header_len); // Prepend header
mg_ws_mask(c, len); // Mask data
}
return c->send.len;
}