dcavalca / rpms / systemd

Forked from rpms/systemd 4 months ago
Clone
594167
From 272d6e85877bb436709ed54c02d3b68101e0438d Mon Sep 17 00:00:00 2001
594167
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
594167
Date: Mon, 9 May 2022 11:01:32 +0200
594167
Subject: [PATCH] shared/json: reduce scope of variables
594167
594167
(cherry picked from commit a4669764f7329d1e8a3d364db519500355cea5f0)
594167
Related: #2087652
594167
---
594167
 src/shared/json.c | 133 +++++++++++++++++++---------------------------
594167
 1 file changed, 54 insertions(+), 79 deletions(-)
594167
594167
diff --git a/src/shared/json.c b/src/shared/json.c
594167
index 6375b87a0b..d35874f8e3 100644
594167
--- a/src/shared/json.c
594167
+++ b/src/shared/json.c
594167
@@ -574,9 +574,6 @@ int json_variant_new_array(JsonVariant **ret, JsonVariant **array, size_t n) {
594167
 }
594167
 
594167
 int json_variant_new_array_bytes(JsonVariant **ret, const void *p, size_t n) {
594167
-        JsonVariant *v;
594167
-        size_t i;
594167
-
594167
         assert_return(ret, -EINVAL);
594167
         if (n == 0) {
594167
                 *ret = JSON_VARIANT_MAGIC_EMPTY_ARRAY;
594167
@@ -584,7 +581,7 @@ int json_variant_new_array_bytes(JsonVariant **ret, const void *p, size_t n) {
594167
         }
594167
         assert_return(p, -EINVAL);
594167
 
594167
-        v = new(JsonVariant, n + 1);
594167
+        JsonVariant *v = new(JsonVariant, n + 1);
594167
         if (!v)
594167
                 return -ENOMEM;
594167
 
594167
@@ -595,7 +592,7 @@ int json_variant_new_array_bytes(JsonVariant **ret, const void *p, size_t n) {
594167
                 .depth = 1,
594167
         };
594167
 
594167
-        for (i = 0; i < n; i++) {
594167
+        for (size_t i = 0; i < n; i++) {
594167
                 JsonVariant *w = v + 1 + i;
594167
 
594167
                 *w = (JsonVariant) {
594167
@@ -790,12 +787,9 @@ static void json_variant_free_inner(JsonVariant *v, bool force_sensitive) {
594167
                 return;
594167
         }
594167
 
594167
-        if (IN_SET(v->type, JSON_VARIANT_ARRAY, JSON_VARIANT_OBJECT)) {
594167
-                size_t i;
594167
-
594167
-                for (i = 0; i < v->n_elements; i++)
594167
+        if (IN_SET(v->type, JSON_VARIANT_ARRAY, JSON_VARIANT_OBJECT))
594167
+                for (size_t i = 0; i < v->n_elements; i++)
594167
                         json_variant_free_inner(v + 1 + i, sensitive);
594167
-        }
594167
 
594167
         if (sensitive)
594167
                 explicit_bzero_safe(v, json_variant_size(v));
594167
@@ -839,11 +833,9 @@ JsonVariant *json_variant_unref(JsonVariant *v) {
594167
 }
594167
 
594167
 void json_variant_unref_many(JsonVariant **array, size_t n) {
594167
-        size_t i;
594167
-
594167
         assert(array || n == 0);
594167
 
594167
-        for (i = 0; i < n; i++)
594167
+        for (size_t i = 0; i < n; i++)
594167
                 json_variant_unref(array[i]);
594167
 }
594167
 
594167
@@ -1218,8 +1210,6 @@ mismatch:
594167
 }
594167
 
594167
 JsonVariant *json_variant_by_key_full(JsonVariant *v, const char *key, JsonVariant **ret_key) {
594167
-        size_t i;
594167
-
594167
         if (!v)
594167
                 goto not_found;
594167
         if (!key)
594167
@@ -1241,6 +1231,7 @@ JsonVariant *json_variant_by_key_full(JsonVariant *v, const char *key, JsonVaria
594167
                 while (b > a) {
594167
                         JsonVariant *p;
594167
                         const char *f;
594167
+                        size_t i;
594167
                         int c;
594167
 
594167
                         i = (a + b) / 2;
594167
@@ -1264,7 +1255,7 @@ JsonVariant *json_variant_by_key_full(JsonVariant *v, const char *key, JsonVaria
594167
         }
594167
 
594167
         /* The variant is not sorted, hence search for the field linearly */
594167
-        for (i = 0; i < v->n_elements; i += 2) {
594167
+        for (size_t i = 0; i < v->n_elements; i += 2) {
594167
                 JsonVariant *p;
594167
 
594167
                 p = json_variant_dereference(v + 1 + i);
594167
@@ -1335,34 +1326,28 @@ bool json_variant_equal(JsonVariant *a, JsonVariant *b) {
594167
                 return true;
594167
 
594167
         case JSON_VARIANT_ARRAY: {
594167
-                size_t i, n;
594167
-
594167
-                n = json_variant_elements(a);
594167
+                size_t n = json_variant_elements(a);
594167
                 if (n != json_variant_elements(b))
594167
                         return false;
594167
 
594167
-                for (i = 0; i < n; i++) {
594167
+                for (size_t i = 0; i < n; i++)
594167
                         if (!json_variant_equal(json_variant_by_index(a, i), json_variant_by_index(b, i)))
594167
                                 return false;
594167
-                }
594167
 
594167
                 return true;
594167
         }
594167
 
594167
         case JSON_VARIANT_OBJECT: {
594167
-                size_t i, n;
594167
-
594167
-                n = json_variant_elements(a);
594167
+                size_t n = json_variant_elements(a);
594167
                 if (n != json_variant_elements(b))
594167
                         return false;
594167
 
594167
                 /* Iterate through all keys in 'a' */
594167
-                for (i = 0; i < n; i += 2) {
594167
+                for (size_t i = 0; i < n; i += 2) {
594167
                         bool found = false;
594167
-                        size_t j;
594167
 
594167
                         /* Match them against all keys in 'b' */
594167
-                        for (j = 0; j < n; j += 2) {
594167
+                        for (size_t j = 0; j < n; j += 2) {
594167
                                 JsonVariant *key_b;
594167
 
594167
                                 key_b = json_variant_by_index(b, j);
594167
@@ -1470,16 +1455,14 @@ static int print_source(FILE *f, JsonVariant *v, JsonFormatFlags flags, bool whi
594167
                 DECIMAL_STR_MAX(unsigned) -1;
594167
 
594167
         if (whitespace) {
594167
-                size_t i, n;
594167
-
594167
-                n = 1 + (v->source ? strlen(v->source->name) : 0) +
594167
-                        ((v->source && (v->line > 0 || v->column > 0)) ? 1 : 0) +
594167
-                        (v->line > 0 ? w : 0) +
594167
-                        (((v->source || v->line > 0) && v->column > 0) ? 1 : 0) +
594167
-                        (v->column > 0 ? k : 0) +
594167
-                        2;
594167
-
594167
-                for (i = 0; i < n; i++)
594167
+                size_t n = 1 + (v->source ? strlen(v->source->name) : 0) +
594167
+                               ((v->source && (v->line > 0 || v->column > 0)) ? 1 : 0) +
594167
+                               (v->line > 0 ? w : 0) +
594167
+                               (((v->source || v->line > 0) && v->column > 0) ? 1 : 0) +
594167
+                               (v->column > 0 ? k : 0) +
594167
+                               2;
594167
+
594167
+                for (size_t i = 0; i < n; i++)
594167
                         fputc(' ', f);
594167
         } else {
594167
                 fputc('[', f);
594167
@@ -1631,10 +1614,7 @@ static int json_format(FILE *f, JsonVariant *v, JsonFormatFlags flags, const cha
594167
                 break;
594167
 
594167
         case JSON_VARIANT_ARRAY: {
594167
-                size_t i, n;
594167
-
594167
-                n = json_variant_elements(v);
594167
-
594167
+                size_t n = json_variant_elements(v);
594167
                 if (n == 0)
594167
                         fputs("[]", f);
594167
                 else {
594167
@@ -1653,7 +1633,7 @@ static int json_format(FILE *f, JsonVariant *v, JsonFormatFlags flags, const cha
594167
                                 fputc('[', f);
594167
                         }
594167
 
594167
-                        for (i = 0; i < n; i++) {
594167
+                        for (size_t i = 0; i < n; i++) {
594167
                                 JsonVariant *e;
594167
 
594167
                                 assert_se(e = json_variant_by_index(v, i));
594167
@@ -1687,10 +1667,7 @@ static int json_format(FILE *f, JsonVariant *v, JsonFormatFlags flags, const cha
594167
         }
594167
 
594167
         case JSON_VARIANT_OBJECT: {
594167
-                size_t i, n;
594167
-
594167
-                n = json_variant_elements(v);
594167
-
594167
+                size_t n = json_variant_elements(v);
594167
                 if (n == 0)
594167
                         fputs("{}", f);
594167
                 else {
594167
@@ -1709,7 +1686,7 @@ static int json_format(FILE *f, JsonVariant *v, JsonFormatFlags flags, const cha
594167
                                 fputc('{', f);
594167
                         }
594167
 
594167
-                        for (i = 0; i < n; i += 2) {
594167
+                        for (size_t i = 0; i < n; i += 2) {
594167
                                 JsonVariant *e;
594167
 
594167
                                 e = json_variant_by_index(v, i);
594167
@@ -1826,7 +1803,7 @@ void json_variant_dump(JsonVariant *v, JsonFormatFlags flags, FILE *f, const cha
594167
 int json_variant_filter(JsonVariant **v, char **to_remove) {
594167
         _cleanup_(json_variant_unrefp) JsonVariant *w = NULL;
594167
         _cleanup_free_ JsonVariant **array = NULL;
594167
-        size_t i, n = 0, k = 0;
594167
+        size_t n = 0, k = 0;
594167
         int r;
594167
 
594167
         assert(v);
594167
@@ -1839,7 +1816,7 @@ int json_variant_filter(JsonVariant **v, char **to_remove) {
594167
         if (strv_isempty(to_remove))
594167
                 return 0;
594167
 
594167
-        for (i = 0; i < json_variant_elements(*v); i += 2) {
594167
+        for (size_t i = 0; i < json_variant_elements(*v); i += 2) {
594167
                 JsonVariant *p;
594167
 
594167
                 p = json_variant_by_index(*v, i);
594167
@@ -1881,7 +1858,7 @@ int json_variant_filter(JsonVariant **v, char **to_remove) {
594167
 int json_variant_set_field(JsonVariant **v, const char *field, JsonVariant *value) {
594167
         _cleanup_(json_variant_unrefp) JsonVariant *field_variant = NULL, *w = NULL;
594167
         _cleanup_free_ JsonVariant **array = NULL;
594167
-        size_t i, k = 0;
594167
+        size_t k = 0;
594167
         int r;
594167
 
594167
         assert(v);
594167
@@ -1896,7 +1873,7 @@ int json_variant_set_field(JsonVariant **v, const char *field, JsonVariant *valu
594167
                 if (!json_variant_is_object(*v))
594167
                         return -EINVAL;
594167
 
594167
-                for (i = 0; i < json_variant_elements(*v); i += 2) {
594167
+                for (size_t i = 0; i < json_variant_elements(*v); i += 2) {
594167
                         JsonVariant *p;
594167
 
594167
                         p = json_variant_by_index(*v, i);
594167
@@ -2007,7 +1984,7 @@ int json_variant_set_field_strv(JsonVariant **v, const char *field, char **l) {
594167
 int json_variant_merge(JsonVariant **v, JsonVariant *m) {
594167
         _cleanup_(json_variant_unrefp) JsonVariant *w = NULL;
594167
         _cleanup_free_ JsonVariant **array = NULL;
594167
-        size_t v_elements, m_elements, i, k;
594167
+        size_t v_elements, m_elements, k;
594167
         bool v_blank, m_blank;
594167
         int r;
594167
 
594167
@@ -2040,7 +2017,7 @@ int json_variant_merge(JsonVariant **v, JsonVariant *m) {
594167
                 return -ENOMEM;
594167
 
594167
         k = 0;
594167
-        for (i = 0; i < v_elements; i += 2) {
594167
+        for (size_t i = 0; i < v_elements; i += 2) {
594167
                 JsonVariant *u;
594167
 
594167
                 u = json_variant_by_index(*v, i);
594167
@@ -2054,7 +2031,7 @@ int json_variant_merge(JsonVariant **v, JsonVariant *m) {
594167
                 array[k++] = json_variant_by_index(*v, i + 1);
594167
         }
594167
 
594167
-        for (i = 0; i < m_elements; i++)
594167
+        for (size_t i = 0; i < m_elements; i++)
594167
                 array[k++] = json_variant_by_index(m, i);
594167
 
594167
         r = json_variant_new_object(&w, array, k);
594167
@@ -2089,19 +2066,17 @@ int json_variant_append_array(JsonVariant **v, JsonVariant *element) {
594167
         if (blank)
594167
                 r = json_variant_new_array(&nv, (JsonVariant*[]) { element }, 1);
594167
         else {
594167
-                _cleanup_free_ JsonVariant **array = NULL;
594167
-                size_t i;
594167
-
594167
-                array = new(JsonVariant*, json_variant_elements(*v) + 1);
594167
+                _cleanup_free_ JsonVariant **array = new(JsonVariant*, json_variant_elements(*v) + 1);
594167
                 if (!array)
594167
                         return -ENOMEM;
594167
 
594167
-                for (i = 0; i < json_variant_elements(*v); i++)
594167
+                size_t size = json_variant_elements(*v);
594167
+                for (size_t i = 0; i < size; i++)
594167
                         array[i] = json_variant_by_index(*v, i);
594167
 
594167
-                array[i] = element;
594167
+                array[size] = element;
594167
 
594167
-                r = json_variant_new_array(&nv, array, i + 1);
594167
+                r = json_variant_new_array(&nv, array, size + 1);
594167
         }
594167
         if (r < 0)
594167
                 return r;
594167
@@ -2116,7 +2091,6 @@ int json_variant_append_array(JsonVariant **v, JsonVariant *element) {
594167
 
594167
 int json_variant_strv(JsonVariant *v, char ***ret) {
594167
         char **l = NULL;
594167
-        size_t n, i;
594167
         bool sensitive;
594167
         int r;
594167
 
594167
@@ -2136,12 +2110,12 @@ int json_variant_strv(JsonVariant *v, char ***ret) {
594167
 
594167
         sensitive = v->sensitive;
594167
 
594167
-        n = json_variant_elements(v);
594167
+        size_t n = json_variant_elements(v);
594167
         l = new(char*, n+1);
594167
         if (!l)
594167
                 return -ENOMEM;
594167
 
594167
-        for (i = 0; i < n; i++) {
594167
+        for (size_t i = 0; i < n; i++) {
594167
                 JsonVariant *e;
594167
 
594167
                 assert_se(e = json_variant_by_index(v, i));
594167
@@ -2160,7 +2134,7 @@ int json_variant_strv(JsonVariant *v, char ***ret) {
594167
                 }
594167
         }
594167
 
594167
-        l[i] = NULL;
594167
+        l[n] = NULL;
594167
         *ret = TAKE_PTR(l);
594167
 
594167
         return 0;
594167
@@ -2847,7 +2821,7 @@ static int json_parse_internal(
594167
                 unsigned *column,
594167
                 bool continue_end) {
594167
 
594167
-        size_t n_stack = 1, i;
594167
+        size_t n_stack = 1;
594167
         unsigned line_buffer = 0, column_buffer = 0;
594167
         void *tokenizer_state = NULL;
594167
         JsonStack *stack = NULL;
594167
@@ -3186,7 +3160,7 @@ done:
594167
         r = 0;
594167
 
594167
 finish:
594167
-        for (i = 0; i < n_stack; i++)
594167
+        for (size_t i = 0; i < n_stack; i++)
594167
                 json_stack_release(stack + i);
594167
 
594167
         free(stack);
594167
@@ -3229,7 +3203,7 @@ int json_parse_file_at(FILE *f, int dir_fd, const char *path, JsonParseFlags fla
594167
 
594167
 int json_buildv(JsonVariant **ret, va_list ap) {
594167
         JsonStack *stack = NULL;
594167
-        size_t n_stack = 1, i;
594167
+        size_t n_stack = 1;
594167
         int r;
594167
 
594167
         assert_return(ret, -EINVAL);
594167
@@ -4147,7 +4121,7 @@ done:
594167
         r = 0;
594167
 
594167
 finish:
594167
-        for (i = 0; i < n_stack; i++)
594167
+        for (size_t i = 0; i < n_stack; i++)
594167
                 json_stack_release(stack + i);
594167
 
594167
         free(stack);
594167
@@ -4231,8 +4205,7 @@ int json_log_internal(
594167
 }
594167
 
594167
 int json_dispatch(JsonVariant *v, const JsonDispatch table[], JsonDispatchCallback bad, JsonDispatchFlags flags, void *userdata) {
594167
-        const JsonDispatch *p;
594167
-        size_t i, n, m;
594167
+        size_t m;
594167
         int r, done = 0;
594167
         bool *found;
594167
 
594167
@@ -4245,14 +4218,16 @@ int json_dispatch(JsonVariant *v, const JsonDispatch table[], JsonDispatchCallba
594167
                 return -EINVAL;
594167
         }
594167
 
594167
-        for (p = table, m = 0; p->name; p++)
594167
+        m = 0;
594167
+        for (const JsonDispatch *p = table; p->name; p++)
594167
                 m++;
594167
 
594167
         found = newa0(bool, m);
594167
 
594167
-        n = json_variant_elements(v);
594167
-        for (i = 0; i < n; i += 2) {
594167
+        size_t n = json_variant_elements(v);
594167
+        for (size_t i = 0; i < n; i += 2) {
594167
                 JsonVariant *key, *value;
594167
+                const JsonDispatch *p;
594167
 
594167
                 assert_se(key = json_variant_by_index(v, i));
594167
                 assert_se(value = json_variant_by_index(v, i+1));
594167
@@ -4326,7 +4301,7 @@ int json_dispatch(JsonVariant *v, const JsonDispatch table[], JsonDispatchCallba
594167
                 }
594167
         }
594167
 
594167
-        for (p = table; p->name; p++) {
594167
+        for (const JsonDispatch *p = table; p->name; p++) {
594167
                 JsonDispatchFlags merged_flags = p->flags | flags;
594167
 
594167
                 if ((merged_flags & JSON_MANDATORY) && !found[p-table]) {
594167
@@ -4621,7 +4596,7 @@ static int json_cmp_strings(const void *x, const void *y) {
594167
 int json_variant_sort(JsonVariant **v) {
594167
         _cleanup_free_ JsonVariant **a = NULL;
594167
         JsonVariant *n = NULL;
594167
-        size_t i, m;
594167
+        size_t m;
594167
         int r;
594167
 
594167
         assert(v);
594167
@@ -4639,7 +4614,7 @@ int json_variant_sort(JsonVariant **v) {
594167
         if (!a)
594167
                 return -ENOMEM;
594167
 
594167
-        for (i = 0; i < m; i++)
594167
+        for (size_t i = 0; i < m; i++)
594167
                 a[i] = json_variant_by_index(*v, i);
594167
 
594167
         qsort(a, m/2, sizeof(JsonVariant*)*2, json_cmp_strings);
594167
@@ -4662,7 +4637,7 @@ int json_variant_sort(JsonVariant **v) {
594167
 int json_variant_normalize(JsonVariant **v) {
594167
         _cleanup_free_ JsonVariant **a = NULL;
594167
         JsonVariant *n = NULL;
594167
-        size_t i, j, m;
594167
+        size_t i, m;
594167
         int r;
594167
 
594167
         assert(v);
594167
@@ -4714,7 +4689,7 @@ int json_variant_normalize(JsonVariant **v) {
594167
         r = 1;
594167
 
594167
 finish:
594167
-        for (j = 0; j < i; j++)
594167
+        for (size_t j = 0; j < i; j++)
594167
                 json_variant_unref(a[j]);
594167
 
594167
         return r;