diff --git a/runtime/include/http_session.h b/runtime/include/http_session.h index c2cb51a..74c0395 100644 --- a/runtime/include/http_session.h +++ b/runtime/include/http_session.h @@ -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; } diff --git a/runtime/src/libc/wasi_impl_serverless.c b/runtime/src/libc/wasi_impl_serverless.c index 2c995e1..32063b6 100644 --- a/runtime/src/libc/wasi_impl_serverless.c +++ b/runtime/src/libc/wasi_impl_serverless.c @@ -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) {