From a3917d95d516e3de267d3cfa5d4d3715a90e8777 Mon Sep 17 00:00:00 2001
From: Michal Vasko <mvasko@cesnet.cz>
Date: Mon, 8 Mar 2021 14:08:05 +0100
Subject: [PATCH] yin parser BUGFIX invalid memory access

... in case there were some unresolved
extensions.
Fixes #1454
Fixes #1455
---
 src/parser_yin.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/src/parser_yin.c b/src/parser_yin.c
index 275991644..256325415 100644
--- a/src/parser_yin.c
+++ b/src/parser_yin.c
@@ -4572,7 +4572,7 @@ read_yin_anydata(struct lys_module *module, struct lys_node *parent, struct lyxm
 
     for (r = 0; r < retval->ext_size; ++r) {
         /* set flag, which represent LYEXT_OPT_VALID */
-        if (retval->ext[r]->flags & LYEXT_OPT_VALID) {
+        if (retval->ext[r] && (retval->ext[r]->flags & LYEXT_OPT_VALID)) {
             retval->flags |= LYS_VALID_EXT;
             break;
         }
@@ -4794,7 +4794,7 @@ read_yin_leaf(struct lys_module *module, struct lys_node *parent, struct lyxml_e
 
     for (r = 0; r < retval->ext_size; ++r) {
         /* set flag, which represent LYEXT_OPT_VALID */
-        if (retval->ext[r]->flags & LYEXT_OPT_VALID) {
+        if (retval->ext[r] && (retval->ext[r]->flags & LYEXT_OPT_VALID)) {
             retval->flags |= LYS_VALID_EXT;
             break;
         }
@@ -5108,7 +5108,7 @@ read_yin_leaflist(struct lys_module *module, struct lys_node *parent, struct lyx
 
     for (r = 0; r < retval->ext_size; ++r) {
         /* set flag, which represent LYEXT_OPT_VALID */
-        if (retval->ext[r]->flags & LYEXT_OPT_VALID) {
+        if (retval->ext[r] && (retval->ext[r]->flags & LYEXT_OPT_VALID)) {
             retval->flags |= LYS_VALID_EXT;
             break;
         }
@@ -5477,7 +5477,7 @@ read_yin_list(struct lys_module *module, struct lys_node *parent, struct lyxml_e
 
     for (r = 0; r < retval->ext_size; ++r) {
         /* set flag, which represent LYEXT_OPT_VALID */
-        if (retval->ext[r]->flags & LYEXT_OPT_VALID) {
+        if (retval->ext[r] && (retval->ext[r]->flags & LYEXT_OPT_VALID)) {
             retval->flags |= LYS_VALID_EXT;
             if (retval->ext[r]->flags & LYEXT_OPT_VALID_SUBTREE) {
                 retval->flags |= LYS_VALID_EXT_SUBTREE;
@@ -5701,8 +5701,9 @@ read_yin_container(struct lys_module *module, struct lys_node *parent, struct ly
     }
 
     for (r = 0; r < retval->ext_size; ++r) {
-        /* set flag, which represent LYEXT_OPT_VALID */
-        if (retval->ext[r]->flags & LYEXT_OPT_VALID) {
+        /* extension instance may not yet be resolved */
+        if (retval->ext[r] && (retval->ext[r]->flags & LYEXT_OPT_VALID)) {
+             /* set flag, which represent LYEXT_OPT_VALID */
             retval->flags |= LYS_VALID_EXT;
             if (retval->ext[r]->flags & LYEXT_OPT_VALID_SUBTREE) {
                 retval->flags |= LYS_VALID_EXT_SUBTREE;
