Skip to content
Merged
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
5 changes: 2 additions & 3 deletions src/lcbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,11 @@ lycbor_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, struct lycbor_ctx **c
cborctx->cbor_data = cbor_load((cbor_data)in->current, in->length, &result);
if (!cborctx->cbor_data) {
LOGVAL(ctx, NULL, LYVE_SYNTAX, "Failed to parse CBOR data (%s).", lycbor_strerror(result.error.code));
free(cborctx);
lycbor_ctx_free(cborctx);
return LY_EVALID;
} else if (result.error.code != CBOR_ERR_NONE) {
LOGVAL(ctx, NULL, LYVE_SYNTAX, "CBOR parsing error (%s).", lycbor_strerror(result.error.code));
cbor_decref(&cborctx->cbor_data);
free(cborctx);
lycbor_ctx_free(cborctx);
return LY_EVALID;
}

Expand Down
14 changes: 14 additions & 0 deletions tests/utests/data/test_cbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,19 @@ test_attributes(void **state)
lyd_free_all(tree);
}

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

/* truncated map, the input must not stay referenced for logging after the failed parse */
CHECK_PARSE_LYD_PARAM_LEN("\xa1", 1, LYD_CBOR, 0, 0, LY_EVALID, tree);
CHECK_LOG_CTX("Failed to parse CBOR data (not enough data).", NULL, 1);

assert_int_equal(LY_EVALID, lyd_new_path(NULL, UTEST_LYCTX, "/cbor-test:lf-intg8", "1000", 0, &tree));
CHECK_LOG_CTX("Value \"1000\" is out of type int8 min/max bounds.", "/cbor-test:lf-intg8", 0);
}

int
main(void)
{
Expand All @@ -405,6 +418,7 @@ main(void)
UTEST(test_schema_mount, setup),
UTEST(test_opaque, setup),
UTEST(test_attributes, setup),
UTEST(test_invalid_input, setup),
};

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