Skip to content

Commit 4ed27e0

Browse files
stewegmichalvasko
authored andcommitted
parsing BUGFIX invalid backward parent pointer
This patch fixes the issue if extensions are used within subsequent refine or typedef statements
1 parent cdffc2a commit 4ed27e0

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

src/parser_yang.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2722,9 +2722,17 @@ parse_refine(struct lysp_yang_ctx *ctx, struct lysp_refine **refines)
27222722
size_t word_len;
27232723
enum ly_stmt kw;
27242724
struct lysp_refine *rf;
2725+
LY_ARRAY_COUNT_TYPE u, v;
27252726

27262727
LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *refines, rf, LY_EMEM);
27272728

2729+
/* revalidate the backward parent pointers from extensions. The realloc within LY_ARRAY_NEW_RET is making previous entries invalid */
2730+
LY_ARRAY_FOR(*refines, u) {
2731+
LY_ARRAY_FOR((*refines)[u].exts, v) {
2732+
(*refines)[u].exts[v].parent = &(*refines)[u];
2733+
}
2734+
}
2735+
27282736
/* get value */
27292737
LY_CHECK_RET(get_argument(ctx, Y_STR_ARG, NULL, &word, &buf, &word_len));
27302738
CHECK_NONEMPTY(ctx, word_len, "refine");
@@ -2792,9 +2800,17 @@ parse_typedef(struct lysp_yang_ctx *ctx, struct lysp_node *parent, struct lysp_t
27922800
size_t word_len;
27932801
enum ly_stmt kw;
27942802
struct lysp_tpdf *tpdf;
2803+
LY_ARRAY_COUNT_TYPE u, v;
27952804

27962805
LY_ARRAY_NEW_RET(PARSER_CTX(ctx), *typedefs, tpdf, LY_EMEM);
27972806

2807+
/* revalidate the backward parent pointers from extensions. The realloc within LY_ARRAY_NEW_RET is making previous entries invalid */
2808+
LY_ARRAY_FOR(*typedefs, u) {
2809+
LY_ARRAY_FOR((*typedefs)[u].exts, v) {
2810+
(*typedefs)[u].exts[v].parent = &(*typedefs)[u];
2811+
}
2812+
}
2813+
27982814
/* get value */
27992815
LY_CHECK_RET(get_argument(ctx, Y_IDENTIF_ARG, NULL, &word, &buf, &word_len));
28002816
INSERT_WORD_GOTO(ctx, buf, tpdf->name, word, word_len, ret, cleanup);

tests/utests/basic/test_plugins.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,31 @@ const char *simple = "module libyang-plugins-simple {"
2626
" prefix s;"
2727
" typedef note { type string; }"
2828
" extension hint { argument value; }"
29+
" extension rt;"
2930
" leaf test {"
3031
" type s:note {length 255;}"
3132
" s:hint \"some hint here\";"
3233
" }"
34+
" grouping grp1 {"
35+
" list l1 {key v; leaf v {type string;} leaf k {type string;}}"
36+
" list l2 {key v; leaf v {type string;} leaf k {type string;}}"
37+
" typedef t1 {"
38+
" type string;"
39+
" s:rt;"
40+
" }"
41+
" typedef t2 {"
42+
" type string;"
43+
" s:rt;"
44+
" }"
45+
" }"
46+
" uses grp1 {"
47+
" refine l1 {"
48+
" s:rt;"
49+
" }"
50+
" refine l2 {"
51+
" s:rt;"
52+
" }"
53+
" }"
3354
"}";
3455

3556
static void
@@ -106,6 +127,29 @@ parse_clb(struct lysp_ctx *UNUSED(pctx), struct lysp_ext_instance *ext)
106127
return LY_SUCCESS;
107128
}
108129

130+
static LY_ERR
131+
parse_clb2(struct lysp_ctx *UNUSED(pctx), struct lysp_ext_instance *ext)
132+
{
133+
struct lysp_refine *refine;
134+
struct lysp_tpdf *tpdf;
135+
LY_ARRAY_COUNT_TYPE count = 0;
136+
137+
if (ext->parent_stmt == LY_STMT_REFINE) {
138+
refine = (struct lysp_refine *)ext->parent;
139+
count = LY_ARRAY_COUNT(refine->exts);
140+
} else if (ext->parent_stmt == LY_STMT_TYPEDEF) {
141+
tpdf = (struct lysp_tpdf *)ext->parent;
142+
count = LY_ARRAY_COUNT(tpdf->exts);
143+
} else {
144+
return LY_SUCCESS;
145+
}
146+
147+
if (count != 1) {
148+
return LY_EINVAL;
149+
}
150+
return LY_SUCCESS;
151+
}
152+
109153
struct lyplg_ext_record memory_recs[] = {
110154
{
111155
.module = "libyang-plugins-simple",
@@ -124,6 +168,23 @@ struct lyplg_ext_record memory_recs[] = {
124168
.plugin.pfree = NULL,
125169
.plugin.cfree = NULL
126170
},
171+
{
172+
.module = "libyang-plugins-simple",
173+
.revision = NULL,
174+
.name = "rt",
175+
176+
.plugin.id = "memory-plugin-v1",
177+
.plugin.parse = parse_clb2,
178+
.plugin.compile = NULL,
179+
.plugin.printer_info = NULL,
180+
.plugin.printer_ctree = NULL,
181+
.plugin.printer_ptree = NULL,
182+
.plugin.node_xpath = NULL,
183+
.plugin.snode = NULL,
184+
.plugin.validate = NULL,
185+
.plugin.pfree = NULL,
186+
.plugin.cfree = NULL
187+
},
127188
{0} /* terminating zeroed item */
128189
};
129190

0 commit comments

Comments
 (0)