Skip to content

Commit ab08f0f

Browse files
committed
schema parsers BUGFIX checking default values
Default values of leafs/leaf-lists are supposed to be checked only in data tree, not inside a grouping. Default values defined in typedefs are checked even in typedefs inside groupings with exception of leafrefs where we might not have the target leaf. Fixes #279
1 parent d178508 commit ab08f0f

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

src/parser_yang.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3592,7 +3592,8 @@ yang_check_typedef(struct lys_module *module, struct lys_node *parent, struct un
35923592

35933593
(*ptr_tpdf_size)++;
35943594
/* check default value*/
3595-
if (unres_schema_add_node(module, unres, &tpdf[i].type, UNRES_TYPE_DFLT, (struct lys_node *)(&tpdf[i].dflt)) == -1) {
3595+
if (unres_schema_add_node(module, unres, &tpdf[i].type, UNRES_TYPEDEF_DFLT,
3596+
(struct lys_node *)(&tpdf[i].dflt)) == -1) {
35963597
++i;
35973598
goto error;
35983599
}
@@ -3709,7 +3710,8 @@ yang_check_leaf(struct lys_module *module, struct lys_node_leaf *leaf, int optio
37093710
goto error;
37103711
}
37113712

3712-
if (unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DFLT, (struct lys_node *)&leaf->dflt) == -1) {
3713+
if (!(options & LYS_PARSE_OPT_INGRP) &&
3714+
(unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DFLT, (struct lys_node *)&leaf->dflt) == -1)) {
37133715
goto error;
37143716
}
37153717

@@ -3764,7 +3766,9 @@ yang_check_leaflist(struct lys_module *module, struct lys_node_leaflist *leaflis
37643766
}
37653767
/* check default value (if not defined, there still could be some restrictions
37663768
* that need to be checked against a default value from a derived type) */
3767-
if (unres_schema_add_node(module, unres, &leaflist->type, UNRES_TYPE_DFLT, (struct lys_node *)(&leaflist->dflt[i])) == -1) {
3769+
if (!(options & LYS_PARSE_OPT_INGRP) &&
3770+
(unres_schema_add_node(module, unres, &leaflist->type, UNRES_TYPE_DFLT,
3771+
(struct lys_node *)(&leaflist->dflt[i])) == -1)) {
37683772
goto error;
37693773
}
37703774
}

src/parser_yin.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1601,7 +1601,7 @@ fill_yin_typedef(struct lys_module *module, struct lys_node *parent, struct lyxm
16011601

16021602
/* check default value (if not defined, there still could be some restrictions
16031603
* that need to be checked against a default value from a derived type) */
1604-
if (unres_schema_add_node(module, unres, &tpdf->type, UNRES_TYPE_DFLT, (struct lys_node *)(&tpdf->dflt)) == -1) {
1604+
if (unres_schema_add_node(module, unres, &tpdf->type, UNRES_TYPEDEF_DFLT, (struct lys_node *)(&tpdf->dflt)) == -1) {
16051605
goto error;
16061606
}
16071607

@@ -4663,7 +4663,9 @@ read_yin_leaf(struct lys_module *module, struct lys_node *parent, struct lyxml_e
46634663

46644664
/* check default value (if not defined, there still could be some restrictions
46654665
* that need to be checked against a default value from a derived type) */
4666-
if (unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DFLT, (struct lys_node *)(&leaf->dflt)) == -1) {
4666+
if (!(options & LYS_PARSE_OPT_INGRP) &&
4667+
(unres_schema_add_node(module, unres, &leaf->type, UNRES_TYPE_DFLT,
4668+
(struct lys_node *)(&leaf->dflt)) == -1)) {
46674669
goto error;
46684670
}
46694671

@@ -4969,8 +4971,9 @@ read_yin_leaflist(struct lys_module *module, struct lys_node *parent, struct lyx
49694971
/* check default value (if not defined, there still could be some restrictions
49704972
* that need to be checked against a default value from a derived type) */
49714973
for (r = 0; r < llist->dflt_size; r++) {
4972-
if (unres_schema_add_node(module, unres, &llist->type, UNRES_TYPE_DFLT,
4973-
(struct lys_node *)(&llist->dflt[r])) == -1) {
4974+
if (!(options & LYS_PARSE_OPT_INGRP) &&
4975+
(unres_schema_add_node(module, unres, &llist->type, UNRES_TYPE_DFLT,
4976+
(struct lys_node *)(&llist->dflt[r])) == -1)) {
49744977
goto error;
49754978
}
49764979
}

src/resolve.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3029,7 +3029,7 @@ resolve_superior_type(const char *name, const char *mod_name, const struct lys_m
30293029
* @return EXIT_SUCCESS on success, EXIT_FAILURE on forward reference, -1 on error.
30303030
*/
30313031
static int
3032-
check_default(struct lys_type *type, const char **value, struct lys_module *module)
3032+
check_default(struct lys_type *type, const char **value, struct lys_module *module, int tpdf)
30333033
{
30343034
struct lys_tpdf *base_tpdf = NULL;
30353035
struct lyd_node_leaf_list node;
@@ -3041,6 +3041,9 @@ check_default(struct lys_type *type, const char **value, struct lys_module *modu
30413041
if (type->base <= LY_TYPE_DER) {
30423042
/* the type was not resolved yet, nothing to do for now */
30433043
return EXIT_FAILURE;
3044+
} else if (type->base == LY_TYPE_LEAFREF && tpdf) {
3045+
/* leafref in typedef cannot be checked */
3046+
return EXIT_SUCCESS;
30443047
}
30453048

30463049
dflt = *value;
@@ -3142,7 +3145,7 @@ check_default(struct lys_type *type, const char **value, struct lys_module *modu
31423145
ret = EXIT_FAILURE;
31433146
goto finish;
31443147
}
3145-
ret = check_default(&type->info.lref.target->type, &dflt, module);
3148+
ret = check_default(&type->info.lref.target->type, &dflt, module, 0);
31463149
if (!ret) {
31473150
/* adopt possibly changed default value to its canonical form */
31483151
if (*value) {
@@ -6539,10 +6542,13 @@ resolve_unres_schema_item(struct lys_module *mod, void *item, enum UNRES_ITEM ty
65396542
case UNRES_USES:
65406543
rc = resolve_unres_schema_uses(item, unres);
65416544
break;
6545+
case UNRES_TYPEDEF_DFLT:
6546+
parent_type++;
6547+
/* no break */
65426548
case UNRES_TYPE_DFLT:
65436549
stype = item;
65446550

6545-
rc = check_default(stype, (const char **)str_snode, mod);
6551+
rc = check_default(stype, (const char **)str_snode, mod, parent_type);
65466552
break;
65476553
case UNRES_CHOICE_DFLT:
65486554
expr = str_snode;
@@ -6764,6 +6770,7 @@ print_unres_schema_item_fail(void *item, enum UNRES_ITEM type, void *str_node)
67646770
case UNRES_USES:
67656771
LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "uses", ((struct lys_node_uses *)item)->name);
67666772
break;
6773+
case UNRES_TYPEDEF_DFLT:
67676774
case UNRES_TYPE_DFLT:
67686775
if (str_node) {
67696776
LOGVRB("Resolving %s \"%s\" failed, it will be attempted later.", "type default", (char *)str_node);

src/resolve.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ enum UNRES_ITEM {
3434
UNRES_IDENT, /* unresolved derived identities */
3535
UNRES_TYPE_IDENTREF, /* check identityref value */
3636
UNRES_FEATURE, /* feature for circular check, it must be postponed when all if-features are resolved */
37-
UNRES_TYPE_DFLT, /* validate default type value */
37+
UNRES_TYPEDEF_DFLT, /* validate default type value (from typedef) */
38+
UNRES_TYPE_DFLT, /* validate default type value (from lys_node) */
3839
UNRES_LIST_KEYS, /* list keys */
3940
UNRES_LIST_UNIQ, /* list uniques */
4041
UNRES_XPATH, /* unchecked XPath expression */

0 commit comments

Comments
 (0)