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

Message padding allocates buffer space unnecessarily #4829

Closed
AaronFeickert opened this issue Oct 19, 2022 · 0 comments · Fixed by #4832
Closed

Message padding allocates buffer space unnecessarily #4829

AaronFeickert opened this issue Oct 19, 2022 · 0 comments · Fixed by #4832

Comments

@AaronFeickert
Copy link
Collaborator

The recent PR 4753 changes message encryption to use an in-place BytesMut construction to avoid unnecessary allocation. When applying padding prior to encryption, it reserves extra space in the buffer to accommodate the padding before adding it. However, it reserves too much space.

The documentation for BytesMut indicates that reserve should be provided the number of additional bytes needed in the buffer, not the total number of bytes needed. As a result, the current code appears to allocate the buffer to accommodate twice the message length plus padding, which appears to negate the desired benefit of using the BytesMut approach.

Changing the linked line to message.reserve(padding_length); seems to be sufficient to avoid unnecessary reallocation, while accounting for the extra space needed for padding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant