Hi,

How to reproduce:

    1. Run md5sum interactively.  Make sure you're using textutils
    md5sum, some GNU/Linux distributions (like Debian) have their own
    md5sum and rename the one from textutils to something like md5sum.textutils.
        $ md5sum
 
    2. Type one line of text into md5sum, then hit CR. "foobar" works.
        $ md5sum
        foobar

    3. Now hit EOF.  Nothing should happen (the bug)
        $ md5sum
        foobar

    4. Hit EOF again.  Now it should exit.
        $ md5sum
        foobar
        14758f1afd44c09b7992073ccf00b43d  -
        $

Doing this again under strace reveals:

    read(0, foobar
    "foobar\n", 4096)               = 7
    read(0, "", 3072)                       = 0
    read(0, "", 3072)                       = 0
    [..other stuff removed..]
    close(0)                                = 0

  that fread() ends up being called again, even though it has
already found EOF.  The bug in some textutils is that they are expecting
slightly different fread() libc semantics.

They are expecting fread() to return 0 when it reaches EOF.  Specifically
when fread() is used with size = 1, nmemb > 1.

This is untrue.  fread() can return non-zero but still report EOF.
The three attached patches save this unnecessary additional call
to fread() in cksum, md5sum and sha1sum.

There may be other instances, but these are the only ones that
have affected me. ;)

Thanks.

-- 
Michael Bacarella                  24/7 phone: 646 641-8662
Netgraft Corporation                   http://netgraft.com/
      "unique technologies to empower your business"

Finger email address for public key.  Key fingerprint:
  C40C CB1E D2F6 7628 6308  F554 7A68 A5CF 0BD8 C055
--- src/cksum.c.old     2003-02-17 05:55:00.000000000 +0000
+++ src/cksum.c 2003-02-17 05:55:13.000000000 +0000
@@ -235,6 +235,8 @@
       length += bytes_read;
       while (bytes_read--)
        crc = (crc << 8) ^ crctab[((crc >> 24) ^ *cp++) & 0xFF];
+      if (feof(fp))
+        break;
     }
 
   if (ferror (fp))
--- lib/md5.c.old       2003-02-17 05:55:20.000000000 +0000
+++ lib/md5.c   2003-02-17 05:56:11.000000000 +0000
@@ -156,13 +156,17 @@
          n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
 
          sum += n;
+         if (feof(stream))
+           break;
        }
       while (sum < BLOCKSIZE && n != 0);
       if (n == 0 && ferror (stream))
         return 1;
 
-      /* If end of file is reached, end the loop.  */
-      if (n == 0)
+      /* If end of file is reached, end the loop. We can still be at EOF
+                       even if fread() returns > 0
+       */
+      if (n == 0 || feof(stream))
        break;
 
       /* Process buffer with BLOCKSIZE bytes.  Note that
--- lib/sha.c.old       2003-02-17 06:02:52.000000000 +0000
+++ lib/sha.c   2003-02-17 06:03:24.000000000 +0000
@@ -142,13 +142,17 @@
          n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream);
 
          sum += n;
+         if (feof(stream))
+           break;
        }
       while (sum < BLOCKSIZE && n != 0);
       if (n == 0 && ferror (stream))
         return 1;
 
-      /* If end of file is reached, end the loop.  */
-      if (n == 0)
+      /* If end of file is reached, end the loop.  We can be at EOF
+                       even if fread() > 0
+       */
+      if (n == 0 || feof(stream))
        break;
 
       /* Process buffer with BLOCKSIZE bytes.  Note that
_______________________________________________
Bug-textutils mailing list
[EMAIL PROTECTED]
http://mail.gnu.org/mailman/listinfo/bug-textutils

Reply via email to