]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core/transaction: make merge_unit_ids() always return NUL-terminated string
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 18 Mar 2023 03:12:01 +0000 (12:12 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 18 Mar 2023 03:12:05 +0000 (12:12 +0900)
Follow-up for 924775e8ce49817f96df19c2b06356c12ecfc754.

The loop run with `STRV_FOREACH_PAIR()`, hence `if (*(unit_id+1))` is
not a good way to detect if there exist a next entry.

Fixes #26872.

src/core/transaction.c

index a6fd184e33e78d1b7a0207422dc8e91309faf626..1f420fe176fa28b369dd3adb8addc1c9b9003a5f 100644 (file)
@@ -323,22 +323,25 @@ _pure_ static bool unit_matters_to_anchor(Unit *u, Job *job) {
         return false;
 }
 
-static char* merge_unit_ids(const char* unit_log_field, char **pairs) {
-        char *ans = NULL;
-        size_t size = 0, next;
+static char* merge_unit_ids(const char* unit_log_field, char * const* pairs) {
+        _cleanup_free_ char *ans = NULL;
+        size_t size = 0;
 
         STRV_FOREACH_PAIR(unit_id, job_type, pairs) {
+                size_t next;
+
+                if (size > 0)
+                        ans[size - 1] = '\n';
+
                 next = strlen(unit_log_field) + strlen(*unit_id);
                 if (!GREEDY_REALLOC(ans, size + next + 1))
-                        return mfree(ans);
+                        return NULL;
 
                 sprintf(ans + size, "%s%s", unit_log_field, *unit_id);
-                if (*(unit_id+1))
-                        ans[size + next] =  '\n';
                 size += next + 1;
         }
 
-        return ans;
+        return TAKE_PTR(ans);
 }
 
 static int transaction_verify_order_one(Transaction *tr, Job *j, Job *from, unsigned generation, sd_bus_error *e) {