-
Notifications
You must be signed in to change notification settings - Fork 423
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
return RecvError::Truncated when calling recv_slice with a small buffer #859
Conversation
c6460ed
to
e87a1f0
Compare
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## main #859 +/- ##
==========================================
- Coverage 79.60% 79.55% -0.06%
==========================================
Files 78 78
Lines 27896 27917 +21
==========================================
+ Hits 22206 22208 +2
- Misses 5690 5709 +19 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I like the design where there are two different ways of receiving metadata from the socket. This certainly takes "convenience" out of "convenience method".
I am not entirely sure what to do about this, but I would personally be happy to have Error::Truncated
mean "there was a packet in the socket that was too big, it is lost, and you can move on to the next packet if you are this sure you only care about smaller ones". Anyone else should be using recv
, probably.
I was not sure about that part either. I'll remove the endpoint from the |
Do you mean that we also don't really need to copy part of the packet in the provided buffer and we should drop the full packet? |
Yep. These are convenience methods for the case where you're pretty certain you do not care at all about the bigger packets. If you sometimes want to process only the first part, or you want to execute some complex error handling code, they're not for you. A note to that end in the docs may be useful. |
When calling `recv_slice`/`peek_slice` in the udp, icmp and raw sockets, data is lost when the provided buffer is smaller than the payload to be copied (not the case for `peek_slice` because it is not dequeued). With this commit, an `RecvError::Truncated` error is returned. In case of UDP, the endpoint is also returned in the error. In case of ICMP, the IP address is also returned in the error. I implemented it the way Whitequark proposes it. Data is still lost, but at least the caller knows that the data was truncated to the size of the provided buffer. As Whitequark says, it is preferred to call `recv` instead of `recv_slice` as there would be no truncation.
e87a1f0
to
1386417
Compare
I made the changes and added a note in the documentation of the functions. Note that now the behaviour of these functions is different: no data is actually copied when the buffer is too small, whereas previously, data was still copied into the smaller buffer. |
This is a breaking change regardless of how much data is copied though, isn't it? |
I think only because of the addition of |
Do you think we can close #330 now? |
I mean that returning a different error, with or without payload, in case an overlong packet is detected, is a breaking change. Whether or not the buffer is filled out with data, at that point, doesn't make it any more breaking. |
Yeah. |
When calling
recv_slice
/peek_slice
in the udp, icmp and raw sockets, data is lost when the provided buffer is smaller than the payload to be copied (not the case forpeek_slice
because it is not dequeued).With this commit, an
RecvError::Truncated
error is returned. In case of UDP, the endpoint is also returned in the error. In case of ICMP, the IP address is also returned in the error.I implemented it the way @whitequark proposes it in #330. Data is still lost, but at least the caller knows that the data was truncated to the size of the provided buffer. As @whitequark says, it is preferred to call
recv
instead ofrecv_slice
as there would be no truncation. If this is expected behaviour forrecv_slice
, we could close #330.