@@ -242,6 +242,53 @@ final class FileOperationsTestWindows: XCTestCase {
242242 }
243243 }
244244 }
245+
246+ /// Test that buffer sizes exceeding DWORD.max (4GB) are properly rejected
247+ func testBufferSizeLimit( ) throws {
248+ try withTemporaryFilePath ( basename: " testBufferSizeLimit " ) { path in
249+ let fd = try FileDescriptor . open (
250+ path. appending ( " test.txt " ) ,
251+ . readWrite,
252+ options: [ . create, . truncate] ,
253+ permissions: . ownerReadWrite
254+ )
255+ defer { try ? fd. close ( ) }
256+
257+ // Write some data first
258+ try fd. writeAll ( " test data " . utf8)
259+
260+ // Allocate a small buffer for testing
261+ let buffer = UnsafeMutableRawBufferPointer . allocate ( byteCount: 1024 , alignment: 1 )
262+ defer { buffer. deallocate ( ) }
263+
264+ // Test that a count exceeding DWORD.max (UInt32.max = 4,294,967,295) returns EINVAL
265+ // We use a buffer pointer but pass a count > DWORD.max
266+ let oversizedCount = Int ( DWORD . max) + 1
267+ let oversizedBuffer = UnsafeMutableRawBufferPointer (
268+ start: buffer. baseAddress,
269+ count: oversizedCount
270+ )
271+
272+ // pread should fail with EINVAL
273+ do {
274+ _ = try fd. read ( fromAbsoluteOffset: 0 , into: oversizedBuffer)
275+ XCTFail ( " Expected EINVAL for buffer size exceeding DWORD.max " )
276+ } catch let err as Errno {
277+ XCTAssertEqual ( err, . invalidArgument, " Expected EINVAL, got \( err) " )
278+ }
279+
280+ // pwrite should also fail with EINVAL
281+ do {
282+ _ = try fd. write ( toAbsoluteOffset: 0 , UnsafeRawBufferPointer ( oversizedBuffer) )
283+ XCTFail ( " Expected EINVAL for buffer size exceeding DWORD.max " )
284+ } catch let err as Errno {
285+ XCTAssertEqual ( err, . invalidArgument, " Expected EINVAL, got \( err) " )
286+ }
287+
288+ // Verify that exactly DWORD.max works (if we had a buffer that large)
289+ // We can't easily test this without allocating 4GB, but the boundary is correct
290+ }
291+ }
245292}
246293
247294#endif // os(Windows)
0 commit comments