Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/parser_lyb.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,11 @@ lyb_parse_module(struct lyd_lyb_ctx *lybctx, char **name, char *revision, char *

/* module name */
LY_CHECK_RET(lyb_read_string(name, pctx));
assert((*name)[0]);
if (!(*name)[0]) {
LOGERR(pctx->ctx, LY_EINVAL, "Invalid LYB data, empty module name.");
rc = LY_EINVAL;
goto cleanup;
}

/* module revision */
LY_CHECK_GOTO(rc = lyb_read(&rev, 2 * 8, pctx), cleanup);
Expand Down Expand Up @@ -967,6 +971,12 @@ lyb_parse_schema_hash(struct lyd_lyb_ctx *lybctx, const struct lysc_node *sparen
return LY_SUCCESS;
}

if (!sparent && !mod) {
/* a child node at the top level or a child of an opaque node */
LOGERR(lybctx->parse_ctx->ctx, LY_EINVAL, "Invalid LYB data, schema node hash without a schema parent.");
return LY_EINVAL;
}

getnext_opts = lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;

/* find our node with matching hashes */
Expand Down
25 changes: 25 additions & 0 deletions tests/utests/data/test_lyb.c
Original file line number Diff line number Diff line change
Expand Up @@ -2937,6 +2937,30 @@ test_memory_bounds(void **state)
lyd_free_all(parsed);
}

static void
test_invalid_hash_parent(void **state)
{
struct lyd_node *tree = NULL;

/* top-level node with an empty module name */
const char empty_mod[] = {0x12, 0x00, 0x03};

/* top-level node encoded as a child, so without any schema parent */
const char top_child[] = {0x02, (char)0xfb, 0x02, (char)0xfb};

(void)state;

assert_int_equal(LY_EINVAL, lyd_parse_data_mem_len(UTEST_LYCTX, empty_mod, sizeof empty_mod, LYD_LYB,
LYD_PARSE_STRICT, 0, &tree));
assert_null(tree);
CHECK_LOG_CTX("Invalid LYB data, empty module name.", NULL, 0);

assert_int_equal(LY_EINVAL, lyd_parse_data_mem_len(UTEST_LYCTX, top_child, sizeof top_child, LYD_LYB,
LYD_PARSE_STRICT, 0, &tree));
assert_null(tree);
CHECK_LOG_CTX("Invalid LYB data, schema node hash without a schema parent.", NULL, 0);
}

int
main(void)
{
Expand All @@ -2955,6 +2979,7 @@ main(void)
UTEST(test_different_contexts, setup),
UTEST(test_skip_module_check, setup),
UTEST(test_memory_bounds),
UTEST(test_invalid_hash_parent),
};

return cmocka_run_group_tests(tests, NULL, NULL);
Expand Down