Skip to content

Fix EncodeUint256 to work with bigger buffers#13580

Merged
yperbasis merged 1 commit intomainfrom
buffer_fix
Jan 27, 2025
Merged

Fix EncodeUint256 to work with bigger buffers#13580
yperbasis merged 1 commit intomainfrom
buffer_fix

Conversation

@yperbasis
Copy link
Copy Markdown
Member

Follow-up to PR #12869 & #13574

@yperbasis yperbasis added this to the 2.61.1-fixes milestone Jan 27, 2025
@yperbasis yperbasis enabled auto-merge (squash) January 27, 2025 15:05
@somnergy
Copy link
Copy Markdown
Member

EncodeUint256 only uses 32 bytes exactly (plus one for the size). It only ever gets passed that throughout the code. It's better to leave it at that or add additional checks to ensure this to make it more robust

@yperbasis
Copy link
Copy Markdown
Member Author

EncodeUint256 only uses 32 bytes exactly (plus one for the size). It only ever gets passed that throughout the code. It's better to leave it at that or add additional checks to ensure this to make it more robust

Well, this PR is making EncodeUint256 more robust (now it works with any buffer whose length >= 32).

@somnergy
Copy link
Copy Markdown
Member

Yes but if the calling code wants to pass a bigger byte slice, it already knows what it's doing, and should pass 32 instead. Otherwise, it might as well pass 31 or less, in which case it will break.

EncodeUint256 only uses 32 bytes exactly (plus one for the size). It only ever gets passed that throughout the code. It's better to leave it at that or add additional checks to ensure this to make it more robust

Well, this PR is making EncodeUint256 more robust (now it works with any buffer whose length >= 32).

@yperbasis
Copy link
Copy Markdown
Member Author

It's a matter of taste, of course, but here I'm following the robustness principle "be conservative in what you do, be liberal in what you accept from others". We could make EncodeUint256 return an error for buffers longer than 32 bytes, but I don't see a point because the function can work with such buffers just fine.

The case when a buffer shorter than 32 bytes is passed is probably easier to detect because it'll panic.

@yperbasis yperbasis merged commit 995e3ca into main Jan 27, 2025
@yperbasis yperbasis deleted the buffer_fix branch January 27, 2025 15:46
@yperbasis yperbasis removed this from the 2.61.1-fixes milestone Jan 27, 2025
somnergy added a commit that referenced this pull request Jan 28, 2025
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 this pull request may close these issues.

3 participants