Skip to content

Pool::create_buffer cannot be filled through the safe API #327

Description

@u5surf

Pool::create_buffer(n) allocates n bytes, but there is no safe way to put
anything into them:

let mut buf = request.pool().create_buffer(body.len()).unwrap();
buf.as_bytes_mut()[..body.len()].copy_from_slice(body); // panics

ngx_create_temp_buf leaves pos == last, so Buffer::len() is 0 and
MutableBuffer::as_bytes_mut() hands back an empty slice. That is consistent
with as_bytes_mut meaning "the contents", but it leaves the buffer
unfillable: neither trait exposes the capacity or a way to set the length, and
TemporaryBuffer has no inherent methods besides from_ngx_buf. The only
route is as_ngx_buf_mut() and advancing last by hand, which drops out of
the safe API.

create_buffer_from_str works and covers most response bodies, so this is not
urgent. It does mean create_buffer currently has no safe use, while the
obvious composition above compiles and then takes down the worker.

Something like TemporaryBuffer::append(&mut self, &[u8]) -> usize, or
exposing capacity next to a length setter, would close it.

I'd like to work on this issue.

Environment

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions