Skip to content

Commit e9ca6ed

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 eca41ba commit e9ca6ed

File tree

9 files changed

+155
-82
lines changed

9 files changed

+155
-82
lines changed

src/plugins_types/binary.c

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@
4343
*/
4444
static const char b64_etable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
4545

46+
4647
static void lyplg_type_free_binary(const struct ly_ctx *ctx, struct lyd_value *value);
48+
static LY_ERR lyplg_type_validate_binary(const struct ly_ctx *ctx, const struct lysc_type *type,
49+
const struct lyd_node *ctx_node, const struct lyd_node *tree, struct lyd_value *storage, struct ly_err_item **err);
4750

4851
/**
4952
* @brief Encode binary value into a base64 string value.
@@ -328,11 +331,9 @@ lyplg_type_store_binary(const struct ly_ctx *ctx, const struct lysc_type *type,
328331
}
329332

330333
if (!(options & LYPLG_TYPE_STORE_ONLY)) {
331-
/* validate length restriction of the binary value */
332-
if (type_bin->length) {
333-
ret = lyplg_type_validate_range(LY_TYPE_BINARY, type_bin->length, val->size, value, value_size, err);
334-
LY_CHECK_GOTO(ret, cleanup);
335-
}
334+
/* validate value */
335+
ret = lyplg_type_validate_binary(ctx, type, NULL, NULL, storage, err);
336+
LY_CHECK_GOTO(ret, cleanup);
336337
}
337338

338339
cleanup:
@@ -346,6 +347,34 @@ lyplg_type_store_binary(const struct ly_ctx *ctx, const struct lysc_type *type,
346347
return ret;
347348
}
348349

