From 752dbd68cc84fc14beb8979cd77c10025d741ef2 Mon Sep 17 00:00:00 2001 From: Eli Zaretskii Date: Thu, 13 Jun 2024 10:24:21 +0300 Subject: [PATCH] Fix removal of file locks on MS-Windows 9X A new Windows-specific function for validating process ID is introduced that does TRT with possibly negative PID values returned by 'getpid' on Windows 9X and also with values larger than INT_MAX, since PID on Windows is actually an unsigned 32-bit value. * src/w32proc.c (w32_valid_process_id): New function. * src/w32common.h (VALID_PROCESS_ID): Define for WINDOWSNT. * src/filelock.c [WINDOWSNT]: Include w32common.h. (Bug#71477) (cherry picked from commit 0007231a7805abce945349c7c9c75b45155bda5c) --- src/filelock.c | 3 ++- src/w32common.h | 2 ++ src/w32proc.c | 13 +++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/filelock.c b/src/filelock.c index d4a4747955a..43ede30975e 100644 --- a/src/filelock.c +++ b/src/filelock.c @@ -47,6 +47,7 @@ along with GNU Emacs. If not, see . */ #ifdef WINDOWSNT #include #include /* for fcntl */ +#include "w32common.h" #endif #ifndef MSDOS @@ -353,7 +354,7 @@ integer_prefixed (char const *s) /* 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. */ + MS-Windows 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 diff --git a/src/w32common.h b/src/w32common.h index 9475481b64e..8a5c7c03bca 100644 --- a/src/w32common.h +++ b/src/w32common.h @@ -53,6 +53,8 @@ extern void cache_system_info (void); #ifdef WINDOWSNT /* Return a static buffer with the MS-Windows version string. */ extern char * w32_version_string (void); +extern bool w32_valid_process_id (intmax_t); +# define VALID_PROCESS_ID(p) w32_valid_process_id(p) #endif typedef void (* VOIDFNPTR) (void); diff --git a/src/w32proc.c b/src/w32proc.c index 55ead13647b..4d237992b14 100644 --- a/src/w32proc.c +++ b/src/w32proc.c @@ -2676,6 +2676,19 @@ count_children: return nr; } +/* This is called from filelock.c:current_lock_owner to validate a PID. + Return true if PID could identify a process on the current host, + false otherwise. */ +bool +w32_valid_process_id (intmax_t id) +{ + if (id == -1 || id == 0 /* always invalid */ + || id > UINT32_MAX /* PID is actually a DWORD */ + || id < INT32_MIN) /* Windows 9X can report negative PIDs */ + return false; + return true; +} + /* Substitute for certain kill () operations */ static BOOL CALLBACK -- 2.39.2