Skip to content

Commit

Permalink
tree schema BUGFIX correct predicate order in lysc_path (CESNET#2244)
Browse files Browse the repository at this point in the history
* tree schema BUGFIX correct predicate order in lysc_path

* correct uncrustify errors

* test non-empty path after predicates

---------

Co-authored-by: Alexandre Snarskii <[email protected]>
  • Loading branch information
snar and Alexandre Snarskii authored May 24, 2024
1 parent eea2384 commit 669345c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
30 changes: 20 additions & 10 deletions src/tree_schema.c
Original file line number Diff line number Diff line change
Expand Up @@ -715,22 +715,32 @@ lysc_path_until(const struct lysc_node *node, const struct lysc_node *parent, LY
}

if ((pathtype == LYSC_PATH_DATA_PATTERN) && (iter->nodetype == LYS_LIST)) {
char *predicates = NULL;

key = NULL;
while ((key = lys_getnext(key, iter, NULL, 0)) && lysc_is_key(key)) {
s = buffer ? strdup(buffer) : path;
s = predicates;

/* print key predicate */
if (buffer) {
len = snprintf(buffer, buflen, "%s[%s='%%s']", s ? s : "", key->name);
} else {
len = asprintf(&path, "%s[%s='%%s']", s ? s : "", key->name);
asprintf(&predicates, "%s[%s='%%s']", s ? s : "", key->name);
if (s) {
free(s);
}
free(s);
}
s = buffer ? strdup(buffer) : path;
if (buffer) {
len = snprintf(buffer, buflen, "%s%s", predicates ? predicates : "", s ? s : "");
} else {
len = asprintf(&path, "%s%s", predicates ? predicates : "", s ? s : "");
}
if (predicates) {
free(predicates);
}
free(s);

if (buffer && (buflen <= (size_t)len)) {
/* not enough space in buffer */
break;
}
if (buffer && (buflen <= (size_t)len)) {
/* not enough space in buffer */
break;
}
}

Expand Down
5 changes: 5 additions & 0 deletions tests/utests/schema/test_schema.c
Original file line number Diff line number Diff line change
Expand Up @@ -1873,13 +1873,18 @@ test_lysc_path(void **state)
" leaf k {type string;}"
" leaf l {type string;}"
" leaf m {type string;}"
" leaf n {type string;}"
" }"
"}}", LYS_IN_YANG, NULL));

node = lys_find_path(UTEST_LYCTX, NULL, "/b:a/l", 0);
path = lysc_path(node, LYSC_PATH_DATA_PATTERN, NULL, 0);
assert_string_equal(path, "/b:a/l[k='%s'][l='%s'][m='%s']");
free(path);
node = lys_find_path(UTEST_LYCTX, NULL, "/b:a/l/n", 0);
path = lysc_path(node, LYSC_PATH_DATA_PATTERN, NULL, 0);
assert_string_equal(path, "/b:a/l[k='%s'][l='%s'][m='%s']/n");
free(path);
}

int
Expand Down

0 comments on commit 669345c

Please sign in to comment.