350+
static LY_ERR
351+
/**
352+
* @brief Implementation of ::lyplg_type_validate_clb for the binary type.
353+
*/
354+
static LY_ERR
355+
lyplg_type_validate_binary(const struct ly_ctx *ctx, const struct lysc_type *type, const struct lyd_node *UNUSED(ctx_node),
356+
const struct lyd_node *UNUSED(tree), struct lyd_value *storage, struct ly_err_item **err)
357+
{
358+
struct lysc_type_bin *type_bin = (struct lysc_type_bin *)type;
359+
struct lyd_value_binary *val;
360+
const void *value;
361+
size_t value_len;
362+
363+
LY_CHECK_ARG_RET(NULL, type, storage, err, LY_EINVAL);
364+
365+
val = LYPLG_TYPE_VAL_IS_DYN(val) ? (struct lyd_value_binary *)(storage->dyn_mem) : (struct lyd_value_binary *)(storage->fixed_mem);
366+
value = lyd_value_get_canonical(ctx, storage);
367+
value_len = strlen(value);
368+
*err = NULL;
369+
370+
/* length restriction of the binary value */
371+
if (type_bin->length) {
372+
LY_CHECK_RET(lyplg_type_validate_range(LY_TYPE_BINARY, type_bin->length, val->size, value, value_len, err));
373+
}
374+
375+
return LY_SUCCESS;
376+
}
377+
349378
static LY_ERR
350379
lyplg_type_compare_binary(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
351380
{

src/plugins_types/decimal64.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,32 @@ lyplg_type_store_decimal64(const struct ly_ctx *ctx, const struct lysc_type *typ
166166
return ret;
167167
}
168168

169+
static LY_ERR
170+
/**
171+
* @brief Implementation of ::lyplg_type_validate_clb for the built-in decimal64 type.
172+
*/
173+
static LY_ERR
174+
lyplg_type_validate_decimal64(const struct ly_ctx *UNUSED(ctx), const struct lysc_type *type, const struct lyd_node *UNUSED(ctx_node),
175+
const struct lyd_node *UNUSED(tree), struct lyd_value *storage, struct ly_err_item **err)
176+
{
177+
LY_ERR ret;
178+
struct lysc_type_dec *type_dec = (struct lysc_type_dec *)type;
179+
int64_t num;
180+
181+
LY_CHECK_ARG_RET(NULL, type, storage, err, LY_EINVAL);
182+
*err = NULL;
183+
num = storage->dec64;
184+
185+
if (type_dec->range) {
186+
/* check range of the number */
187+
ret = lyplg_type_validate_range(type->basetype, type_dec->range, num, storage->_canonical,
188+
strlen(storage->_canonical), err);
189+
LY_CHECK_RET(ret);
190+
}
191+
192+
return LY_SUCCESS;
193+
}
194+
169195
static LY_ERR
170196
lyplg_type_compare_decimal64(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
171197
const struct lyd_value *val2)

src/plugins_types/integer.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
* | variable | yes | pointer to the specific integer type | little-endian integer value |
4040
*/
4141

42+
static LY_ERR lyplg_type_validate_int(const struct ly_ctx *ctx, const struct lysc_type *type,
43+
const struct lyd_node *ctx_node, const struct lyd_node *tree, struct lyd_value *storage, struct ly_err_item **err);
44+
static LY_ERR lyplg_type_validate_uint(const struct ly_ctx *ctx, const struct lysc_type *type,
45+
const struct lyd_node *ctx_node, const struct lyd_node *tree, struct lyd_value *storage, struct ly_err_item **err);
46+
4247
static LY_ERR
4348
lyplg_type_store_int(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint32_t value_size_bits,
4449
uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
@@ -165,6 +170,48 @@ lyplg_type_store_int(const struct ly_ctx *ctx, const struct lysc_type *type, con
165170
return ret;
166171
}
167172

173+
/**
174+
* @brief Implementation of ::lyplg_type_validate_clb for the signed interger types.
175+
*/
176+
static LY_ERR
177+
lyplg_type_validate_int(const struct ly_ctx *UNUSED(ctx), const struct lysc_type *type, const struct lyd_node *UNUSED(ctx_node),
178+
const struct lyd_node *UNUSED(tree), struct lyd_value *storage, struct ly_err_item **err)
179+
{
180+
LY_ERR ret;
181+
struct lysc_type_num *type_num = (struct lysc_type_num *)type;
182+
int64_t num;
183+
184+
LY_CHECK_ARG_RET(NULL, type, storage, err, LY_EINVAL);
185+
*err = NULL;
186+
187+
/* set the value (matters for big-endian) and get the correct int64 number */
188+
switch (type->basetype) {
189+
case LY_TYPE_INT8:
190+
num = storage->int8;
191+
break;
192+
case LY_TYPE_INT16:
193+
num = storage->int16;
194+
break;
195+
case LY_TYPE_INT32:
196+
num = storage->int32;
197+
break;
198+
case LY_TYPE_INT64:
199+
num = storage->int64;
200+
break;
201+
default:
202+
return LY_EINVAL;
203+
}
204+
205+
/* validate range of the number */
206+
if (type_num->range) {
207+
ret = lyplg_type_validate_range(type->basetype, type_num->range, num, storage->_canonical,
208+
strlen(storage->_canonical), err);
209+
LY_CHECK_RET(ret);
210+
}
211+
212+
return LY_SUCCESS;
213+
}
214+
168215
static LY_ERR
169216
lyplg_type_compare_int(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
170217
{
@@ -409,6 +456,48 @@ lyplg_type_store_uint(const struct ly_ctx *ctx, const struct lysc_type *type, co
409456
return ret;
410457
}
411458

459+
/**
460+
* @brief Implementation of ::lyplg_type_validate_clb for the unsigned interger types.
461+
*/
462+
static LY_ERR
463+
lyplg_type_validate_uint(const struct ly_ctx *UNUSED(ctx), const struct lysc_type *type, const struct lyd_node *UNUSED(ctx_node),
464+
const struct lyd_node *UNUSED(tree), struct lyd_value *storage, struct ly_err_item **err)
465+
{
466+
LY_ERR ret;
467+
struct lysc_type_num *type_num = (struct lysc_type_num *)type;
468+
uint64_t num;
469+
470+
LY_CHECK_ARG_RET(NULL, type, storage, err, LY_EINVAL);
471+
*err = NULL;
472+
473+
/* set the value (matters for big-endian) and get the correct int64 number */
474+
switch (type->basetype) {
475+
case LY_TYPE_UINT8:
476+
num = storage->uint8;
477+
break;
478+
case LY_TYPE_UINT16:
479+
num = storage->uint16;
480+
break;
481+
case LY_TYPE_UINT32:
482+
num = storage->uint32;
483+
break;
484+
case LY_TYPE_UINT64:
485+
num = storage->uint64;
486+
break;
487+
default:
488+
return LY_EINVAL;
489+
}
490+
491+
/* validate range of the number */
492+
if (type_num->range) {
493+
ret = lyplg_type_validate_range(type->basetype, type_num->range, num, storage->_canonical,
494+
strlen(storage->_canonical), err);
495+
LY_CHECK_RET(ret);
496+
}
497+
498+
return LY_SUCCESS;
499+
}
500+
412501
static LY_ERR
413502
lyplg_type_compare_uint(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
414503
{

src/plugins_types/ipv4_address.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -173,24 +173,6 @@ lyplg_type_store_ipv4_address(const struct ly_ctx *ctx, const struct lysc_type *
173173
ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
174174
LY_CHECK_GOTO(ret, cleanup);
175175

176-
if (!(options & LYPLG_TYPE_STORE_ONLY)) {
177-
/* length restriction of the string */
178-
if (type_str->length) {
179-
/* value_size is in bytes, but we need number of characters here */
180-
ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_size), value,
181-
value_size, err);
182-
LY_CHECK_GOTO(ret, cleanup);
183-
}
184-
185-
/* pattern restrictions */
186-
ret = lyplg_type_validate_patterns(ctx, type_str->patterns, value, value_size, err);
187-
LY_CHECK_GOTO(ret, cleanup);
188-
}
189-
190-
/* pattern restrictions */
191-
ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
192-
LY_CHECK_GOTO(ret, cleanup);
193-
194176
/* get the network-byte order address */
195177
ret = ipv4address_str2ip(value, value_size, options, ctx, &val->addr, &val->zone, err);
196178
LY_CHECK_GOTO(ret, cleanup);

src/plugins_types/ipv4_prefix.c

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,19 +165,9 @@ lyplg_type_store_ipv4_prefix(const struct ly_ctx *ctx, const struct lysc_type *t
165165
ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
166166
LY_CHECK_GOTO(ret, cleanup);
167167

168-
if (!(options & LYPLG_TYPE_STORE_ONLY)) {
169-
/* length restriction of the string */
170-
if (type_str->length) {
171-
/* value_size is in bytes, but we need number of characters here */
172-
ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_size), value,
173-
value_size, err);
174-
LY_CHECK_GOTO(ret, cleanup);
175-
}
176-
177-
/* pattern restrictions */
178-
ret = lyplg_type_validate_patterns(ctx, type_str->patterns, value, value_size, err);
179-
LY_CHECK_GOTO(ret, cleanup);
180-
}
168+
/* pattern restrictions */
169+
ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
170+
LY_CHECK_GOTO(ret, cleanup);
181171

182172
/* pattern restrictions */
183173
ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);

src/plugins_types/ipv6_address.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -174,24 +174,6 @@ lyplg_type_store_ipv6_address(const struct ly_ctx *ctx, const struct lysc_type *
174174
ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
175175
LY_CHECK_GOTO(ret, cleanup);
176176

177-
if (!(options & LYPLG_TYPE_STORE_ONLY)) {
178-
/* length restriction of the string */
179-
if (type_str->length) {
180-
/* value_size is in bytes, but we need number of characters here */
181-
ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_size), value,
182-
value_size, err);
183-
LY_CHECK_GOTO(ret, cleanup);
184-
}
185-
186-
/* pattern restrictions */
187-
ret = lyplg_type_validate_patterns(ctx, type_str->patterns, value, value_size, err);
188-
LY_CHECK_GOTO(ret, cleanup);
189-
}
190-
191-
/* pattern restrictions */
192-
ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
193-
LY_CHECK_GOTO(ret, cleanup);
194-
195177
/* get the network-byte order address */
196178
ret = ipv6address_str2ip(value, value_size, options, ctx, &val->addr, &val->zone, err);
197179
LY_CHECK_GOTO(ret, cleanup);

src/plugins_types/ipv6_prefix.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -179,24 +179,6 @@ lyplg_type_store_ipv6_prefix(const struct ly_ctx *ctx, const struct lysc_type *t
179179
ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
180180
LY_CHECK_GOTO(ret, cleanup);
181181

182-
if (!(options & LYPLG_TYPE_STORE_ONLY)) {
183-
/* length restriction of the string */
184-
if (type_str->length) {
185-
/* value_size is in bytes, but we need number of characters here */
186-
ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_size), value,
187-
value_size, err);
188-
LY_CHECK_GOTO(ret, cleanup);
189-
}
190-
191-
/* pattern restrictions */
192-
ret = lyplg_type_validate_patterns(ctx, type_str->patterns, value, value_size, err);
193-
LY_CHECK_GOTO(ret, cleanup);
194-
}
195-
196-
/* pattern restrictions */
197-
ret = lyplg_type_validate_patterns(type_str->patterns, value, value_len, err);
198-
LY_CHECK_GOTO(ret, cleanup);
199-
200182
/* get the mask in network-byte order */
201183
ret = ipv6prefix_str2ip(value, value_size, &val->addr, &val->prefix, err);
202184
LY_CHECK_GOTO(ret, cleanup);

src/plugins_types/string.c

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,8 @@ lyplg_type_store_string(const struct ly_ctx *ctx, const struct lysc_type *type,
101101
}
102102

103103
if (!(options & LYPLG_TYPE_STORE_ONLY)) {
104-
/* length restriction of the string */
105-
if (type_str->length) {
106-
/* value_size is in bytes, but we need number of characters here */
107-
ret = lyplg_type_validate_range(LY_TYPE_STRING, type_str->length, ly_utf8len(value, value_size), value,
108-
value_size, err);
109-
LY_CHECK_GOTO(ret, cleanup);
110-
}
111-
112-
/* pattern restrictions */
113-
ret = lyplg_type_validate_patterns(ctx, type_str->patterns, value, value_size, err);
104+
/* validate value */
105+
ret = lyplg_type_validate_string(ctx, type, NULL, NULL, storage, err);
114106
LY_CHECK_GOTO(ret, cleanup);
115107
}
116108

src/plugins_types/xpath1.0.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ lyplg_type_store_xpath10(const struct ly_ctx *ctx, const struct lysc_type *type,
250250
{
251251
LY_ERR ret = LY_SUCCESS;
252252
uint32_t value_size;
253+
struct lysc_type_str *type_str = (struct lysc_type_str *)type;
253254
struct lyd_value_xpath10 *val;
254255
char *canon;
255256

0 commit comments

Comments
 (0)