On 2026-11-19 14:22, Paul Eggert via Cygwin wrote:
This is a followup to GNU diffutils bug 82273 <https://bugs.gnu.org/82273>, which appears to stem from a Cygwin bug. I do not have a local Cygwin installation to run cygcheck.

On 64-bit Cygwin, calling read() on a regular file with a buffer size of 2**32 bytes or larger appears to truncate the requested length to its lower 32 bits.

When the requested byte count is a non-zero multiple of 2**32, read() returns 0 immediately instead of reading data, causing callers to treat the result as EOF.

When running diff on two regular files of 11,070,000,002 bytes each, diff first reads 65,536 bytes and then calls read() for the remaining 11,069,934,480 bytes). In the strace output:

   read: read(3, 0x6FFD6C2E0010, -1814967408) blocking
   fhandler_base::read: returning -1814967408, binary mode
   read: 2479999888 = read(3, 0x6FFD6C2E0010, -1814967408)
   read: read(3, 0x6FFDFFFFCBA0, 0) blocking
   fhandler_base::read: returning 0, binary mode
   read: 0 = read(3, 0x6FFDFFFFCBA0, 0)

Only the lower 32 bits (0x93D1CB90 = 2,479,999,888 bytes) were read on the first call.  When diff looped to read the remaining 8,589,934,592 bytes (0x200000000), the length was truncated to 0, returning a false EOF and causing diff to silently ignore the last ~8.59 GB of both files. (strace logs the length and internal return value as signed 32-bit integers.)

I have not checked whether other system calls like write() have a similar problem. Although I plan to modify GNU diffutils to work around the Cygwin bug, obviously the bug should be fixed in Cygwin too.

Though I can't easily run Cygwin, Gemini suggests this reproducer:

   #include <stdio.h>
   #include <stdlib.h>
   #include <unistd.h>

   int
   main (void)
   {
     size_t size = 1ULL << 32;
     void *buf = malloc (size);
     if (!buf)
       {
         perror ("malloc");
         return 1;
       }

     ssize_t n = read (0, buf, size);
     printf ("read returned %zd (expected %zu)\n", n, size);
     return n != (ssize_t) size;
   }

Which can be run with a sparse file via:

   gcc -O2 test_read.c -o test_read
   truncate -s 4294967297 testfile
   ./test_read < testfile

Thanks Paul,

Correct - reproducible!
This is despite built in Large File Support AFAIK?

--
Take care. Thanks, Brian Inglis              Calgary, Alberta, Canada

La perfection est atteinte                   Perfection is achieved
non pas lorsqu'il n'y a plus rien à ajouter  not when there is no more to add
mais lorsqu'il n'y a plus rien à retrancher  but when there is no more to cut
                                -- Antoine de Saint-Exupéry
/* test-read-size-2GB.c - Cygwin test read 2GB+ */

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int
main (void)
{
  size_t size = 1ULL << 32;
  void *buf = malloc (size);
  if (!buf)
    {
      perror ("malloc");
      return errno;
    }

  ssize_t n = read (0, buf, size);
  printf ("read returned %zd (expected %zu)\n", n, size);
  return n != (ssize_t) size;
} 
#!/bin/sh
# test-read-size-2GB.sh - Cygwin test read 2GB+

trs2GB=test-read-size-2GB
fourGBp1=$(((1<<32)+1))

gcc -O2 -o $trs2GB      $trs2GB.c

truncate -s $fourGBp1   $trs2GB.dat

ls -Gg                  $trs2GB.dat

./$trs2GB       <       $trs2GB.dat

#!/bin/sh
# test-read-size-2GB.sh - Cygwin test read 2GB+

trs2GB=test-read-size-2GB
+ trs2GB=test-read-size-2GB
fourGBp1=$(((1<<32)+1))
+ fourGBp1=4294967297

gcc -O2 -o $trs2GB      $trs2GB.c
+ gcc -O2 -o test-read-size-2GB test-read-size-2GB.c

truncate -s $fourGBp1   $trs2GB.dat
+ truncate -s 4294967297 test-read-size-2GB.dat

ls -Gg                  $trs2GB.dat
+ ls -Gg test-read-size-2GB.dat
-rw-r--r-- 1 4294967297 Nov 19 19:30 test-read-size-2GB.dat

./$trs2GB       <       $trs2GB.dat
+ ./test-read-size-2GB
read returned 0 (expected 4294967296)

Reply via email to