Skip to content
Merged
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
11 changes: 4 additions & 7 deletions src/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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. */
Expand All @@ -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;
}
Expand Down
11 changes: 8 additions & 3 deletions src/parser_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
3 changes: 2 additions & 1 deletion src/plugins_types.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
41 changes: 35 additions & 6 deletions src/xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
}
Expand Down
49 changes: 49 additions & 0 deletions tests/utests/basic/test_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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));
Expand Down Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions tests/utests/basic/test_xml.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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, "<!-- comment", &tree));
CHECK_LOG_CTX("Unexpected end-of-input.", NULL, 1);

assert_int_equal(LY_EVALID, parse_chunk(ctx, "<!-- comment --", &tree));
CHECK_LOG_CTX("Unexpected end-of-input.", NULL, 1);

assert_int_equal(LY_EVALID, parse_chunk(ctx, "<?pi data?", &tree));
CHECK_LOG_CTX("Unexpected end-of-input.", NULL, 1);

/* error messages printing the input that ends with it */
assert_int_equal(LY_EVALID, parse_chunk(ctx, "<!NONSENSE", &tree));
CHECK_LOG_CTX("Unknown XML section \"<!NONSENSE\".", NULL, 1);

assert_int_equal(LY_EVALID, parse_chunk(ctx, "<¢", &tree));
CHECK_LOG_CTX("Identifier \"¢\" starts with an invalid character.", NULL, 1);
}

int
Expand Down
Loading