Skip to content

fix: json.set recursive processing crash #5040

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

vyavdoshenko
Copy link
Contributor

@vyavdoshenko vyavdoshenko commented May 1, 2025

Fixes: #5038

  1. What's not working:
    Using JSON.SET with recursive descent paths ($..) on nested JSON objects could lead to hangs or crashes. This occurred because the mutation logic could process the same node multiple times if it was discovered via different paths during the traversal.

  2. How this PR fixes the issue:
    This PR adjusts the iteration logic. When processing elements of an object or array targeted by a path segment, the code now correctly handles iterator advancement, especially after an element might have been modified or erased by the mutation callback. This ensures that each element within the target object/array is considered for mutation exactly once during that step, preventing infinite loops or incorrect processing that previously led to the crash/hang with $.. paths.

@romange
Copy link
Collaborator

romange commented May 1, 2025

Please add explanation to the PR description. In general, for any non-trivial PRs, please state at least:

  1. what's not working
  2. how this PR fixes the problem.

@@ -3087,4 +3087,29 @@ TEST_F(JsonFamilyTest, DepthLimitExceeded) {
ASSERT_THAT(resp, ErrArg("ERR failed to parse JSON"));
}

TEST_F(JsonFamilyTest, RecursiveDescentMutation) {
Copy link
Collaborator

@romange romange May 2, 2025

Choose a reason for hiding this comment

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

Once you identified the problem you should find what is the minimum possible unit test that can help reproducing the problem. It's fine to add tests here but it's necessary to add tests to src/core/json/jsonpath_test.cc where you can reproduce it precisely and be able to iterate quickly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@vyavdoshenko vyavdoshenko marked this pull request as draft May 2, 2025 15:31
@vyavdoshenko vyavdoshenko changed the title fix: json.set recursive processing crash [wip] fix: json.set recursive processing crash May 2, 2025
@vyavdoshenko vyavdoshenko force-pushed the bobik/json_set_recursive_crash branch 5 times, most recently from 4f9afa5 to f1ade14 Compare May 2, 2025 16:47
return depth_state_.first;
}

// Added getter for segment_step_
Copy link
Collaborator

Choose a reason for hiding this comment

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

no need to write "added" in code comments :)
also, this comment is redundant we do not need describing c++ one liners.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

@@ -8,6 +8,8 @@

#include "core/json/detail/jsoncons_dfs.h"

#include <set>
Copy link
Collaborator

Choose a reason for hiding this comment

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

we use #include <absl/container/flat_hash_set.h> as drop in replacement

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

Copy link
Collaborator

@romange romange left a comment

Choose a reason for hiding this comment

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

good work simplifying the fix!

@vyavdoshenko vyavdoshenko force-pushed the bobik/json_set_recursive_crash branch from f1ade14 to 71aa4c5 Compare May 3, 2025 10:10
@vyavdoshenko vyavdoshenko changed the title [wip] fix: json.set recursive processing crash fix: json.set recursive processing crash May 3, 2025
@vyavdoshenko vyavdoshenko marked this pull request as ready for review May 3, 2025 10:21
@vyavdoshenko vyavdoshenko requested a review from romange May 3, 2025 10:22
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.

json.set causes to unable to allocate aligned OS memory directly & dragonfly hangs
2 participants