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
15 changes: 12 additions & 3 deletions src/parser_cbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,15 +381,24 @@ lydcbor_item_to_string(const cbor_item_t *item, char **str_val, size_t *str_len)
break;
}
case CBOR_TYPE_NEGINT: {
int64_t val = -1 - (int64_t)cbor_get_int(item);
int len = snprintf(NULL, 0, "%" PRId64, val);
/* the value is -1 - val, which does not fit into int64_t for val > INT64_MAX */
uint64_t val = cbor_get_int(item);
int len;

if (val == UINT64_MAX) {
*str_val = strdup("-18446744073709551616");
LY_CHECK_ERR_RET(!*str_val, LOGMEM(NULL), LY_EMEM);
*str_len = strlen(*str_val);
break;
}

len = snprintf(NULL, 0, "-%" PRIu64, val + 1);
if (len < 0) {
return LY_ESYS;
}
*str_val = malloc(len + 1);
LY_CHECK_ERR_RET(!*str_val, LOGMEM(NULL), LY_EMEM);
sprintf(*str_val, "%" PRId64, val);
sprintf(*str_val, "-%" PRIu64, val + 1);
*str_len = len;
break;
}
Expand Down
21 changes: 21 additions & 0 deletions tests/utests/data/test_cbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,26 @@ test_invalid_input(void **state)
CHECK_LOG_CTX("Value \"1000\" is out of type int8 min/max bounds.", "/cbor-test:lf-intg8", 0);
}

static void
test_negint(void **state)
{
struct lyd_node *tree;

/* {"cbor-test:lf-uintg8": -18446744073709551611} */
const char data1[] = "\xa1\x73" "cbor-test:lf-uintg8" "\x3b\xff\xff\xff\xff\xff\xff\xff\xfa";

/* {"cbor-test:lf-uintg8": -18446744073709551616} */
const char data2[] = "\xa1\x73" "cbor-test:lf-uintg8" "\x3b\xff\xff\xff\xff\xff\xff\xff\xff";

assert_int_equal(LY_EVALID, lyd_parse_data_mem_len(UTEST_LYCTX, data1, sizeof data1 - 1, LYD_CBOR, 0,
LYD_VALIDATE_PRESENT, &tree));
CHECK_LOG_CTX("Value \"-18446744073709551611\" is out of type uint8 min/max bounds.", "/cbor-test:lf-uintg8", 1);

assert_int_equal(LY_EVALID, lyd_parse_data_mem_len(UTEST_LYCTX, data2, sizeof data2 - 1, LYD_CBOR, 0,
LYD_VALIDATE_PRESENT, &tree));
CHECK_LOG_CTX("Invalid type uint8 value \"-18446744073709551616\".", "/cbor-test:lf-uintg8", 1);
}

int
main(void)
{
Expand All @@ -419,6 +439,7 @@ main(void)
UTEST(test_opaque, setup),
UTEST(test_attributes, setup),
UTEST(test_invalid_input, setup),
UTEST(test_negint, setup),
};

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