Skip to content
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

rapidyaml assigning out-of-bound id values to new nodes #396

Open
allanleal opened this issue Nov 27, 2023 · 0 comments
Open

rapidyaml assigning out-of-bound id values to new nodes #396

allanleal opened this issue Nov 27, 2023 · 0 comments

Comments

@allanleal
Copy link

allanleal commented Nov 27, 2023

Hello, thanks for developing this library. I would like to report a potential issue, demonstrated with the following example:

#include <ryml.hpp>

int main(int argc, char const *argv[])
{
    ryml::Tree tree;

    auto rootnode = tree.rootref();

    rootnode |= ryml::SEQ;
    rootnode[0] << 1;
    rootnode[1] << 2;
    rootnode[2] << 3;
    rootnode[3] << 4;

    int A, B, C, D;
    rootnode[0] >> A;
    rootnode[1] >> B;
    rootnode[2] >> C;
    rootnode[3] >> D;

    assert(A == 1);
    assert(B == 2);
    assert(C == 3);
    assert(D == 4);

    rootnode.clear();
    rootnode |= ryml::SEQ;

    rootnode.append_child() << 10;
    rootnode.append_child() << 20;

    rootnode[0] >> A;
    rootnode[1] >> B;

    assert(A == 10);
    assert(B == 20);

    rootnode.clear();
    rootnode |= ryml::SEQ;

    size_t iA = rootnode.append_child().id(); // Wrong id value here, iA = 3 while tree.m_size is 2
    size_t iB = rootnode.append_child().id();

    tree.ref(iA) << 11;  // An out-of-bound access error happens here
    tree.ref(iB) << 22;

    rootnode[0] >> A;
    rootnode[1] >> B;

    assert(A == 11);
    assert(B == 22);

    return 0;
}

As pointed out above, the line of code:

size_t iA = rootnode.append_child().id();

produces a node with an id iA that is out of bounds with respect to the tree size. When using Tree::ref method with such id, an error happens.

Note the previous line of code:

rootnode.append_child() << 10;

works because rapidyaml uses node retrieval methods that do not perform bounds check.

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

No branches or pull requests

1 participant