Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposing the buffer field of Consumer and Producer is unsound #26

Closed
caelunshun opened this issue Nov 18, 2020 · 1 comment · Fixed by #27
Closed

Exposing the buffer field of Consumer and Producer is unsound #26

caelunshun opened this issue Nov 18, 2020 · 1 comment · Fixed by #27

Comments

@caelunshun
Copy link

The Arc<RingBuffer> field on Consumer and Producer is made public, which is unsound. Overwriting the buffer field without updating head and tail can cause a read from uninitialized memory without unsafe code in downstream crates.

If the RingBuffer needs to be exposed, then I would recommend a method returning a shared reference instead of a public field.

@mgeier
Copy link
Owner

mgeier commented Nov 18, 2020

Thanks for the report!

I was very unsure when making this field public, but I thought it's fine because the Arc only ever gives read-only access.
However, I didn't realize that the whole field can still be re-assigned!

If the RingBuffer needs to be exposed,

I'm actually not completely sure yet.
For now, a reference to the ring buffer is only needed to get the capacity(), which is a method of the RingBuffer, and not of Producer/Consumer.

However, in the future there might be more methods on RingBuffer, for example mlock()/munlock() (see #3).

I wanted to avoid having to duplicate all methods from RingBuffer on both Producer and Consumer ...

Any suggestions?

then I would recommend a method returning a shared reference instead of a public field.

I've just created #27 as a possible solution.

@mgeier mgeier closed this as completed in 3abfc4f Nov 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants