Fix overflow in Windows replacement pg_pread/pg_pwrite.
authorThomas Munro <[email protected]>
Sat, 2 Mar 2024 19:40:41 +0000 (08:40 +1300)
committerThomas Munro <[email protected]>
Sat, 2 Mar 2024 19:40:41 +0000 (08:40 +1300)
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 <[email protected]>
Discussion: https://postgr.es/m/1672202.1703441340%40sss.pgh.pa.us

src/port/win32pread.c
src/port/win32pwrite.c

index e1a066fdbe483a854d6d87ec737f4c415bbf2c0e..2d022e6d3784e45c8055ca91605754671e936c85 100644 (file)
@@ -30,6 +30,9 @@ pg_pread(int fd, void *buf, size_t size, off_t offset)
        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))
index c54bf041bf34a72167a6fd7756376d7435e2ed1b..b37bb2f92e0ab2094bc6541c734ad7549b3b672c 100644 (file)
@@ -30,6 +30,9 @@ pg_pwrite(int fd, const void *buf, size_t size, off_t offset)
        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 (!WriteFile(handle, buf, size, &result, &overlapped))