From 383832b003e8b93f4f60823464c42541a2775734 Mon Sep 17 00:00:00 2001 From: Affan Ziya Date: Sun, 15 Nov 2026 21:00:25 +0530 Subject: [PATCH] json BUGFIX exponent numbers with a leading zero For a number such as 0.xxxEn whose new decimal point falls inside the digits, lyjson_exp_number() placed the point at n - 1 instead of n minus the skipped leading zeros and reserved no byte for it, so the last digit was cut off. 0.25E2 became "2." and 0.5E1 became ".", and 0.0034E3 (3.4) became "34", which an integer leaf accepts. Anydata content kept the converted text, printing -0.00642406E+4 as -642.40. --- src/json.c | 9 +++++---- tests/utests/basic/test_json.c | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/json.c b/src/json.c index 042ea9b519..4da22fe734 100644 --- a/src/json.c +++ b/src/json.c @@ -564,21 +564,22 @@ lyjson_exp_number(const struct ly_ctx *ctx, const char *in, const char *exponent /* Set a new range of 'numeric part'. Old decimal point is skipped. */ num++; num_len--; - dp_position--; /* Get the number of useless zeros between the old * and new decimal point. For example, in the number 0.005E1, * there is one useless zero. */ - zeros = lyjson_count_in_row(num, num + dp_position + 1, '0', FORWARD); + zeros = lyjson_count_in_row(num, num + dp_position, '0', FORWARD); /* If the new decimal point will be in the place of the first non-zero subnumber. */ - if (zeros == (dp_position + 1)) { + if (zeros == dp_position) { /* keep one zero as leading zero */ zeros--; /* new decimal point will be behind the leading zero */ dp_position = 1; dot = 1; } else { - dot = 0; + /* the useless zeros are skipped, no decimal point if there are no digits behind it */ + dp_position -= zeros; + dot = (dp_position < (int32_t)(num_len - zeros)) ? 1 : 0; } buf_len = minus + dot + (num_len - zeros); LY_CHECK_RET(lyjson_get_buffer_for_number(ctx, buf_len, &buf)); diff --git a/tests/utests/basic/test_json.c b/tests/utests/basic/test_json.c index f50ac0498e..f2c629bae7 100644 --- a/tests/utests/basic/test_json.c +++ b/tests/utests/basic/test_json.c @@ -271,6 +271,27 @@ test_number(void **state) lyd_free_all(tree); tree = NULL; + str = "0.25E2"; + assert_non_null(ly_in_memory(in, str)); + assert_int_equal(LY_SUCCESS, lyd_parse_value_fragment(ctx, "/test:num", in, LYD_JSON, 0, 0, 0, &tree)); + assert_string_equal("25", lyd_get_value(tree)); + lyd_free_all(tree); + tree = NULL; + + str = "-0.62098E5"; + assert_non_null(ly_in_memory(in, str)); + assert_int_equal(LY_SUCCESS, lyd_parse_value_fragment(ctx, "/test:num", in, LYD_JSON, 0, 0, 0, &tree)); + assert_string_equal("-62098", lyd_get_value(tree)); + lyd_free_all(tree); + tree = NULL; + + str = "0.0034E3"; + assert_non_null(ly_in_memory(in, str)); + assert_int_equal(LY_EVALID, lyd_parse_value_fragment(ctx, "/test:num", in, LYD_JSON, 0, 0, 0, &tree)); + CHECK_LOG_CTX("Invalid union value \"3.4\" - no matching subtype found:\n" + " ly2 integers: Invalid type int32 value \"3.4\".\n" + " ly2 decimal64: Invalid non-string-encoded decimal64 value \"3.4\".\n", "/test:num", 1); + /* decimal number, negative exponent */ str = "\"35.94e-1\""; assert_non_null(ly_in_memory(in, str));