tree CHANGE test for NULL in LY_ARRAY_COUNT() macro

Make it more safe in cost of losing possibility to use macro as lvalue.
diff --git a/src/parser_lyb.c b/src/parser_lyb.c
index 8a54c6e..c7a64b3 100644
--- a/src/parser_lyb.c
+++ b/src/parser_lyb.c
@@ -246,12 +246,7 @@
     uint8_t meta_buf[LYB_META_BYTES];
     LY_ARRAY_COUNT_TYPE u;
 
-    if (!lybctx->subtrees) {
-        assert(lybctx->subtree_size == 0);
-        u = 0;
-    } else {
-        u = LY_ARRAY_COUNT(lybctx->subtrees);
-    }
+    u = LY_ARRAY_COUNT(lybctx->subtrees);
     if (u == lybctx->subtree_size) {
         LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->subtrees, u + LYB_SUBTREE_STEP, LY_EMEM);
         lybctx->subtree_size = u + LYB_SUBTREE_STEP;
diff --git a/src/printer_lyb.c b/src/printer_lyb.c
index 01d35f4..3b62d27 100644
--- a/src/printer_lyb.c
+++ b/src/printer_lyb.c
@@ -325,12 +325,7 @@
 {
     LY_ARRAY_COUNT_TYPE u;
 
-    if (!lybctx->subtrees) {
-        assert(lybctx->subtree_size == 0);
-        u = 0;
-    } else {
-        u = LY_ARRAY_COUNT(lybctx->subtrees);
-    }
+    u = LY_ARRAY_COUNT(lybctx->subtrees);
     if (u == lybctx->subtree_size) {
         LY_ARRAY_CREATE_RET(lybctx->ctx, lybctx->subtrees, u + LYB_SUBTREE_STEP, LY_EMEM);
         lybctx->subtree_size = u + LYB_SUBTREE_STEP;
diff --git a/src/schema_compile.c b/src/schema_compile.c
index d2efc50..ecc1a86 100644
--- a/src/schema_compile.c
+++ b/src/schema_compile.c
@@ -439,9 +439,6 @@
 
     for (v = 0; v < recursion.count; ++v) {
         drv = recursion.objs[v];
-        if (!drv->derived) {
-            continue;
-        }
         for (u = 0; u < LY_ARRAY_COUNT(drv->derived); ++u) {
             if (ident == drv->derived[u]) {
                 LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_REFERENCE,
@@ -560,7 +557,7 @@
     lysc_update_path(ctx, NULL, "{identity}");
 
 restart:
-    for (u = 0, v = 0; *idents && (u < LY_ARRAY_COUNT(*idents)); ++u) {
+    for (u = 0, v = 0; u < LY_ARRAY_COUNT(*idents); ++u) {
         /* find matching parsed identity, the disabled ones are missing in the compiled array */
         while (v < LY_ARRAY_COUNT(idents_p)) {
             if (idents_p[v].name == (*idents)[u].name) {
@@ -1243,20 +1240,11 @@
 
     assert(dflt || dflts);
 
-    if (llist->dflts) {
-        /* there were already some defaults and we are adding new by deviations */
-        assert(dflts);
-        orig_count = LY_ARRAY_COUNT(llist->dflts);
-    } else {
-        orig_count = 0;
-    }
+    /* in case there were already some defaults and we are adding new by deviations */
+    orig_count = LY_ARRAY_COUNT(llist->dflts);
 
     /* allocate new items */
-    if (dflts) {
-        LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + LY_ARRAY_COUNT(dflts), LY_EMEM);
-    } else {
-        LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + 1, LY_EMEM);
-    }
+    LY_ARRAY_CREATE_RET(ctx->ctx, llist->dflts, orig_count + (dflts ? LY_ARRAY_COUNT(dflts) : 1), LY_EMEM);
 
     /* fill each new default value */
     if (dflts) {
diff --git a/src/schema_compile_node.c b/src/schema_compile_node.c
index 49c376c..53d73ec 100644
--- a/src/schema_compile_node.c
+++ b/src/schema_compile_node.c
@@ -203,7 +203,7 @@
     LY_CHECK_ERR_RET(!dup, LOGMEM(ctx), NULL);
     if (orig->parts) {
         LY_ARRAY_CREATE_GOTO(ctx, dup->parts, LY_ARRAY_COUNT(orig->parts), ret, cleanup);
-        LY_ARRAY_COUNT(dup->parts) = LY_ARRAY_COUNT(orig->parts);
+        (*((LY_ARRAY_COUNT_TYPE*)(dup->parts) - 1)) = LY_ARRAY_COUNT(orig->parts);
         memcpy(dup->parts, orig->parts, LY_ARRAY_COUNT(dup->parts) * sizeof *dup->parts);
     }
     DUP_STRING_GOTO(ctx, orig->eapptag, dup->eapptag, ret, cleanup);
diff --git a/src/schema_features.c b/src/schema_features.c
index 16ccaed..15ffed9 100644
--- a/src/schema_features.c
+++ b/src/schema_features.c
@@ -105,7 +105,7 @@
     if (!*idx) {
         /* module features */
         features = pmod->features;
-    } else if (pmod->includes && ((*idx - 1) < LY_ARRAY_COUNT(pmod->includes))) {
+    } else if ((*idx - 1) < LY_ARRAY_COUNT(pmod->includes)) {
         /* submodule features */
         features = pmod->includes[*idx - 1].submodule->features;
     } else {
@@ -647,9 +647,6 @@
 
     for (v = 0; v < recursion.count; ++v) {
         drv = recursion.objs[v];
-        if (!drv->depfeatures) {
-            continue;
-        }
         for (u = 0; u < LY_ARRAY_COUNT(drv->depfeatures); ++u) {
             if (feature == drv->depfeatures[u]) {
                 LOGVAL(ctx, LY_VLOG_NONE, NULL, LYVE_REFERENCE, "Feature \"%s\" is indirectly referenced from itself.",
diff --git a/src/tree.h b/src/tree.h
index 911addb..cc8799b 100644
--- a/src/tree.h
+++ b/src/tree.h
@@ -140,15 +140,13 @@
  */
 #define LY_ARRAY_FOR_INDEX(ARRAY, INDEX) \
     for (INDEX = 0; \
-         ARRAY && INDEX < (*((LY_ARRAY_COUNT_TYPE*)(ARRAY) - 1)); \
+         INDEX < LY_ARRAY_COUNT(ARRAY); \
          ++INDEX)
 
 /**
  * @brief Get the number of records in the ARRAY.
- *
- * Does not check if array exists!
  */
-#define LY_ARRAY_COUNT(ARRAY) (*((LY_ARRAY_COUNT_TYPE*)(ARRAY) - 1))
+#define LY_ARRAY_COUNT(ARRAY) (ARRAY ? (*((LY_ARRAY_COUNT_TYPE*)(ARRAY) - 1)) : 0)
 
 /**
  * @brief Sized-array iterator (for-loop).
diff --git a/src/tree_data.c b/src/tree_data.c
index 97cd4de..31b53b1 100644
--- a/src/tree_data.c
+++ b/src/tree_data.c
@@ -1285,6 +1285,7 @@
             options & LYD_NEW_PATH_OUTPUT ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, LY_PREF_JSON,
             NULL, NULL, &p), cleanup);
 
+    assert(p);
     schema = p[LY_ARRAY_COUNT(p) - 1].node;
     if ((schema->nodetype == LYS_LIST) && (p[LY_ARRAY_COUNT(p) - 1].pred_type == LY_PATH_PREDTYPE_NONE) &&
             !(options & LYD_NEW_PATH_OPAQ)) {
diff --git a/src/tree_schema_helpers.c b/src/tree_schema_helpers.c
index 5fa24ba..2d645cb 100644
--- a/src/tree_schema_helpers.c
+++ b/src/tree_schema_helpers.c
@@ -109,7 +109,7 @@
     LY_ARRAY_COUNT_TYPE i, r;
     struct lysp_revision rev;
 
-    for (i = 1, r = 0; revs && i < LY_ARRAY_COUNT(revs); i++) {
+    for (i = 1, r = 0; i < LY_ARRAY_COUNT(revs); i++) {
         if (strcmp(revs[i].date, revs[r].date) > 0) {
             r = i;
         }