[C++][Windows] mmap returns Win32 error codes instead of errno
- Dominant language
- C++
- Stars
- 17.1k
- Forks
- 4.3k
- Avg merge
- 3d 13h
- Merged PRs (30d)
- 88
Description
### Describe the bug, including details regarding any error messages, version, and platform.
## Problem
The `__map_mman_error()` function in `cpp/src/arrow/io/mman.h` does not properly map Windows error codes to POSIX errno values. The function currently has a TODO comment and returns raw Windows error codes (DWORD values like 2, 5, 6, etc.) instead of POSIX errno values (like ENOENT, EACCES, EBADF, etc.).
## Repro
The function is called internally by Windows memory mapping operations (`mmap`, `munmap`, `mprotect`, `msync`, `mlock`, `munlock`) when Windows API calls fail. The current implementation:
```
// In cpp/src/arrow/io/mman.h, line 42-46 (before fix)
static inline int __map_mman_error(const DWORD err, const int deferr) {
if (err == 0) return 0;
// TODO: implement
return err; // Returns raw Windows error code (e.g., 2, 5, 6, 112)
}
```
When memory mapping operations fail on Windows, `GetLastError()` returns Windows error codes (e.g., `ERROR_FILE_NOT_FOUND = 2`, `ERROR_ACCESS_DENIED = 5`, `ERROR_NOT_ENOUGH_MEMORY = 8`). These values are then assigned to errno, which should contain POSIX errno values.
## Output
This breaks errno-based error handling because:
- Windows error codes (typically small integers like 2-112) are not valid POSIX errno values
- Code that checks errno == ENOENT will fail even when ERROR_FILE_NOT_FOUND (2) occurs
- Error messages and error handling logic expecting POSIX errno values receive raw Windows error codes instead
For example, when `CreateFileMapping` fails with `ERROR_FILE_NOT_FOUND` (2), `errno` is set to 2 instead of `ENOENT` (which is typically 2 on many systems, but this is coincidental and not guaranteed).
## Expected Behavior
The function should map Windows error codes to POSIX errno values, matching Arrow's existing WinErrorToErrno() conventions. Common mappings should include:
- `ERROR_FILE_NOT_FOUND` (2) → `ENOENT`
- `ERROR_ACCESS_DENIED` (5) → `EACCES`
- `ERROR_INVALID_HANDLE` (6) → `EBADF`
- `ERROR_NOT_ENOUGH_MEMORY` (8) → `ENOMEM`
- `ERROR_DISK_FULL` (112) → `ENOSPC`
- etc
### Component(s)
C++
Contributor guide
Assessment
This issue has not been assessed yet.