This repository was archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
fix: recursively size variable size structs in your buffer layouts #27624
Merged
steveluscher
merged 1 commit into
solana-labs:master
from
steveluscher:recursively-size-structs-buffer-layout
Sep 7, 2022
Merged
fix: recursively size variable size structs in your buffer layouts #27624
steveluscher
merged 1 commit into
solana-labs:master
from
steveluscher:recursively-size-structs-buffer-layout
Sep 7, 2022
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
steveluscher
commented
Sep 6, 2022
@@ -152,6 +152,9 @@ export function getAlloc(type: any, fields: any): number { | |||
if (Array.isArray(field)) { | |||
return field.length * getItemAlloc(item.elementLayout); | |||
} | |||
} else if ('fields' in item) { |
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 will match both Structure
and BitStructure
objects, but I did not test it with BitStructure
. I presume that BitStructure
objects can't be of variable length, so they will match the if (item.span >= 0)
check above.
I encountered this bug while creating #27627. |
Codecov Report
@@ Coverage Diff @@
## master #27624 +/- ##
=========================================
- Coverage 76.9% 76.6% -0.4%
=========================================
Files 48 52 +4
Lines 2505 2659 +154
Branches 355 366 +11
=========================================
+ Hits 1927 2037 +110
- Misses 448 488 +40
- Partials 130 134 +4 |
This was referenced Oct 4, 2022
This was referenced Oct 28, 2022
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
Imagine you have a data structure like this:
The present implementation of
getAlloc
will not dive into that inner structure and size it, so the total size of this entire layout will – incorrectly – be 1.Summary of Changes
getAlloc
encounters a structure it recurses into it to obtain its size.