diff --git a/src/lcbor.c b/src/lcbor.c index 4d9035a5f..afb3e375e 100644 --- a/src/lcbor.c +++ b/src/lcbor.c @@ -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; } diff --git a/tests/utests/data/test_cbor.c b/tests/utests/data/test_cbor.c index 135ed9e1c..fab33ad4c 100644 --- a/tests/utests/data/test_cbor.c +++ b/tests/utests/data/test_cbor.c @@ -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) { @@ -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);