Skip to content

Validate nested FlexBuffers, union metadata, and path helpers in JSON text generation and util - #9439

Open
professor-moody wants to merge 1 commit into
google:masterfrom
professor-moody:fix-idl-gen-text-and-path-helper-bounds
Open

professor-moody wants to merge 1 commit into
google:masterfrom
professor-moody:fix-idl-gen-text-and-path-helper-bounds

Conversation

@professor-moody

Copy link
Copy Markdown

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 #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.

… 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.
@github-actions github-actions Bot added c++ codegen Involving generating code from schema labels Oct 27, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ codegen Involving generating code from schema

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant