fix: correct operator precedence in ForAllFields reverse iteration#8991
Open
Tulgaaaaaaaa wants to merge 2 commits intogoogle:masterfrom
Open
fix: correct operator precedence in ForAllFields reverse iteration#8991Tulgaaaaaaaa wants to merge 2 commits intogoogle:masterfrom
Tulgaaaaaaaa wants to merge 2 commits intogoogle:masterfrom
Conversation
The expression `size() - i + 1` evaluates as `(size() - i) + 1` due to left-to-right associativity, producing an out-of-bounds index when reverse=true. For a vector of size N, the first iteration (i=0) accesses index N+1, which is 2 past the last valid index. Changed to `size() - (i + 1)` to match the correct implementation already present in bfbs_gen.h:192. Bug: CWE-125 (Out-of-bounds Read), CWE-783 (Operator Precedence Error)
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Verify that ForAllFields with reverse=true iterates fields in descending ID order. Tests both Stat (3 fields) and Monster (many fields with non-sequential definition order) tables.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
ForAllFields()insrc/reflection.cpp:392has an operator precedence bug when iterating fields in reverse order. The expressionsize() - i + 1evaluates as(size() - i) + 1due to C++ left-to-right associativity, instead of the intendedsize() - (i + 1).For a 3-field object at
i=0, this produces index3 + 1 = 4, which is 2 past the last valid index (0, 1, 2). This causes an out-of-bounds read on every call toForAllFieldswithreverse=true.The correct expression
size() - (i + 1)is already used in the parallel implementation atbfbs_gen.h:192.Fix
Changed
field_to_id_map.size() - i + 1tofield_to_id_map.size() - (i + 1)to matchbfbs_gen.h.size()+1(OOB)size()-1(last)size()(OOB)size()-2size()-1size()-3(first)Tests added
ForAllFieldsReverseTestintests/reflection_test.cpp