Start of a fix for bug#71477

* src/filelock.c (integer_prefixed): New static function.
(VALID_PROCESS_ID): New macro.
(current_lock_owner): Use them to allow negative process IDs
on some Microsoft platforms.
This commit is contained in:
Paul Eggert 2024-06-12 08:42:24 -07:00
parent b8873a7080
commit ac14d56a4d

View file

@ -342,6 +342,22 @@ read_lock_data (char *lfname, char lfinfo[MAX_LFINFO + 1])
return nbytes;
}
/* Whether the string S starts with a decimal integer, optionally
negative. */
static bool
integer_prefixed (char const *s)
{
/* Doing it this way avoids a conditional branch on most platforms. */
return c_isdigit (s[s[0] == '-']);
}
/* Whether the integer P could identify an individual process. On most
platforms this simply checks for positive pid_t, but on some
Microsoft ports our headers #define it to to some other test. */
#ifndef VALID_PROCESS_ID
# define VALID_PROCESS_ID(p) (0 < (p) && (p) <= TYPE_MAXIMUM (pid_t))
#endif
/* True if errno values are negative. Although the C standard
requires them to be positive, they are negative in Haiku. */
enum { NEGATIVE_ERRNO = EDOM < 0 };
@ -393,7 +409,7 @@ current_lock_owner (lock_info_type *owner, Lisp_Object lfname)
return EINVAL;
/* The PID is everything from the last '.' to the ':' or equivalent. */
if (! c_isdigit (dot[1]))
if (! integer_prefixed (dot + 1))
return EINVAL;
errno = 0;
pid = strtoimax (dot + 1, &owner->colon, 10);
@ -419,9 +435,7 @@ current_lock_owner (lock_info_type *owner, Lisp_Object lfname)
boot += 2;
FALLTHROUGH;
case ':':
if (!(c_isdigit (boot[0])
/* A negative number. */
|| (boot[0] == '-' && c_isdigit (boot[1]))))
if (! integer_prefixed (boot))
return EINVAL;
boot_time = strtoimax (boot, &lfinfo_end, 10);
break;
@ -451,7 +465,7 @@ current_lock_owner (lock_info_type *owner, Lisp_Object lfname)
{
if (pid == getpid ())
return I_OWN_IT;
else if (0 < pid && pid <= TYPE_MAXIMUM (pid_t)
else if (VALID_PROCESS_ID (pid)
&& (kill (pid, 0) >= 0 || errno == EPERM)
&& (boot_time == 0
|| (boot_time <= TYPE_MAXIMUM (time_t)