From cbacdca9e3f6dcf9b88704391f06daf7301608b0 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Thu, 15 Aug 2024 11:29:16 -0700 Subject: [PATCH 1/4] Fix unlikely lock file integer overflow * src/filelock.c (within_one_second): Accept intmax_t first arg. Avoid undefined behavior on integer overflow. (current_lock_owner): Simplify based on within_one_second change. --- src/filelock.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/filelock.c b/src/filelock.c index 69bd0322d4c..55ab15feb8d 100644 --- a/src/filelock.c +++ b/src/filelock.c @@ -298,9 +298,10 @@ lock_file_1 (Lisp_Object lfname, bool force) /* Return true if times A and B are no more than one second apart. */ static bool -within_one_second (time_t a, time_t b) +within_one_second (intmax_t a, time_t b) { - return (a - b >= -1 && a - b <= 1); + intmax_t diff; + return !ckd_sub (&diff, a, b) && -1 <= diff && diff <= 1; } /* On systems lacking ELOOP, test for an errno value that shouldn't occur. */ @@ -469,8 +470,7 @@ current_lock_owner (lock_info_type *owner, Lisp_Object lfname) else if (VALID_PROCESS_ID (pid) && (kill (pid, 0) >= 0 || errno == EPERM) && (boot_time == 0 - || (boot_time <= TYPE_MAXIMUM (time_t) - && within_one_second (boot_time, get_boot_sec ())))) + || within_one_second (boot_time, get_boot_sec ()))) return ANOTHER_OWNS_IT; /* The owner process is dead or has a strange pid, so try to zap the lockfile. */ -- 2.43.0