Skip to content

Commit 9ddf2d7

Browse files
affan-archmichalvasko
authored andcommitted
cbor BUGFIX stale input log location on load failure
lycbor_ctx_new() freed the context directly when cbor_load() failed, skipping ly_log_location_revert(), so the freed ly_in stayed on the log location stack and the next logged error read its line.
1 parent 4a6fea2 commit 9ddf2d7

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

‎src/lcbor.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,11 @@ lycbor_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, struct lycbor_ctx **c
133133
cborctx->cbor_data = cbor_load((cbor_data)in->current, in->length, &result);
134134
if (!cborctx->cbor_data) {
135135
LOGVAL(ctx, NULL, LYVE_SYNTAX, "Failed to parse CBOR data (%s).", lycbor_strerror(result.error.code));
136-
free(cborctx);
136+
lycbor_ctx_free(cborctx);
137137
return LY_EVALID;
138138
} else if (result.error.code != CBOR_ERR_NONE) {
139139
LOGVAL(ctx, NULL, LYVE_SYNTAX, "CBOR parsing error (%s).", lycbor_strerror(result.error.code));
140-
cbor_decref(&cborctx->cbor_data);
141-
free(cborctx);
140+
lycbor_ctx_free(cborctx);
142141
return LY_EVALID;
143142
}
144143

‎tests/utests/data/test_cbor.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,19 @@ test_attributes(void **state)
395395
lyd_free_all(tree);
396396
}
397397

398+
static void
399+
test_invalid_input(void **state)
400+
{
401+
struct lyd_node *tree = NULL;
402+
403+
/* truncated map, the input must not stay referenced for logging after the failed parse */
404+
CHECK_PARSE_LYD_PARAM_LEN("\xa1", 1, LYD_CBOR, 0, 0, LY_EVALID, tree);
405+
CHECK_LOG_CTX("Failed to parse CBOR data (not enough data).", NULL, 1);
406+
407+
assert_int_equal(LY_EVALID, lyd_new_path(NULL, UTEST_LYCTX, "/cbor-test:lf-intg8", "1000", 0, &tree));
408+
CHECK_LOG_CTX("Value \"1000\" is out of type int8 min/max bounds.", "/cbor-test:lf-intg8", 0);
409+
}
410+
398411
int
399412
main(void)
400413
{
@@ -405,6 +418,7 @@ main(void)
405418
UTEST(test_schema_mount, setup),
406419
UTEST(test_opaque, setup),
407420
UTEST(test_attributes, setup),
421+
UTEST(test_invalid_input, setup),
408422
};
409423

410424
return cmocka_run_group_tests(tests, NULL, NULL);

0 commit comments

Comments
 (0)