Skip to content

Commit a738c73

Browse files
committed
plugins types BUGFIX missing type validation in separate validation
Temporary revert of d866dd9, will be implemented properly in new SO version.
1 parent 7783869 commit a738c73

File tree

14 files changed

+300
-108
lines changed

14 files changed

+300
-108
lines changed

src/plugins_types/binary.c

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
*/
4343
static const char b64_etable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
4444

45+
static LY_ERR lyplg_type_validate_binary(const struct ly_ctx *UNUSED(ctx), const struct lysc_type *type, const struct lyd_node *UNUSED(ctx_node), const struct lyd_node *UNUSED(tree), struct lyd_value *storage, struct ly_err_item **err);
46+
4547
/**
4648
* @brief Encode binary value into a base64 string value.
4749
*
@@ -258,7 +260,6 @@ lyplg_type_store_binary(const struct ly_ctx *ctx, const struct lysc_type *type,
258260
struct ly_err_item **err)
259261
{
260262
LY_ERR ret = LY_SUCCESS;
261-
struct lysc_type_bin *type_bin = (struct lysc_type_bin *)type;
262263
struct lyd_value_binary *val;
263264

264265
/* init storage */
@@ -320,11 +321,9 @@ lyplg_type_store_binary(const struct ly_ctx *ctx, const struct lysc_type *type,
320321
}
321322

322323
if (!(options & LYPLG_TYPE_STORE_ONLY)) {
323-
/* validate length restriction of the binary value */
324-
if (type_bin->length) {
325-
ret = lyplg_type_validate_range(LY_TYPE_BINARY, type_bin->length, val->size, value, value_len, err);
326-
LY_CHECK_GOTO(ret, cleanup);
327-
}
324+
/* validate value */
325+
ret = lyplg_type_validate_binary(ctx, type, NULL, NULL, storage, err);
326+
LY_CHECK_GOTO(ret, cleanup);
328327
}
329328

330329
cleanup:
@@ -338,6 +337,33 @@ lyplg_type_store_binary(const struct ly_ctx *ctx, const struct lysc_type *type,
338337
return ret;
339338
}
340339

