From 1ece85e7d4af7517aa9cfe12b21cde1d82fa3167 Mon Sep 17 00:00:00 2001
From: Yu Watanabe <watanabe.yu+github@gmail.com>
Date: Sat, 18 Mar 2023 12:12:01 +0900
Subject: [PATCH] core/transaction: make merge_unit_ids() always return
 NUL-terminated string

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.

(cherry picked from commit 366eced4c81a15a25b9225347fa203aa67798b02)
(cherry picked from commit 7002c5c210a7ae3607bd8a424112e9f8789bc5f9)
(cherry picked from commit 89f780969b4436cf766eb9c0d6412362fc85f042)

Conflict:keep variable unit_id and job_type to solve compilation failure
Reference:https://github.com/systemd/systemd-stable/commit/1ece85e7d4af7517aa9cfe12b21cde1d82fa3167
---
 src/core/transaction.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/src/core/transaction.c b/src/core/transaction.c
index ebe5f19..9532531 100644
--- a/src/core/transaction.c
+++ b/src/core/transaction.c
@@ -328,22 +328,26 @@ _pure_ static bool unit_matters_to_anchor(Unit *u, Job *j) {
         return false;
 }
 
-static char* merge_unit_ids(const char* unit_log_field, char **pairs) {
-        char **unit_id, **job_type, *ans = NULL;
-        size_t size = 0, next;
+static char* merge_unit_ids(const char* unit_log_field, char * const* pairs) {
+        char **unit_id, **job_type = NULL;
+        _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) {
-- 
2.33.0

