-
Notifications
You must be signed in to change notification settings - Fork 6
Replace byte buffer with sector buffer #388
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
Conversation
c1e617d to
affb6a8
Compare
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.
Pull request overview
This PR replaces the use of bytes.NewBuffer with a custom sectorBuffer implementation in the handleRPCWriteSector function to prevent potential data corruption. The change addresses a fragility issue where bytes.NewBuffer explicitly states not to reuse its input, which the code was doing by passing the same sector array to StoreSector after buffering.
Changes:
- Introduced a custom
sectorBuffertype that implementsio.Writerfor safer sector data streaming - Replaced
bytes.NewBuffer(sector[:0])withnewSectorBuffer(§or)inhandleRPCWriteSector - Added changeset documentation for the patch-level change
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| rhp/v4/server.go | Replaced bytes.NewBuffer with custom sectorBuffer implementation and added the sectorBuffer type definition with its Write method and constructor |
| .changeset/use_custom_sectorbuffer_rather_than_bytesbuffer_in_handlerpcwritesector.md | Added changeset documentation for the patch-level change |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
This is a reasonable defensive change, but it doesn't address the other half of the ownership problem which is significantly more likely to bite us in the ass. We have tests that read a full sector without reallocating. A change to stdlib that broke this code would fail our tests first. As-is this is slightly less fragile while still being very fragile.
Can you take this further and enforce ownership of the backing array too? Otherwise, this seems unnecessary.
affb6a8 to
ff3150b
Compare
I pushed a little change. Not sure if that is what you had in mind regarding "ownership" but I just thought about all the ways the Rust borrow checker would hate us for that code and this is what I came up with. |
|
That is pretty much exactly what I had in mind lol |
While our current implementation is theoretically safe it is quite fragile.
bytes.NewBufferexplicitly states to not reuse its input afterwards which is something we do. So if the buffer's implementation changes or some edge case triggers a realloc of the underlying slice,sectormight contain corrupted data without us knowing it.sectorBufferis a drop-in replacement that makes this impossible and easier to reason about.