refactor: Increase buffer by explicit size

master
Sean McBride 3 years ago
parent 32a099ceec
commit 41a087394e

@ -313,8 +313,6 @@ http_session_receive_request(struct http_session *session, http_session_receive_
goto err;
}
assert(bytes_parsed == bytes_received);
session->http_request.length_parsed += bytes_parsed;
}
@ -353,8 +351,10 @@ http_session_write_response(struct http_session *session, const uint8_t *source,
int rc = 0;
if (session->response_buffer.capacity - session->response_buffer.length < n) {
rc = vec_u8_grow(&session->response_buffer);
size_t buffer_remaining = session->response_buffer.capacity - session->response_buffer.length;
if (buffer_remaining < n) {
rc = vec_u8_resize(&session->response_buffer, session->response_buffer.capacity + n - buffer_remaining);
if (rc != 0) goto DONE;
}

@ -791,6 +791,19 @@ wasi_snapshot_preview1_backing_fd_write(wasi_context_t *context, __wasi_fd_t fd,
__wasi_size_t nwritten = 0;
int rc = 0;
/* Precompute required buffer size for entire iovec to call realloc at most once */
size_t total_size_to_copy = 0;
for (size_t i = 0; i < iovs_len; i++) { total_size_to_copy += iovs[i].buf_len; }
buffer_remaining = s->http->response_buffer.capacity - s->http->response_buffer.length;
if (buffer_remaining < total_size_to_copy) {
rc = vec_u8_resize(&s->http->response_buffer,
s->http->response_buffer.capacity + total_size_to_copy - buffer_remaining);
assert(rc == 0);
}
assert(s->http->response_buffer.capacity - s->http->response_buffer.length >= total_size_to_copy);
for (size_t i = 0; i < iovs_len; i++) {
#ifdef LOG_SANDBOX_STDERR
if (fd == STDERR_FILENO) {

Loading…
Cancel
Save