Skip to content

Invalidate iterator->first on dtor to expose use-after-free#1409

Merged
jbeder merged 1 commit intojbeder:masterfrom
gdh1995:master
Feb 28, 2026
Merged

Invalidate iterator->first on dtor to expose use-after-free#1409
jbeder merged 1 commit intojbeder:masterfrom
gdh1995:master

Conversation

@gdh1995
Copy link
Contributor

@gdh1995 gdh1995 commented Feb 26, 2026

A Node variable's iterator creates temporary variables (iterator_base::proxy) to expose an array element or mapped key+value of the node. However this usage may cause dangling pointers on proxy.first and proxy.second.

Here is a demo:

YAML::Node node;
node["key"] = "value";
const YAML::Node& invalid_node = node.begin()->first;
std::string key_str = invalid_node.Scalar();

The invalid_node refers to a destroyed proxy-typed variable, and then:

  1. if its memory region is not overwritten when calling invalid_node.Scalar(), then it may return std::string("key") as expected.
  2. if data in its memory region gets modified (e.g., by XXmalloc library or with ASAN enabled), then it may cause a crash.

This PR will fix this undefined behavior, by always filling memory region of proxy variables with 0, and then such a wrong usage will always cause a crash.

Also fix #1071 .

@SGSSGene
Copy link
Contributor

very cool! if I understand correctly, it doesn't really fix the bug, but makes it much more likely to be detected at run time, right?

@gdh1995
Copy link
Contributor Author

gdh1995 commented Feb 27, 2026 via email

@jbeder jbeder merged commit 05c050c into jbeder:master Feb 28, 2026
31 checks passed
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.

v0.7.0: invalide node error bug?

3 participants