Repository navigation
Validate nested FlexBuffers, union metadata, and path helpers in JSON text generation and util - #9439
Open
professor-moody wants to merge 1 commit into
Conversation
… text generation and util ## Summary This change hardens JSON text generation in `src/idl_gen_text.cpp` and path/case utilities in `src/util.cpp`: - **`JsonPrinter::GenFieldOffset` (`src/idl_gen_text.cpp`)**: Call `flexbuffers::VerifyBuffer(vec->data(), vec->size())` before `flexbuffers::GetRoot(vec->data(), vec->size())` when formatting `flexbuffer` byte-vector fields with `opts.json_nested_flexbuffers`, returning `"invalid nested flexbuffer"` on malformed nested buffers. - **`JsonPrinter::PrintOffset` (`src/idl_gen_text.cpp`)**: Check `if (index >= type_vec->size()) return "union type vector too short";` before calling `type_vec->Get(index)` on union vectors so out-of-bounds union type lookups fail cleanly. - **`GenerateTextFromTable` (`src/idl_gen_text.cpp`)**: Return `GenTextFromTable(parser, table, table_name, _text) == nullptr` so the boolean wrapper returns `true` on success and `false` on error. - **`RelativeToRootPath` and case-conversion helpers (`src/util.cpp`)**: Guard against empty `absolute_project` or `absolute_filepath` before calling `absolute_project.back()` or `absolute_filepath.substr(common_prefix_len + 1)` in `RelativeToRootPath`, and cast `char` inputs to `unsigned char` before calling C character classification functions (`islower`, `isdigit`, `isalpha`) in `ToSnakeCase`, `CamelToSnake`, `ToDasher`, and `SnakeToSnake2`. ## Verification - Added positive control and negative regression tests in `tests/json_test.cpp` (`JsonUnionStructTest`) covering nested FlexBuffers, truncated union type vectors, `GenerateTextFromTable`, portable `RelativeToRootPath` expectations, and `ConvertCase` high-bit inputs. - The recorded full, unfiltered `flattests` run reports `ALL TESTS PASSED` and exit code 0 with the patch. The recorded baseline run exits 1. The logs do not establish a registered-test count or a Windows test result. ## Scope and related work These checks provide targeted validation within text generation. Validation of the enclosing FlatBuffer remains a caller responsibility. [PR google#9308](google#9308) handles absent scalar union type metadata in the same printer and test files. That change is outside this patch's retained scope. [PR google#9207](google#9207) changes make-dependency escaping in the text generator and utility file; [PR google#9270](google#9270) changes locale initialization in the utility file. These open changes share files, so integration still requires review even though the retained changes have different purposes. The packet includes a hash-bound hunk-level prior-art scope supersession receipt that links the current patch to the earlier reviewed patch and records removal of the overlapping scalar union-discriminator hunks.
This branch has not been deployed
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.
Summary
This change hardens JSON text generation in
src/idl_gen_text.cppand path/case utilities insrc/util.cpp:JsonPrinter::GenFieldOffset(src/idl_gen_text.cpp): Callflexbuffers::VerifyBuffer(vec->data(), vec->size())beforeflexbuffers::GetRoot(vec->data(), vec->size())when formattingflexbufferbyte-vector fields withopts.json_nested_flexbuffers, returning"invalid nested flexbuffer"on malformed nested buffers.JsonPrinter::PrintOffset(src/idl_gen_text.cpp): Checkif (index >= type_vec->size()) return "union type vector too short";before callingtype_vec->Get(index)on union vectors so out-of-bounds union type lookups fail cleanly.GenerateTextFromTable(src/idl_gen_text.cpp): ReturnGenTextFromTable(parser, table, table_name, _text) == nullptrso the boolean wrapper returnstrueon success andfalseon error.RelativeToRootPathand case-conversion helpers (src/util.cpp): Guard against emptyabsolute_projectorabsolute_filepathbefore callingabsolute_project.back()orabsolute_filepath.substr(common_prefix_len + 1)inRelativeToRootPath, and castcharinputs tounsigned charbefore calling C character classification functions (islower,isdigit,isalpha) inToSnakeCase,CamelToSnake,ToDasher, andSnakeToSnake2.Verification
tests/json_test.cpp(JsonUnionStructTest) covering nested FlexBuffers, truncated union type vectors,GenerateTextFromTable, portableRelativeToRootPathexpectations, andConvertCasehigh-bit inputs.flattestsrun reportsALL TESTS PASSEDand exit code 0 with the patch. The recorded baseline run exits 1. The logs do not establish a registered-test count or a Windows test result.Scope and related work
These checks provide targeted validation within text generation. Validation of the enclosing FlatBuffer remains a caller responsibility.
PR #9308 handles absent scalar union type metadata in the same printer and test files. That change is outside this patch's retained scope. PR #9207 changes make-dependency escaping in the text generator and utility file; PR #9270 changes locale initialization in the utility file. These open changes share files, so integration still requires review even though the retained changes have different purposes.
The packet includes a hash-bound hunk-level prior-art scope supersession receipt that links the current patch to the earlier reviewed patch and records removal of the overlapping scalar union-discriminator hunks.