-
Notifications
You must be signed in to change notification settings - Fork 295
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
Docs: Buf::chunk
should have more restrictions.
#716
Comments
I think we already de-facto have this requirement, if it's not already specified. |
Oh, I just found the Lines 166 to 179 in 8cc9407
I will open a PR to fix this issue. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This issue could also be a sub-issue of #178.
According to the docs of
Buf::chunk
:bytes/src/buf/buf_impl.rs
Lines 142 to 150 in 8cc9407
It only requires the implementer to return an empty slice when
Buf::remaining
returns 0, but a non-continuous implementation ofBuf::chunk
can return an empty slice even ifBuf::remaining
> 0.For example:
The problem is when you call some methods like
Buf::copy_to_slice
orBuf::copy_to_bytes
, it will cause an infinite loop.Let's see the default implementations of
Buf::copy_to_slice
:bytes/src/buf/buf_impl.rs
Lines 268 to 282 in 8cc9407
The
let src = self.chunk();
causes the problem, and its docs does not remind the implementer to override its behavior:bytes/src/buf/buf_impl.rs
Lines 247 to 268 in 8cc9407
The
Buf::copy_to_bytes
has the same problem.As for some methods like
Buf::get_u8
, they panic:bytes/src/buf/buf_impl.rs
Lines 300 to 307 in 8cc9407
let ret = self.chunk()[0];
cause panic, and its docs only says: "This function panics if there is no more remaining data inself
.":bytes/src/buf/buf_impl.rs
Lines 284 to 300 in 8cc9407
But it also panics when
Buf::remaining
> 0.In fact, the problem is already mentioned in #178 (comment).
These are all because the
Buf::chunk
can return an empty slice even ifBuf::remaining
> 0.Solutions
A quick fix is restricting the
Buf::chunk
to return an empty slice if and only ifBuf::remaining
returns 0, but it also changes the semantics ofBuf::chunk
, should it be considered a breaking change? I can open a PR if the change is acceptable.Any more ideas?
The text was updated successfully, but these errors were encountered: