Skip to content

[#719] Avoid quadratic strlen in JSON parser - #720

Open
hardikkaurani wants to merge 1 commit into
pgexporter:mainfrom
hardikkaurani:fix/avoid-quadratic-strlen-json-719
Open

hardikkaurani wants to merge 1 commit into
pgexporter:mainfrom
hardikkaurani:fix/avoid-quadratic-strlen-json-719

Conversation

@hardikkaurani

@hardikkaurani hardikkaurani commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Closes #719

Summary

  • In src/libpgexporter/json.c, measure the JSON string length once in pgexporter_json_parse_string() and pass len down to parse_string() and fill_value().
  • Avoid repeated O(N) strlen(str) calls across recursive parsing of objects, arrays, and values, eliminating O(N^2) quadratic scanning overhead on large or nested JSON documents.
  • Guard obj == NULL in pgexporter_json_parse_string() and initialize *obj = NULL to prevent null pointer dereferences and uninitialized output pointers on failure.
  • Add regression tests in test/testcases/test_json.c:
    • test_json_parse_string_null_and_bounds: validates error handling and NULL assignment for NULL output pointer, NULL string, empty string, and incomplete JSON.
    • test_json_parse_string_nested: validates nested objects and arrays parsing.

Verification

  • Formatted src/libpgexporter/json.c and test/testcases/test_json.c with clang-format (clang-format --dry-run --Werror passed).
  • Checked git diff --check with 0 whitespace errors.
  • Verified commit has DCO sign-off and is exactly 1 commit rebased on upstream/main.

Copilot AI balanced review requested due to automatic review settings November 30, 2026 07:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

@jesperpedersen Hi Jesper, whenever you have a chance to take a look, this PR addresses #719 to optimize JSON parsing performance and edge-case handling.

Key changes:

  • In src/libpgexporter/json.c, pgexporter_json_parse_string() now computes the string length once upfront and threads len down through parse_string() and fill_value(). This replaces repeated strlen(str) calls across recursive token and value parsing with direct bounds checks, eliminating O(N^2) scanning overhead on larger or nested payloads.
  • Added explicit validation for obj == NULL and initialized *obj = NULL upfront to ensure output pointers are cleanly handled on failure.
  • Added test cases in test/testcases/test_json.c (test_json_parse_string_null_and_bounds and test_json_parse_string_nested) verifying NULL handling, malformed/truncated JSON bounds, and nested structures.

All JSON test cases pass cleanly, code formatting matches .clang-format, and the PR is kept to a single rebased and signed-off commit. Feedback is welcome!

@Pranav-error

Copy link
Copy Markdown
Collaborator

#692 is merged now. It fixes the Debug crash in test_connection_bind_unix_socket_permissions (#691), which is what fails both build-linux Debug jobs here. Please rebase on main.

@hardikkaurani
hardikkaurani force-pushed the fix/avoid-quadratic-strlen-json-719 branch from 9f22ed5 to 030b9a8 Compare December 2, 2026 21:31
@hardikkaurani

Copy link
Copy Markdown
Contributor Author

@Pranav-error Rebased onto main (030b9a8) to pick up #692.

Pass the string length down to parse_string() and fill_value() rather
than calling strlen() repeatedly on every object, array, and value
encountered during JSON parsing. Also validate that the output pointer
is non-NULL in pgexporter_json_parse_string() and initialize *obj = NULL.

Add regression tests in test/testcases/test_json.c covering NULL and
short-string input bounds and nested object/array parsing.

Signed-off-by: Hardik Kaurani <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid quadratic strlen in JSON parser

3 participants