Skip to content

~DAGNode() erases from 'parents' while iterating over it (container-overflow under ASan) #86

Closed

Description

@joergbrech

Summary

DAGNode::~DAGNode() iterates parents with a range-based for while remove_parent(*parent) erases elements from that very vector. That is iterator invalidation (undefined behaviour). With libstdc++ container annotations ASan reports it on the most trivial graph.

Reproducer

// does destroying a feature graph read past size() of a std::vector? (run with -fsanitize=address -D_GLIBCXX_SANITIZE_VECTOR)
#include <grunk/dynamic.hpp>
#include <cstdio>
int main() {
    grunk::state g;
    g.register_function("add", [](double a, double b) { return a + b; });
    {
        auto x = g.feature(1.0).with_id("x");
        auto y = g.feature(2.0).with_id("y");
        auto z = g.action("add", x, y);
        auto w = g.action("add", z, x);
        std::printf("%g\n", w.value().as<double>());
    }
    std::printf("done\n");
}
c++ -std=c++17 -O1 -g -fsanitize=address -D_GLIBCXX_SANITIZE_VECTOR -D_GLIBCXX_SANITIZE_STD_ALLOCATOR -DGRUNK_WITH_DYNAMIC ... p2.cpp -llua

Output (abridged)

ERROR: AddressSanitizer: container-overflow on address ...
READ of size 8 at ... thread T0
    #0 std::__shared_ptr<parametric::DAGNode, ...>::operator bool() const
    #1 parametric::DAGNode::~DAGNode()                       parametric/dag.hpp:322
    #2 parametric::ClonableDAGNode<grunk::ActionDynamic>::~ClonableDAGNode()  dag.hpp:371
    #3 parametric::ComputeNode<grunk::ActionDynamic, ...>::~ComputeNode()
    #4 grunk::ActionDynamic::~ActionDynamic()

Cause

virtual ~DAGNode() {
    for (const auto& parent : parents) {          // iterating `parents` ...
        if (parent) remove_parent(*parent);       // ... while remove_parent() does parents.erase(...) on the same vector
    }
}

Suggestion

Detach first: auto old = std::move(parents); parents.clear(); for (auto& p : old) if (p) remove_child_entry(*p); (or loop while (!parents.empty())). Note that it also never succeeds in removing the child entry from the parent (see the other issue about dead childs entries).

Environment: grunk main (8af34a7, 0.5.0), parametric 0.3.5, sol2 3.5.0, Lua 5.4.8, GCC 15.3 (conda-forge), Linux x86-64; WebAssembly: Emscripten 3.1.58.
(Found while building a prototype GUI/viewer on top of grunk.)

Activity

  1. added
    bug
    on Oct 1, 2026
  2. self-assigned this
    on Oct 2, 2026
  3. added a commit that references this issue on Oct 2, 2026
    ac46fb8
  4. linked a pull request that will close this issuefix #86 #87on Oct 2, 2026
  5. added a commit that references this issue on Oct 2, 2026
    b40ccd1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions