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.
This commit is contained in:
Paul Eggert 2024-08-15 11:29:16 -07:00
parent 8db72a8d4b
commit cbacdca9e3

View file

@ -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. */