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

validateAll returns error when parseData returns std::nullopt #11

Open
bedrich-schindler opened this issue Jul 26, 2023 · 3 comments
Open

Comments

@bedrich-schindler
Copy link
Contributor

We have following code (simplified for the purpose of example):

// ...
libyang::Context *context;
// ...

// ...
YangModule::YangModule() {
    this->context = new libyang::Context(std::nullopt, libyang::ContextOptions::SetPrivParsed);
}
// ...

/*
 * Validates the data tree against the schema.
 *
 * @param dataTreeJson The data tree to validate
 * @return `true` if the data tree is valid, `false` otherwise
 */
bool YangModule::validateData(const std::string &dataTreeJson) const {
    // Errors are automatically truncated every function call
    this->clearErrors();

    try {
        std::optional<libyang::DataNode> dataNode = this->context->parseDataMem(
            dataTreeJson,
            libyang::DataFormat::JSON,
            libyang::ParseOptions::ParseOnly,
            libyang::ValidationOptions::Present
        );

        validateAll(dataNode, libyang::ValidationOptions::Present);
    } catch (...) {
        return false;
    }

    // Errors are automatically truncated every function call
    return this->getErrors().empty();
}

If parseDataMem (respectively parseData in new version) returns std::nullopt instead of DataNode, and you pass it to the validateAll, libyang returns following error libyang[0]: Invalid argument tree (lyd_validate_all()).

I can provide example schema later, but the problem might be that std::optional<DataNode> Context::parseData returns std::nullopt when !tree. Colleague that use libyang (not libyang-cpp) says it works fine in C:

lyd_parse_data_mem(ctx, "{}", LYD_JSON, LYD_PARSE_ONLY, 0, &dnode);
// lyd_parse_data_mem(ctx, "", LYD_XML, LYD_PARSE_ONLY, 0, &dnode);
lyd_validate_all(&dnode, ctx, LYD_VALIDATE_PRESENT, NULL);

If he passes NULL instead of empty object, it the returns the same error as I mentioned before.

We think that C++ implementation is not correct.

@jktjkt
Copy link
Contributor

jktjkt commented Jul 26, 2023

Interesting. The main reason why the validateAll wrapper takes an optional<DataNode> & and not just a const DataNode & is that it might modify the node in-place, possibly even setting it to nullopt. The other reason is that a nullopt might be a valid "tree" on some models. The proper fix will probably be to extend validateAll to accept both an optional node, and a libyang context. Right now we're setting ctx to NULL and are relying on the C library to infer the context from the data tree, which it obviously cannot do if the tree is NULL as well.

However, the real point is that explicitly requesting validation like this should not be needed in the first place. If I read the C docs correctly, lyd_parse_XX performs validation already, so validateAll is intended to be called only when you're building your data trees programmatically.

@syyyr
Copy link
Contributor

syyyr commented Jul 26, 2023

The solution in this specific case would probably be just removing the libyang::ParseOptions::ParseOnly flag.

Adding an optional Context argument is probably pretty easy yeah. Something like an std::optional<Context> argument to validateAll

@bedrich-schindler
Copy link
Contributor Author

Thank you for quick response. We somehow ignored the fact that validateAll is not necessary. If I change our code from:

    try {
        std::optional<libyang::DataNode> dataNode = this->context->parseDataMem(
            dataTreeJson,
            libyang::DataFormat::JSON,
            libyang::ParseOptions::ParseOnly,
            libyang::ValidationOptions::Present
        );

        if (!dataNode.has_value()) {
            return false;
        }

        validateAll(dataNode, libyang::ValidationOptions::Present);
    } catch (...) {
        return false;
    }

to this:

    try {
        std::optional<libyang::DataNode> dataNode = this->context->parseDataMem(
            dataTreeJson,
            libyang::DataFormat::JSON,
            libyang::ParseOptions::Strict,
            libyang::ValidationOptions::Present
        );
    } catch (...) {
        return false;
    }

it almost works. I just have to change libyang::ParseOptions::ParseOnly to libyang::ParseOptions::Strict otherwise it does not validate anything. But with Strict options, it almost validates "empty data" as expected. I just need to figure out why the catch does not catch error as in previous case, whole code crashes instead and error is outputted in the standard output instead - before, I called parseData, validateAll and I found those error messages with getErrors function. But it might be problem in our WebAssemly code.

However, should't the mentioned problem be fixed? Empty data are still valid data, so if parseData returns std::nullopt, validateAll should be able it (if C code is able to do it), should't it?

jktjkt pushed a commit that referenced this issue Sep 6, 2024
There is ODR violation in libyang tests reported by ASan which breaks
our CI builds. This commit temporarily (until this gets solved in
upstream) pins the libyang version to 3.0.18 which builds just fine.

