context CHANGE refactor ly_ctx_get_module_iter()
diff --git a/src/context.c b/src/context.c
index c16c250..d3ec7a3 100644
--- a/src/context.c
+++ b/src/context.c
@@ -317,11 +317,11 @@
 {
     LY_CHECK_ARG_RET(ctx, ctx, index, NULL);
 
-    for ( ; *index < (unsigned)ctx->list.count; (*index)++) {
+    if (*index < (unsigned)ctx->list.count) {
         return ctx->list.objs[(*index)++];
+    } else {
+        return NULL;
     }
-
-    return NULL;
 }
 
 /**
diff --git a/tests/src/test_context.c b/tests/src/test_context.c
index 2a3c302..756365e 100644
--- a/tests/src/test_context.c
+++ b/tests/src/test_context.c
@@ -364,6 +364,9 @@
     const char *str1 = "module a {namespace urn:a;prefix a;revision 2018-10-23;}";
     const char *str2 = "module a {namespace urn:a;prefix a;revision 2018-10-23;revision 2018-10-24;}";
 
+    unsigned int index = 0;
+    const char *names[] = {"ietf-yang-metadata", "yang", "ietf-inet-types", "ietf-yang-types", "ietf-datastores", "ietf-yang-library", "a", "a", "a"};
+
     assert_int_equal(LY_SUCCESS, ly_ctx_new(NULL, 0, &ctx));
 
     /* invalid arguments */
@@ -416,6 +419,11 @@
     assert_null(lys_parse_mem_module(ctx, str1, LYS_IN_YANG, 1, NULL, NULL));
     logbuf_assert("Input data contains submodule which cannot be parsed directly without its main module.");
 
+    while ((mod = ly_ctx_get_module_iter(ctx, &index))) {
+        assert_string_equal(names[index - 1], mod->name);
+    }
+    assert_int_equal(9, index);
+
     /* cleanup */
     *state = NULL;
     ly_ctx_destroy(ctx, NULL);