]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Backport fix from libucl
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sun, 30 Sep 2018 17:36:51 +0000 (18:36 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sun, 30 Sep 2018 17:36:51 +0000 (18:36 +0100)
contrib/libucl/ucl_schema.c

index 4d7c871ff9f8f150a215fd0573bdd2e8a9ece8ca..d11d6690975219d06fa4740252165d813e8a66c7 100644 (file)
@@ -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 (&reg, pattern, REG_EXTENDED | REG_NOSUB) == 0) {
-               while ((elt = ucl_object_iterate (obj, &iter, true)) != NULL) {
-                       if (regexec (&reg, ucl_object_key (elt), 0, NULL, 0) == 0) {
-                               res = elt;
-                               break;
+               if (recursive) {
+                       while ((elt = ucl_object_iterate (obj, &iter, true)) != NULL) {
+                               if (regexec (&reg, ucl_object_key (elt), 0, NULL, 0) == 0) {
+                                       res = elt;
+                                       break;
+                               }
                        }
+               } else {
+                       if (regexec (&reg, ucl_object_key (obj), 0, NULL, 0) == 0)
+                               res = obj;
                }
                regfree (&reg);
        }
@@ -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;