diff --git a/src/plugins_types/date.c b/src/plugins_types/date.c index c0fa883d2..af9fcec3d 100644 --- a/src/plugins_types/date.c +++ b/src/plugins_types/date.c @@ -131,6 +131,13 @@ lyplg_type_store_date(const struct ly_ctx *ctx, const struct lysc_type *type, co } if (val) { + if (value_size < 10) { + /* not even a full date, the pattern was not checked with LYPLG_TYPE_STORE_ONLY */ + ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to parse %s value \"%.*s\".", type->name, + (int)value_size, (char *)value); + goto cleanup; + } + /* create date-and-time value */ if (asprintf(&str, "%.*sT00:00:00%.*s", 10, (char *)value, (int)(value_size - 10), (char *)value + 10) == -1) { ret = ly_err_new_no_mem(err); diff --git a/tests/utests/types/yang_types.c b/tests/utests/types/yang_types.c index 85398b8f8..f918d5b6e 100644 --- a/tests/utests/types/yang_types.c +++ b/tests/utests/types/yang_types.c @@ -80,7 +80,8 @@ static void test_data_xml(void **state) { - const char *schema; + const char *schema, *data; + struct lyd_node *tree; /* xml test */ schema = MODULE_CREATE_YANG("a", @@ -151,6 +152,16 @@ test_data_xml(void **state) "\"[0-9]{4}-(1[0-2]|0[1-9])-(0[1-9]|[1-2][0-9]|3[0-1])(Z|[\\+\\-]((1[0-3]|0[0-9]):([0-5][0-9])|14:00))?\".", "/a:l2", 1); + /* date stored only, shorter than a date */ + data = "22005-05-31"; + CHECK_PARSE_LYD_PARAM(data, LYD_XML, LYD_PARSE_STORE_ONLY, 0, LY_EVALID, tree); + assert_null(tree); + CHECK_LOG_CTX("Failed to parse date value \"2\".", "/a:l2", 1); + data = "2"; + CHECK_PARSE_LYD_PARAM(data, LYD_XML, LYD_PARSE_STORE_ONLY, 0, LY_EVALID, tree); + assert_null(tree); + CHECK_LOG_CTX("Failed to parse date value \"2\".", "/a:l2", 1); + /* date-no-zone */ TEST_SUCCESS_XML("a", "l2nz", "2005-05-31", STRING, "2005-05-31"); #ifndef _WIN32