From 525dc1d17d273ed6b38afa09b778fb5752366bfe Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Fri, 4 Aug 2023 14:50:04 +0200 Subject: [PATCH] readutmp: Ensure multithread-safety. * lib/readutmp.c (get_boot_time): Initialize 'cached' after 'boot_time', not before. Also declare both as volatile. --- ChangeLog | 6 ++++++ lib/readutmp.c | 6 +++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index dc30f193f0..7e9c04111e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2023-08-04 Bruno Haible + + readutmp: Ensure multithread-safety. + * lib/readutmp.c (get_boot_time): Initialize 'cached' after 'boot_time', + not before. Also declare both as volatile. + 2023-08-04 Bruno Haible readutmp tests: Fix gcc warning (regression from yesterday). diff --git a/lib/readutmp.c b/lib/readutmp.c index c692b3854d..01c02ad36e 100644 --- a/lib/readutmp.c +++ b/lib/readutmp.c @@ -152,13 +152,13 @@ get_boot_time_uncached (void) static struct timespec get_boot_time (void) { - static bool cached; - static struct timespec boot_time; + static bool volatile cached; + static struct timespec volatile boot_time; if (!cached) { - cached = true; boot_time = get_boot_time_uncached (); + cached = true; } return boot_time; } -- 2.39.5