diff --git a/src/json.c b/src/json.c index b075a8541..ee328d2d8 100644 --- a/src/json.c +++ b/src/json.c @@ -15,7 +15,6 @@ #include #include -#include #include #include #include @@ -481,7 +480,7 @@ lyjson_number_is_zero(const char *in, const char *end) in++; assert(in < end); } - if ((in[0] == '0') && (in[1] == '.')) { + if ((in[0] == '0') && (in + 1 < end) && (in[1] == '.')) { in += 2; if (!(in < end)) { return 1; @@ -603,7 +602,7 @@ lyjson_exp_number(const struct ly_ctx *ctx, const char *in, const char *exponent /* Final position of decimal point in the buf. */ int32_t dp_position; /* Exponent as integer. */ - long long e_val; + int64_t e_val; /* Byte for the decimal point. */ int8_t dot; /* Required additional byte for the minus sign. */ @@ -621,10 +620,8 @@ lyjson_exp_number(const struct ly_ctx *ctx, const char *in, const char *exponent return LY_EVALID; } - /* Convert exponent. */ - errno = 0; - e_val = strtoll(exponent + 1, NULL, LY_BASE_DEC); - if (errno || (e_val > UINT16_MAX) || (e_val < -UINT16_MAX)) { + /* Convert exponent, the input may not be terminated. */ + if (ly_parse_int(exponent + 1, (in + total_len) - (exponent + 1), -UINT16_MAX, UINT16_MAX, LY_BASE_DEC, &e_val)) { LOGVAL(ctx, NULL, LYVE_SEMANTICS, "Exponent out-of-bounds in a JSON Number value (%.*s).", (int)total_len, in); return LY_EVALID; } diff --git a/src/parser_common.c b/src/parser_common.c index 4bd16e4b9..22573e399 100644 --- a/src/parser_common.c +++ b/src/parser_common.c @@ -179,9 +179,14 @@ lyd_parser_get_snode(struct lyd_ctx *lydctx, ly_bool is_attr, const char *prefix if (!prefix_len) { prefix = NULL; } - r = lys_find_child_node(parent ? LYD_CTX(parent) : lydctx->data_ctx->ctx, sparent, mod, prefix, prefix_len, - LY_VALUE_JSON, NULL, name, name_len, getnext_opts, snode, ext); - LY_CHECK_RET(r && (r != LY_ENOT), r); + if (!name_len) { + /* no schema node has an empty name and ::lys_find_child_node() would take the name for NULL-terminated */ + r = LY_ENOT; + } else { + r = lys_find_child_node(parent ? LYD_CTX(parent) : lydctx->data_ctx->ctx, sparent, mod, prefix, prefix_len, + LY_VALUE_JSON, NULL, name, name_len, getnext_opts, snode, ext); + LY_CHECK_RET(r && (r != LY_ENOT), r); + } if (!r) { /* check that schema node is valid and can be used but rely on that implicitly for extensions */ diff --git a/src/plugins_types.c b/src/plugins_types.c index 065284150..308df6fdc 100644 --- a/src/plugins_types.c +++ b/src/plugins_types.c @@ -771,7 +771,8 @@ lyplg_type_lypath_new(const struct ly_ctx *ctx, const char *value, uint32_t valu e = (struct ly_err_item *)ly_err_last(ctx); /* parse the value */ - ret = ly_path_parse(ctx, ctx_node, value, value_len, 0, LY_PATH_BEGIN_ABSOLUTE, prefix_opt, LY_PATH_PRED_SIMPLE, &exp); + ret = ly_path_parse(ctx, ctx_node, value_len ? value : "", value_len, 0, LY_PATH_BEGIN_ABSOLUTE, prefix_opt, + LY_PATH_PRED_SIMPLE, &exp); if (ret) { err_fmt = "Invalid instance-identifier \"%.*s\" value - syntax error%s%s"; goto cleanup; diff --git a/src/xml.c b/src/xml.c index 732c46757..c686b5765 100644 --- a/src/xml.c +++ b/src/xml.c @@ -157,6 +157,29 @@ lyxml_logval_instrexp(struct lyxml_ctx *xmlctx, const char *param) param); } +/** + * @brief Get the length of the input data to print in an error message. + * + * @param[in] xmlctx XML parser. + * @param[in] offset Offset of the printed data before the current input position. + * @param[in] max Maximum length to print. + * @return Length to use with the "%.*s" format. + */ +static int +lyxml_logval_len(const struct lyxml_ctx *xmlctx, uint32_t offset, uint32_t max) +{ + size_t len = max; + + if (xmlctx->in->bounded) { + len = xmlctx->in->length - (xmlctx->in->current - xmlctx->in->start) + offset; + if (len > max) { + len = max; + } + } + + return (int)len; +} + /** * @brief Ignore and skip any characters until the delim of the size delim_len is read, including the delim * @@ -173,18 +196,22 @@ skip_section(struct lyxml_ctx *xmlctx, const char *delim, uint32_t delim_len, co register const char *input, *a, *b; uint64_t parsed = 0, newlines = 0; - for (input = xmlctx->in->current; *input; ++input, ++parsed) { + for (input = xmlctx->in->current; ; ++input, ++parsed) { + LY_CHECK_RET(lyxml_check_read(xmlctx, parsed + 1)); + if (!*input) { + break; + } + if (*input != *delim) { if (*input == '\n') { ++newlines; } - LY_CHECK_RET(lyxml_check_read(xmlctx, parsed)); continue; } a = input; b = delim; for (i = 0; i < delim_len; ++i) { - LY_CHECK_RET(lyxml_check_read(xmlctx, parsed + i)); + LY_CHECK_RET(lyxml_check_read(xmlctx, parsed + i + 1)); if (*a++ != *b++) { break; @@ -228,7 +255,8 @@ lyxml_parse_identifier(struct lyxml_ctx *xmlctx, const char **start, const char /* check NameStartChar (minus colon) */ LY_CHECK_RET(lyxml_get_utf8(xmlctx, &c, &parsed)); if (!is_xmlqnamestartchar(c)) { - LOGVAL(xmlctx->ctx, NULL, LYVE_SYNTAX, "Identifier \"%s\" starts with an invalid character.", xmlctx->in->current); + LOGVAL(xmlctx->ctx, NULL, LYVE_SYNTAX, "Identifier \"%.*s\" starts with an invalid character.", + lyxml_logval_len(xmlctx, 0, LY_VCODE_INSTREXP_MAXLEN), xmlctx->in->current); return LY_EVALID; } @@ -438,7 +466,8 @@ lyxml_skip_until_end_or_after_otag(struct lyxml_ctx *xmlctx) LOGVAL(ctx, NULL, LY_VCODE_NSUPP, "Document Type Declaration"); return LY_EVALID; } else { - LOGVAL(ctx, NULL, LYVE_SYNTAX, "Unknown XML section \"%.20s\".", &xmlctx->in->current[-2]); + LOGVAL(ctx, NULL, LYVE_SYNTAX, "Unknown XML section \"%.*s\".", + lyxml_logval_len(xmlctx, 2, LY_VCODE_INSTREXP_MAXLEN), &xmlctx->in->current[-2]); return LY_EVALID; } LY_CHECK_RET(skip_section(xmlctx, endtag, endtag_len, sectname)); @@ -641,7 +670,7 @@ lyxml_parse_value_reference(struct lyxml_ctx *xmlctx, char *buf, uint32_t *len) } else { LOGVAL(ctx, NULL, LYVE_SYNTAX, "Entity reference \"%.*s\" not supported, only predefined references allowed.", - 10, &xmlctx->in->current[-1]); + lyxml_logval_len(xmlctx, 1, 10), &xmlctx->in->current[-1]); goto error; } } diff --git a/tests/utests/basic/test_json.c b/tests/utests/basic/test_json.c index 92c7abfbf..11b6ede90 100644 --- a/tests/utests/basic/test_json.c +++ b/tests/utests/basic/test_json.c @@ -638,6 +638,34 @@ test_array(void **state) CHECK_LOG_CTX("Invalid character sequence \", null]}\", expected a JSON value.", NULL, 1); } +/** + * @brief Parse JSON data from an exactly-sized buffer that is not NULL-terminated. + * + * @param[in] ctx Context to use. + * @param[in] str Data to parse, copied into the buffer. + * @param[out] tree Parsed data tree. + * @return LY_ERR value. + */ +static LY_ERR +parse_chunk(struct ly_ctx *ctx, const char *str, struct lyd_node **tree) +{ + LY_ERR rc; + struct ly_in *in; + uint32_t len = strlen(str); + char *chunk; + + chunk = malloc(len); + assert_non_null(chunk); + memcpy(chunk, str, len); + + assert_int_equal(LY_SUCCESS, ly_in_new_memory_chunk(chunk, len, &in)); + rc = lyd_parse_data(ctx, NULL, in, LYD_JSON, 0, LYD_VALIDATE_PRESENT, tree); + ly_in_free(in, 0); + free(chunk); + + return rc; +} + static void test_bound_input(void **state) { @@ -654,6 +682,12 @@ test_bound_input(void **state) " leaf emp {" " type empty;" " }" + " leaf num {" + " type int32;" + " }" + " leaf iid {" + " type instance-identifier;" + " }" "}"; assert_int_equal(LY_SUCCESS, lys_parse_mem(ctx, yang, LYS_IN_YANG, NULL)); @@ -733,6 +767,21 @@ test_bound_input(void **state) assert_int_equal(LY_EVALID, lyd_parse_data(ctx, NULL, in, LYD_JSON, 0, LYD_VALIDATE_PRESENT, &tree)); ly_in_free(in, 0); CHECK_LOG_CTX("Unexpected end-of-input.", NULL, 1); + + /* values ending with the input, use a buffer with no terminating byte at all */ + assert_int_equal(LY_EVALID, parse_chunk(ctx, "{\"test:num\":0", &tree)); + CHECK_LOG_CTX("Unexpected end-of-input.", NULL, 1); + + assert_int_equal(LY_EVALID, parse_chunk(ctx, "{\"test:num\":1e2", &tree)); + CHECK_LOG_CTX("Unexpected end-of-input.", NULL, 1); + + /* zero-length object member name and value */ + assert_int_equal(LY_EVALID, parse_chunk(ctx, "{\"\":true}", &tree)); + CHECK_LOG_CTX("Top-level object member \"\" must be namespace-qualified.", NULL, 1); + + assert_int_equal(LY_EVALID, parse_chunk(ctx, "{\"test:iid\":\"\"}", &tree)); + CHECK_LOG_CTX("Invalid instance-identifier \"\" value - syntax error: XPath \"\" was expected to be absolute.", + "/test:iid", 1); } int diff --git a/tests/utests/basic/test_xml.c b/tests/utests/basic/test_xml.c index 1a6bfd2d0..ab74c0616 100644 --- a/tests/utests/basic/test_xml.c +++ b/tests/utests/basic/test_xml.c @@ -412,6 +412,34 @@ test_simple_xml(void **state) tree = NULL; } +/** + * @brief Parse XML data from an exactly-sized buffer that is not NULL-terminated. + * + * @param[in] ctx Context to use. + * @param[in] str Data to parse, copied into the buffer. + * @param[out] tree Parsed data tree. + * @return LY_ERR value. + */ +static LY_ERR +parse_chunk(struct ly_ctx *ctx, const char *str, struct lyd_node **tree) +{ + LY_ERR rc; + struct ly_in *in; + uint32_t len = strlen(str); + char *chunk; + + chunk = malloc(len); + assert_non_null(chunk); + memcpy(chunk, str, len); + + assert_int_equal(LY_SUCCESS, ly_in_new_memory_chunk(chunk, len, &in)); + rc = lyd_parse_data(ctx, NULL, in, LYD_XML, 0, LYD_VALIDATE_PRESENT, tree); + ly_in_free(in, 0); + free(chunk); + + return rc; +} + static void test_bound_input(void **state) { @@ -493,6 +521,23 @@ test_bound_input(void **state) assert_int_equal(LY_EVALID, lyd_parse_data(ctx, NULL, in, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree)); ly_in_free(in, 0); CHECK_LOG_CTX("Unexpected end-of-input.", NULL, 1); + + /* sections ending with the input, use a buffer with no terminating byte at all */ + assert_int_equal(LY_EVALID, parse_chunk(ctx, "