mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-09-27 17:21:55 +02:00
1e01374654
When calling the Windows file I/O APIs there is an implicit conversion from size_t to DWORD, which could overflow. Clamp the size at 1GB to avoid that. Not a really a live bug as we don't expect anything in PostgreSQL to call with such large values. Reviewed-by: Peter Eisentraut <peter@eisentraut.org> Discussion: https://postgr.es/m/1672202.1703441340%40sss.pgh.pa.us
49 lines
982 B
C
49 lines
982 B
C
/*-------------------------------------------------------------------------
|
|
*
|
|
* win32pread.c
|
|
* Implementation of pread(2) for Windows.
|
|
*
|
|
* Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
|
|
*
|
|
* IDENTIFICATION
|
|
* src/port/win32pread.c
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
|
|
|
|
#include "c.h"
|
|
|
|
#include <windows.h>
|
|
|
|
ssize_t
|
|
pg_pread(int fd, void *buf, size_t size, off_t offset)
|
|
{
|
|
OVERLAPPED overlapped = {0};
|
|
HANDLE handle;
|
|
DWORD result;
|
|
|
|
handle = (HANDLE) _get_osfhandle(fd);
|
|
if (handle == INVALID_HANDLE_VALUE)
|
|
{
|
|
errno = EBADF;
|
|
return -1;
|
|
}
|
|
|
|
/* Avoid overflowing DWORD. */
|
|
size = Min(size, 1024 * 1024 * 1024);
|
|
|
|
/* Note that this changes the file position, despite not using it. */
|
|
overlapped.Offset = offset;
|
|
if (!ReadFile(handle, buf, size, &result, &overlapped))
|
|
{
|
|
if (GetLastError() == ERROR_HANDLE_EOF)
|
|
return 0;
|
|
|
|
_dosmaperr(GetLastError());
|
|
return -1;
|
|
}
|
|
|
|
return result;
|
|
}
|