From: Paul Eggert Date: Wed, 12 Jun 2024 15:42:24 +0000 (-0700) Subject: Start of a fix for bug#71477 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=3b97c9ea01026dfcd5dbd889d46a929fe2abcf55;p=emacs.git 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. (cherry picked from commit ac14d56a4d749661a9c52941e6511a0c300d35e4) --- diff --git a/src/filelock.c b/src/filelock.c index 050cac565c9..d4a4747955a 100644 --- a/src/filelock.c +++ b/src/filelock.c @@ -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)