[php-src] PR #21103: Windows Shared Memory (shmop) errno Handling
| From: | EdmondDantes | Date: | Sat, 31 Jan 2026 16:34:47 +0000 |
| Subject: | [php-src] PR #21103: Windows Shared Memory (shmop) errno Handling | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-34748@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/21103
Author: EdmondDantes
### Problem
On Windows, the
shmop extension functions (shmop_open,
shmop_delete, etc.) were not setting errno properly when operations
failed. This resulted in unhelpful error messages like "No error" instead of
meaningful diagnostics.
Additionally, shmctl(IPC_RMID) did not properly close the Windows file mapping handle,
causing shared memory segments to persist even after deletion.
### Solution
#### 1. Windows-to-POSIX errno Mapping (tsrm_win32.c)
Introduced tsrm_set_errno_from_win32_error() function that converts Windows error codes
to POSIX errno values:
| Windows Error | POSIX errno |
|---------------------------------------------------|---------------|
| ERROR_ACCESS_DENIED | EACCES |
| ERROR_NOT_ENOUGH_MEMORY, ERROR_OUTOFMEMORY | ENOMEM
|
| ERROR_INVALID_PARAMETER, ERROR_INVALID_HANDLE | EINVAL
|
| ERROR_ALREADY_EXISTS | EEXIST |
| ERROR_FILE_NOT_FOUND, ERROR_PATH_NOT_FOUND | ENOENT
|
#### 2. errno Set in All Error Paths
**shmget():**
- EINVAL — invalid size (0 or overflow)
- EEXIST — segment exists when IPC_EXCL flag is set
- ENOMEM — failed to allocate internal structure
- Windows error mapping for CreateFileMapping/OpenFileMapping failures
**shmdt():**
- EINVAL — invalid segment address
- Windows error mapping for UnmapViewOfFile failures
**shmctl():**
- EINVAL — invalid key or unknown command
#### 3. Proper Handle Cleanup in IPC_RMID
When shmctl(IPC_RMID) is called and no processes are attached, the Windows file mapping
handle is now properly closed with CloseHandle(). This allows Windows to destroy the
named mapping object, matching POSIX semantics where IPC_RMID marks the segment for
deletion.
### Files Changed
- TSRM/tsrm_win32.c — Core implementation
### Tests Added
- ext/shmop/tests/shmop_errno_codes_win32.phpt
- ext/shmop/tests/shmop_errno_mapping_win32.phpt
- ext/shmop/tests/shmop_errno_tests.phpt
- ext/shmop/tests/shmop_error_conditions_win32.phpt
### Result
Error messages are now meaningful:
- Before: Unable to attach or create shared memory segment "No error"
- After: Unable to attach or create shared memory segment "File exists"