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



Reply via email to