From 366eced4c81a15a25b9225347fa203aa67798b02 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 18 Mar 2023 12:12:01 +0900 Subject: 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. --- src/core/transaction.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/core/transaction.c b/src/core/transaction.c index a6fd184e33..1f420fe176 100644 --- a/src/core/transaction.c +++ b/src/core/transaction.c @@ -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) { -- cgit v1.2.1 From 999f16514367224cbc50cb3ccc1e4392e43f6811 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 18 Mar 2023 12:17:54 +0900 Subject: core/transaction: make merge_unit_ids() return non-NULL on success --- src/core/transaction.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/core/transaction.c b/src/core/transaction.c index 1f420fe176..3cfa974fda 100644 --- a/src/core/transaction.c +++ b/src/core/transaction.c @@ -341,6 +341,9 @@ static char* merge_unit_ids(const char* unit_log_field, char * const* pairs) { size += next + 1; } + if (!ans) + return strdup(""); + return TAKE_PTR(ans); } -- cgit v1.2.1 From 5803c24da5cf543a55c4fce9009a9c5f2b18519a Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 18 Mar 2023 12:15:10 +0900 Subject: core/transaction: do not log "(null)" As we ignores the failure in merge_unit_ids(), so unit_ids may be NULL. --- src/core/transaction.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/transaction.c b/src/core/transaction.c index 3cfa974fda..dcb448a4b6 100644 --- a/src/core/transaction.c +++ b/src/core/transaction.c @@ -400,7 +400,7 @@ static int transaction_verify_order_one(Transaction *tr, Job *j, Job *from, unsi "Found %s on %s/%s", unit_id == array ? "ordering cycle" : "dependency", *unit_id, *job_type), - "%s", unit_ids); + "%s", strna(unit_ids)); if (delete) { const char *status; @@ -410,7 +410,7 @@ static int transaction_verify_order_one(Transaction *tr, Job *j, Job *from, unsi "Job %s/%s deleted to break ordering cycle starting with %s/%s", delete->unit->id, job_type_to_string(delete->type), j->unit->id, job_type_to_string(j->type)), - "%s", unit_ids); + "%s", strna(unit_ids)); if (log_get_show_color()) status = ANSI_HIGHLIGHT_RED " SKIP " ANSI_NORMAL; @@ -429,7 +429,7 @@ static int transaction_verify_order_one(Transaction *tr, Job *j, Job *from, unsi log_struct(LOG_ERR, LOG_UNIT_MESSAGE(j->unit, "Unable to break cycle starting with %s/%s", j->unit->id, job_type_to_string(j->type)), - "%s", unit_ids); + "%s", strna(unit_ids)); return sd_bus_error_setf(e, BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC, "Transaction order is cyclic. See system logs for details."); -- cgit v1.2.1