mirror of
https://github.com/cesanta/mongoose.git
synced 2024-12-27 15:01:03 +08:00
handle possible memory trashes on mg_send() and mg_iobuf_add() failures
This commit is contained in:
parent
ad3acf355e
commit
7f7c1ff6c2
16
mongoose.c
16
mongoose.c
@ -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);
|
||||
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
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
16
src/ws.c
16
src/ws.c
@ -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);
|
||||
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
|
||||
|
||||
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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user