Skip to content

feat: add thread-safe contains() function for Node.#1457

Open
sxrzh wants to merge 2 commits into
jbeder:masterfrom
sxrzh:fix1350&1092-contains
Open

feat: add thread-safe contains() function for Node.#1457
sxrzh wants to merge 2 commits into
jbeder:masterfrom
sxrzh:fix1350&1092-contains

Conversation

@sxrzh

@sxrzh sxrzh commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Add contains() function to check if node[key] exists.
Close #1350 and close #1092.

@sxrzh

sxrzh commented Jul 4, 2026

Copy link
Copy Markdown
Contributor Author

Note that for a sequence node and an integer within [0, size - 1] as the key, contains still returns true. I think this is reasonable.

@SGSSGene SGSSGene left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you @sxrzh! I think this is a much needed function!

The current way is not thread-safe.
Also could you instead of a C-Cast use a static_cast as Node::operator[] uses it?

Your PR is highly appreciated!

Comment on lines +398 to +401
inline bool Node::contains(const Key& key) const {
EnsureNodeExists();
return ((const detail::node*)m_pNode)->get(key, m_pMemory) != nullptr;
}

@SGSSGene SGSSGene Jul 7, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is not thread safe. The problem is that the function EnusreNodeExists is not thread-safe. Even though it is marked as 'const' is manipulates the member variable m_pMemory which is marked as mutable.

I think instead of calling EnsureNodeExists it should do the check it self and return false if the node doesn't exists.

@sxrzh sxrzh force-pushed the fix1350&1092-contains branch from 470d5e9 to 4249380 Compare July 7, 2026 12:40
@sxrzh sxrzh requested a review from SGSSGene July 7, 2026 12:45
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.

[Crash] Segmentation Fault/Double Free in Multithreaded Access to Undefined YAML::Node via IsDefined() and reset() Checking for key without mutation

2 participants