The log from ASan:

  52/60 Test #31: utest_plugins .....................***Failed    1.22 sec
  [==========] tests: Running 4 test(s).
  [ RUN      ] test_add_invalid
  [       OK ] test_add_invalid
  [ RUN      ] test_add_simple
  =================================================================
  ==2826==ERROR: AddressSanitizer: odr-violation (0x7f6d170dcb40):
    [1] size=24 'ly_version_so' /home/ci/src/cesnet-gerrit-public/github/CESNET/libyang/src/ly_common.c:43
    [2] size=24 'ly_version_so' /home/ci/src/cesnet-gerrit-public/github/CESNET/libyang/src/ly_common.c:43
  These globals were registered at these points:
    [1]:
      #0 0x437ffa in __asan_register_globals (/home/ci/build/github/CESNET/libyang/tests/utest_plugins+0x437ffa) (BuildId: e79d14f37e59b6533b66d19363b25fea9e324edb)
      #1 0x7f6d16691b9e in asan.module_ctor ly_common.c
      #2 0x7f6d1aad7236 in call_init /usr/src/debug/glibc-2.37-18.fc38.x86_64/elf/dl-init.c:74:3
      #3 0x7f6d1aad7236 in call_init /usr/src/debug/glibc-2.37-18.fc38.x86_64/elf/dl-init.c:26:1
      #4 0x7f6d1aad732c in _dl_init /usr/src/debug/glibc-2.37-18.fc38.x86_64/elf/dl-init.c:121:5
      #5 0x7f6d1aad35c1 in _dl_catch_exception /usr/src/debug/glibc-2.37-18.fc38.x86_64/elf/dl-catch.c:211:7
      #6 0x7f6d1aaddeeb in dl_open_worker /usr/src/debug/glibc-2.37-18.fc38.x86_64/elf/dl-open.c:827:5
      #7 0x7f6d1aad3522 in _dl_catch_exception /usr/src/debug/glibc-2.37-18.fc38.x86_64/elf/dl-catch.c:237:8
      #8 0x7f6d1aade2e3 in _dl_open /usr/src/debug/glibc-2.37-18.fc38.x86_64/elf/dl-open.c:903:17
      #9 0x7f6d1a7b5713 in dlopen_doit /usr/src/debug/glibc-2.37-18.fc38.x86_64/dlfcn/dlopen.c:56:15
      #10 0x7f6d1aad3522 in _dl_catch_exception /usr/src/debug/glibc-2.37-18.fc38.x86_64/elf/dl-catch.c:237:8
      #11 0x7f6d1aad3678 in _dl_catch_error /usr/src/debug/glibc-2.37-18.fc38.x86_64/elf/dl-catch.c:256:19
      #12 0x7f6d1a7b51f2 in _dlerror_run /usr/src/debug/glibc-2.37-18.fc38.x86_64/dlfcn/dlerror.c:138:17
      #13 0x7f6d1a7b57ce  /usr/src/debug/glibc-2.37-18.fc38.x86_64/dlfcn/dlopen.c:71:10
      #14 0x7f6d1a7b57ce in dlopen@@GLIBC_2.34 /usr/src/debug/glibc-2.37-18.fc38.x86_64/dlfcn/dlopen.c:81:12
      #15 0x485f82 in dlopen (/home/ci/build/github/CESNET/libyang/tests/utest_plugins+0x485f82) (BuildId: e79d14f37e59b6533b66d19363b25fea9e324edb)
      #16 0xcb5ee3 in plugins_load_module /home/ci/src/cesnet-gerrit-public/github/CESNET/libyang/src/plugins.c:378:17
      #17 0xcb5e67 in lyplg_add /home/ci/src/cesnet-gerrit-public/github/CESNET/libyang/src/plugins.c:589:11
      #18 0xea2a60 in test_add_simple /home/ci/src/cesnet-gerrit-public/github/CESNET/libyang/tests/utests/basic/test_plugins.c:50:5
      #19 0x7f6d1aac218f  (/lib64/libcmocka.so.0+0x618f) (BuildId: 785844a0941c0bde763740a981d056f60aa9c7b7)
      #20 0x7f6d1aac2904 in _cmocka_run_group_tests (/lib64/libcmocka.so.0+0x6904) (BuildId: 785844a0941c0bde763740a981d056f60aa9c7b7)
      #21 0xea21a2 in main /home/ci/src/cesnet-gerrit-public/github/CESNET/libyang/tests/utests/basic/test_plugins.c:152:12

Change-Id: Id8a4771296d2e85de2ec29fbd17046d280d2eda2
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

3 participants