-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
fix: recursively size variable size structs in your buffer layouts #27624
fix: recursively size variable size structs in your buffer layouts #27624
Conversation
@@ -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 |
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.