340+
/**
341+
* @brief Implementation of ::lyplg_type_validate_clb for the binary type.
342+
*/
343+
static LY_ERR
344+
lyplg_type_validate_binary(const struct ly_ctx *ctx, const struct lysc_type *type, const struct lyd_node *UNUSED(ctx_node),
345+
const struct lyd_node *UNUSED(tree), struct lyd_value *storage, struct ly_err_item **err)
346+
{
347+
struct lysc_type_bin *type_bin = (struct lysc_type_bin *)type;
348+
struct lyd_value_binary *val;
349+
const void *value;
350+
size_t value_len;
351+
352+
LY_CHECK_ARG_RET(NULL, type, storage, err, LY_EINVAL);
353+
354+
val = LYPLG_TYPE_VAL_IS_DYN(val) ? (struct lyd_value_binary *)(storage->dyn_mem) : (struct lyd_value_binary *)(storage->fixed_mem);
355+
value = lyd_value_get_canonical(ctx, storage);
356+
value_len = strlen(value);
357+
*err = NULL;
358+
359+
/* length restriction of the binary value */
360+
if (type_bin->length) {
361+
LY_CHECK_RET(lyplg_type_validate_range(LY_TYPE_BINARY, type_bin->length, val->size, value, value_len, err));
362+
}
363+
364+
return LY_SUCCESS;
365+
}
366+
341367
LIBYANG_API_DEF LY_ERR
342368
lyplg_type_compare_binary(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
343369
{
@@ -473,7 +499,7 @@ const struct lyplg_type_record plugins_binary[] = {
473499

474500
.plugin.id = "libyang 2 - binary, version 1",
475501
.plugin.store = lyplg_type_store_binary,
476-
.plugin.validate = NULL,
502+
.plugin.validate = lyplg_type_validate_binary,
477503
.plugin.compare = lyplg_type_compare_binary,
478504
.plugin.sort = lyplg_type_sort_binary,
479505
.plugin.print = lyplg_type_print_binary,

src/plugins_types/date_and_time.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ lyplg_type_store_date_and_time(const struct ly_ctx *ctx, const struct lysc_type
5353
struct ly_err_item **err)
5454
{
5555
LY_ERR ret = LY_SUCCESS;
56+
struct lysc_type_str *type_dat = (struct lysc_type_str *)type;
5657
struct lyd_value_date_and_time *val;
5758
uint32_t i;
5859
char c;
@@ -101,9 +102,20 @@ lyplg_type_store_date_and_time(const struct ly_ctx *ctx, const struct lysc_type
101102
ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
102103
LY_CHECK_GOTO(ret, cleanup);
103104

104-
/* convert to UNIX time and fractions of second, function must check for all the possible errors */
105-
if (ly_time_str2time(value, &val->time, &val->fractions_s)) {
106-
ret = ly_err_new(err, LY_EVALID, 0, NULL, NULL, "%s", ly_last_logmsg());
105+
/* length restriction, there can be only ASCII chars */
106+
if (type_dat->length) {
107+
ret = lyplg_type_validate_range(LY_TYPE_STRING, type_dat->length, value_len, value, value_len, err);
108+
LY_CHECK_GOTO(ret, cleanup);
109+
}
110+
111+
/* date-and-time pattern */
112+
ret = lyplg_type_validate_patterns(type_dat->patterns, value, value_len, err);
113+
LY_CHECK_GOTO(ret, cleanup);
114+
115+
/* convert to UNIX time and fractions of second */
116+
ret = ly_time_str2time(value, &val->time, &val->fractions_s);
117+
if (ret) {
118+
ret = ly_err_new(err, ret, 0, NULL, NULL, "%s", ly_last_logmsg());
107119
goto cleanup;
108120
}
109121

src/plugins_types/decimal64.c

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
* | 8 | yes | `int64_t *` | little-endian value represented without floating point |
3434
*/
3535

36+
static LY_ERR lyplg_type_validate_decimal64(const struct ly_ctx *UNUSED(ctx), const struct lysc_type *type, const struct lyd_node *UNUSED(ctx_node), const struct lyd_node *UNUSED(tree), struct lyd_value *storage, struct ly_err_item **err);
37+
3638
/**
3739
* @brief Convert decimal64 number to canonical string.
3840
*
@@ -142,12 +144,8 @@ lyplg_type_store_decimal64(const struct ly_ctx *ctx, const struct lysc_type *typ
142144

143145
if (!(options & LYPLG_TYPE_STORE_ONLY)) {
144146
/* validate value */
145-
if (type_dec->range) {
146-
/* check range of the number */
147-
ret = lyplg_type_validate_range(type->basetype, type_dec->range, num, storage->_canonical,
148-
strlen(storage->_canonical), err);
149-
LY_CHECK_GOTO(ret, cleanup);
150-
}
147+
ret = lyplg_type_validate_decimal64(ctx, type, NULL, NULL, storage, err);
148+
LY_CHECK_GOTO(ret, cleanup);
151149
}
152150

153151
cleanup:
@@ -161,6 +159,31 @@ lyplg_type_store_decimal64(const struct ly_ctx *ctx, const struct lysc_type *typ
161159
return ret;
162160
}
163161

162+
/**
163+
* @brief Implementation of ::lyplg_type_validate_clb for the built-in decimal64 type.
164+
*/
165+
static LY_ERR
166+
lyplg_type_validate_decimal64(const struct ly_ctx *UNUSED(ctx), const struct lysc_type *type, const struct lyd_node *UNUSED(ctx_node),
167+
const struct lyd_node *UNUSED(tree), struct lyd_value *storage, struct ly_err_item **err)
168+
{
169+
LY_ERR ret;
170+
struct lysc_type_dec *type_dec = (struct lysc_type_dec *)type;
171+
int64_t num;
172+
173+
LY_CHECK_ARG_RET(NULL, type, storage, err, LY_EINVAL);
174+
*err = NULL;
175+
num = storage->dec64;
176+
177+
if (type_dec->range) {
178+
/* check range of the number */
179+
ret = lyplg_type_validate_range(type->basetype, type_dec->range, num, storage->_canonical,
180+
strlen(storage->_canonical), err);
181+
LY_CHECK_RET(ret);
182+
}
183+
184+
return LY_SUCCESS;
185+
}
186+
164187
LIBYANG_API_DEF LY_ERR
165188
lyplg_type_compare_decimal64(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
166189
const struct lyd_value *val2)
@@ -239,7 +262,7 @@ const struct lyplg_type_record plugins_decimal64[] = {
239262

240263
.plugin.id = "libyang 2 - decimal64, version 1",
241264
.plugin.store = lyplg_type_store_decimal64,
242-
.plugin.validate = NULL,
265+
.plugin.validate = lyplg_type_validate_decimal64,
243266
.plugin.compare = lyplg_type_compare_decimal64,
244267
.plugin.sort = lyplg_type_sort_decimal64,
245268
.plugin.print = lyplg_type_print_decimal64,

src/plugins_types/hex_string.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ lyplg_type_store_hex_string(const struct ly_ctx *ctx, const struct lysc_type *ty
5555
ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
5656
LY_CHECK_GOTO(ret, cleanup);
5757

58+
/* length restriction of the string */
59+
if (type_str->length) {
60+
/* value_len is in bytes, but we need number of characters here */
61+
ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
62+
LY_CHECK_GOTO(ret, cleanup);
63+
}
64+
65+
/* pattern restrictions */
66+
ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
67+
LY_CHECK_GOTO(ret, cleanup);
68+
5869
/* make a copy, it is needed for canonization */
5970
if ((format != LY_VALUE_CANON) && !(options & LYPLG_TYPE_STORE_DYNAMIC)) {
6071
value = strndup(value, value_len);
@@ -81,19 +92,6 @@ lyplg_type_store_hex_string(const struct ly_ctx *ctx, const struct lysc_type *ty
8192
LY_CHECK_GOTO(ret, cleanup);
8293
}
8394

84-
if (!(options & LYPLG_TYPE_STORE_ONLY)) {
85-
/* validate length restriction of the string */
86-
if (type_str->length) {
87-
/* value_len is in bytes, but we need number of characters here */
88-
ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_len), value, value_len, err);
89-
LY_CHECK_GOTO(ret, cleanup);
90-
}
91-
92-
/* validate pattern restrictions */
93-
ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
94-
LY_CHECK_GOTO(ret, cleanup);
95-
}
96-
9795
cleanup:
9896
if (options & LYPLG_TYPE_STORE_DYNAMIC) {
9997
free((void *)value);

0 commit comments

Comments
 (0)