From 4ba5536f71b99b8f4b460036800ce2f7e1287c1d Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 11 Oct 2022 16:39:51 +0200 Subject: [PATCH] logs-show: be more careful when initializing get_dual_timestamp() return parameters make sure we always have something to return in all return parameters, including in the boot id return parameter, in all code paths. Follow-up for: #24965 --- src/shared/logs-show.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c index 562fe0e78d8..d6294a543c8 100644 --- a/src/shared/logs-show.c +++ b/src/shared/logs-show.c @@ -1267,6 +1267,7 @@ static int get_dual_timestamp(sd_journal *j, dual_timestamp *ret_ts, sd_id128_t PARSE_FIELD_VEC_ENTRY("_SOURCE_MONOTONIC_TIMESTAMP=", &monotonic, &monotonic_len), }; int r; + bool realtime_good = false, monotonic_good = false, boot_id_good = false; assert(j); assert(ret_ts); @@ -1284,21 +1285,24 @@ static int get_dual_timestamp(sd_journal *j, dual_timestamp *ret_ts, sd_id128_t return r; if (realtime) - r = safe_atou64(realtime, &ret_ts->realtime); - if (!realtime || r < 0 || !VALID_REALTIME(ret_ts->realtime)) - r = sd_journal_get_realtime_usec(j, &ret_ts->realtime); - if (r < 0) + realtime_good = safe_atou64(realtime, &ret_ts->realtime) >= 0; + if (!realtime_good || !VALID_REALTIME(ret_ts->realtime)) + realtime_good = sd_journal_get_realtime_usec(j, &ret_ts->realtime) >= 0; + if (!realtime_good) ret_ts->realtime = USEC_INFINITY; if (monotonic) - r = safe_atou64(monotonic, &ret_ts->monotonic); - r = sd_journal_get_monotonic_usec( - j, - !monotonic || r < 0 || !VALID_MONOTONIC(ret_ts->monotonic) ? &ret_ts->monotonic : NULL, - ret_boot_id); - if (r < 0) + monotonic_good = safe_atou64(monotonic, &ret_ts->monotonic) >= 0; + if (!monotonic_good || !VALID_MONOTONIC(ret_ts->monotonic)) + monotonic_good = boot_id_good = sd_journal_get_monotonic_usec(j, &ret_ts->monotonic, ret_boot_id) >= 0; + if (!monotonic_good) ret_ts->monotonic = USEC_INFINITY; + if (!boot_id_good) + boot_id_good = sd_journal_get_monotonic_usec(j, NULL, ret_boot_id) >= 0; + if (!boot_id_good) + *ret_boot_id = SD_ID128_NULL; + /* Restart all data before */ sd_journal_restart_data(j); sd_journal_restart_unique(j); -- 2.47.3