Skip to content

Internal error when when depends on a node whose own when evaluated to false #2516

@szabolcs-szekely

Description

@szabolcs-szekely

Hi,

we have a validation case where yanglint reports an internal error when one when expression depends on another data node whose own when condition has already evaluated to false. I tested this on devel, commit 2d34826, using yanglint -t config.

The same state ambiguity also affects must expressions. A must depending on a when-guarded leaf can report:

Must "..." depends on a node with a when condition, which has not been evaluated.

even though the referenced node's when was evaluated to false.

Reproducer

twm.yang:

module twm {
  yang-version 1.1;
  namespace "urn:tests:twm";
  prefix twm;

  container top {
    leaf flag {
      type boolean;
      default "false";
    }

    leaf gated {
      when "../flag = 'true'";
      type string;
    }

    leaf checker {
      must "/twm:top/twm:gated = 'expected'" {
        error-message "checker requires gated=expected";
      }
      type string;
    }

    leaf dep-when {
      when "/twm:top/twm:gated = 'expected'";
      mandatory true;
      type string;
    }
  }
}

data-internal-error.xml:

<top xmlns="urn:tests:twm">
  <gated>x</gated>
</top>

Output on devel:

libyang err : When condition "../flag = 'true'" not satisfied. (/twm:top/gated)
libyang err : Internal error (/libyang-devel-check/src/validation.c:1058).
YANGLINT[E]: Failed to parse input data file "data-internal-error.xml".
exit_code=1

data-must.xml:

<top xmlns="urn:tests:twm">
  <gated>x</gated>
  <checker>present</checker>
</top>

Output on devel:

libyang err : When condition "../flag = 'true'" not satisfied. (/twm:top/gated)
libyang err : Must "/twm:top/twm:gated = 'expected'" depends on a node with a when condition, which has not been evaluated.
YANGLINT[E]: Failed to parse input data file "data-must.xml".
exit_code=1

With a local LYD_WHEN_FALSE correction, the second command reports the real must error instead:

libyang err : When condition "../flag = 'true'" not satisfied. (/twm:top/gated)
libyang err : checker requires gated=expected (/twm:top/checker)
YANGLINT[E]: Failed to parse input data file "data-must.xml".
exit_code=1

Why this looks wrong

The gated leaf is present in the input, but its when "../flag = 'true'" evaluates to false because flag defaults to false. In multi-error validation, the invalid node remains in the tree so validation can continue.

Later, XPath evaluation for dep-when or checker reaches /twm:top/twm:gated. Internally this node only lacks LYD_WHEN_TRUE, which seems to make it indistinguishable from a node whose when has not been evaluated yet. That can produce LY_EINCOMPLETE, which becomes either the internal error above or the misleading must diagnostic.

Expected behavior: once a node's when was evaluated to false, XPath should treat it as not present. It should not be reported as “not evaluated”.

Possible fixes

We tried a narrow fix for the internal error by using LYXP_IGNORE_WHEN in lyd_validate_dummy_when():

xp_opts |= LYXP_IGNORE_WHEN;

That avoids the internal error for data-internal-error.xml, but it does not solve the general ambiguity for must, leafref, and other XPath users.

A broader fix we tried is to introduce a separate internal state, for example:

LYD_WHEN_TRUE set      -> when evaluated to true
LYD_WHEN_FALSE set     -> when evaluated to false
neither flag is set    -> when has not been evaluated yet

Then XPath can return LY_EINCOMPLETE only for genuinely unevaluated when conditions, while treating LYD_WHEN_FALSE nodes as non-existent.

Would you prefer the narrow LYXP_IGNORE_WHEN fix for lyd_validate_dummy_when(), or would you be open to a LYD_WHEN_FALSE-style internal state for the broader evaluated-false vs not-yet-evaluated ambiguity?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions