From 5c8208c6972fb0e5880836b7957cf7dd63ac0897 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sun, 30 Sep 2018 18:36:51 +0100 Subject: [PATCH] [Minor] Backport fix from libucl --- contrib/libucl/ucl_schema.c | 47 ++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/contrib/libucl/ucl_schema.c b/contrib/libucl/ucl_schema.c index 4d7c871ff..d11d66909 100644 --- a/contrib/libucl/ucl_schema.c +++ b/contrib/libucl/ucl_schema.c @@ -49,7 +49,16 @@ static bool ucl_schema_validate (const ucl_object_t *schema, /* * Create validation error */ -static void + +#ifdef __GNUC__ +static inline void +ucl_schema_create_error (struct ucl_schema_error *err, + enum ucl_schema_error_code code, const ucl_object_t *obj, + const char *fmt, ...) +__attribute__ (( format( printf, 4, 5) )); +#endif + +static inline void ucl_schema_create_error (struct ucl_schema_error *err, enum ucl_schema_error_code code, const ucl_object_t *obj, const char *fmt, ...) @@ -69,7 +78,7 @@ ucl_schema_create_error (struct ucl_schema_error *err, * Check whether we have a pattern specified */ static const ucl_object_t * -ucl_schema_test_pattern (const ucl_object_t *obj, const char *pattern) +ucl_schema_test_pattern (const ucl_object_t *obj, const char *pattern, bool recursive) { const ucl_object_t *res = NULL; #ifdef HAVE_REGEX_H @@ -78,11 +87,16 @@ ucl_schema_test_pattern (const ucl_object_t *obj, const char *pattern) ucl_object_iter_t iter = NULL; if (regcomp (®, pattern, REG_EXTENDED | REG_NOSUB) == 0) { - while ((elt = ucl_object_iterate (obj, &iter, true)) != NULL) { - if (regexec (®, ucl_object_key (elt), 0, NULL, 0) == 0) { - res = elt; - break; + if (recursive) { + while ((elt = ucl_object_iterate (obj, &iter, true)) != NULL) { + if (regexec (®, ucl_object_key (elt), 0, NULL, 0) == 0) { + res = elt; + break; + } } + } else { + if (regexec (®, ucl_object_key (obj), 0, NULL, 0) == 0) + res = obj; } regfree (®); } @@ -205,12 +219,17 @@ ucl_schema_validate_object (const ucl_object_t *schema, } } else if (strcmp (ucl_object_key (elt), "patternProperties") == 0) { + const ucl_object_t *vobj; + ucl_object_iter_t viter; piter = NULL; while (ret && (prop = ucl_object_iterate (elt, &piter, true)) != NULL) { - found = ucl_schema_test_pattern (obj, ucl_object_key (prop)); - if (found) { - ret = ucl_schema_validate (prop, found, true, err, root, - ext_ref); + viter = NULL; + while (ret && (vobj = ucl_object_iterate (obj, &viter, true)) != NULL) { + found = ucl_schema_test_pattern (vobj, ucl_object_key (prop), false); + if (found) { + ret = ucl_schema_validate (prop, found, true, err, root, + ext_ref); + } } } } @@ -234,7 +253,7 @@ ucl_schema_validate_object (const ucl_object_t *schema, piter = NULL; pat = ucl_object_lookup (schema, "patternProperties"); while ((pelt = ucl_object_iterate (pat, &piter, true)) != NULL) { - found = ucl_schema_test_pattern (obj, ucl_object_key (pelt)); + found = ucl_schema_test_pattern (obj, ucl_object_key (pelt), true); if (found != NULL) { break; } @@ -301,7 +320,7 @@ ucl_schema_validate_number (const ucl_object_t *schema, if (fabs (remainder (val, constraint)) > alpha) { ucl_schema_create_error (err, UCL_SCHEMA_CONSTRAINT, obj, "number %.4f is not multiple of %.4f, remainder is %.7f", - val, constraint); + val, constraint, remainder (val, constraint)); ret = false; break; } @@ -361,7 +380,7 @@ ucl_schema_validate_string (const ucl_object_t *schema, constraint = ucl_object_toint (elt); if (obj->len > constraint) { ucl_schema_create_error (err, UCL_SCHEMA_CONSTRAINT, obj, - "string is too big: %.3f, maximum is: %.3f", + "string is too big: %u, maximum is: %" PRId64, obj->len, constraint); ret = false; break; @@ -372,7 +391,7 @@ ucl_schema_validate_string (const ucl_object_t *schema, constraint = ucl_object_toint (elt); if (obj->len < constraint) { ucl_schema_create_error (err, UCL_SCHEMA_CONSTRAINT, obj, - "string is too short: %.3f, minimum is: %.3f", + "string is too short: %u, minimum is: %" PRId64, obj->len, constraint); ret = false; break; -- 2.39.5