]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
md5sum: quote all printed file names
authorPádraig Brady <P@draigBrady.com>
Mon, 26 Oct 2015 01:26:04 +0000 (01:26 +0000)
committerPádraig Brady <P@draigBrady.com>
Tue, 27 Oct 2015 13:20:50 +0000 (13:20 +0000)
This is especially significant when using --check
with files generated on a windows system, where the \r
characters produce corrupted and confusing error messages.
This also ensures status messages are output on a single line.

* src/md5sum.c: Use quote() for printed file names.
* tests/misc/md5sum.pl: Adjust accordingly.
* NEWS: Mention the change in behavior.
Fixes http://bugs.gnu.org/21757

NEWS
src/md5sum.c
tests/misc/md5sum.pl
tests/misc/sha1sum.pl

diff --git a/NEWS b/NEWS
index e7715856252e18345cd23edefe9522ba211188dc..1997632f2fcafaf26edbd90b5751031697168862 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -23,6 +23,9 @@ GNU coreutils NEWS                                    -*- outline -*-
   df now prefers sources towards the root of a device when
   eliding duplicate bind mounted entries.
 
+  md5sum now quotes all printed file names to avoid confusing error messages.
+  This also affects sha1sum, sha224sum, sha256sum, sha384sum and sha512sum.
+
 ** Improvements
 
   dircolors now supports globbing of TERM entries in its database.
index bc2b70927b8523ca5d16af204a79be461db87f6a..564b7539236284f2faa17ada5dc9d58ae7c93858 100644 (file)
@@ -37,6 +37,7 @@
 #endif
 #include "error.h"
 #include "fadvise.h"
+#include "quote.h"
 #include "stdio--.h"
 #include "xfreopen.h"
 
@@ -451,7 +452,7 @@ digest_file (const char *filename, int *binary, unsigned char *bin_result)
       fp = fopen (filename, (O_BINARY && *binary ? "rb" : "r"));
       if (fp == NULL)
         {
-          error (0, errno, "%s", filename);
+          error (0, errno, "%s", quote (filename));
           return false;
         }
     }
@@ -461,7 +462,7 @@ digest_file (const char *filename, int *binary, unsigned char *bin_result)
   err = DIGEST_STREAM (fp, bin_result);
   if (err)
     {
-      error (0, errno, "%s", filename);
+      error (0, errno, "%s", quote (filename));
       if (fp != stdin)
         fclose (fp);
       return false;
@@ -469,7 +470,7 @@ digest_file (const char *filename, int *binary, unsigned char *bin_result)
 
   if (!is_stdin && fclose (fp) != 0)
     {
-      error (0, errno, "%s", filename);
+      error (0, errno, "%s", quote (filename));
       return false;
     }
 
@@ -504,7 +505,7 @@ digest_check (const char *checkfile_name)
       checkfile_stream = fopen (checkfile_name, "r");
       if (checkfile_stream == NULL)
         {
-          error (0, errno, "%s", checkfile_name);
+          error (0, errno, "%s", quote (checkfile_name));
           return false;
         }
     }
@@ -522,7 +523,7 @@ digest_check (const char *checkfile_name)
       ++line_number;
       if (line_number == 0)
         error (EXIT_FAILURE, 0, _("%s: too many checksum lines"),
-               checkfile_name);
+               quote (checkfile_name));
 
       line_length = getline (&line, &line_chars_allocated, checkfile_stream);
       if (line_length <= 0)
@@ -547,7 +548,7 @@ digest_check (const char *checkfile_name)
               error (0, 0,
                      _("%s: %" PRIuMAX
                        ": improperly formatted %s checksum line"),
-                     checkfile_name, line_number,
+                     quote (checkfile_name), line_number,
                      DIGEST_TYPE_STRING);
             }
 
@@ -569,9 +570,7 @@ digest_check (const char *checkfile_name)
             {
               ++n_open_or_read_failures;
               if (!status_only)
-                {
-                  printf (_("%s: FAILED open or read\n"), filename);
-                }
+                printf (_("%s: FAILED open or read\n"), quote (filename));
             }
           else
             {
@@ -593,9 +592,9 @@ digest_check (const char *checkfile_name)
               if (!status_only)
                 {
                   if (cnt != digest_bin_bytes)
-                    printf ("%s: %s\n", filename, _("FAILED"));
+                    printf ("%s: %s\n", quote (filename), _("FAILED"));
                   else if (!quiet)
-                    printf ("%s: %s\n", filename, _("OK"));
+                    printf ("%s: %s\n", quote (filename), _("OK"));
                 }
             }
         }
@@ -606,13 +605,13 @@ digest_check (const char *checkfile_name)
 
   if (ferror (checkfile_stream))
     {
-      error (0, 0, _("%s: read error"), checkfile_name);
+      error (0, 0, _("%s: read error"), quote (checkfile_name));
       return false;
     }
 
   if (!is_stdin && fclose (checkfile_stream) != 0)
     {
-      error (0, errno, "%s", checkfile_name);
+      error (0, errno, "%s", quote (checkfile_name));
       return false;
     }
 
@@ -620,7 +619,7 @@ digest_check (const char *checkfile_name)
     {
       /* Warn if no tests are found.  */
       error (0, 0, _("%s: no properly formatted %s checksum lines found"),
-             checkfile_name, DIGEST_TYPE_STRING);
+             quote (checkfile_name), DIGEST_TYPE_STRING);
     }
   else
     {
index b3ff4b4f6c426533ffa60028ee222c16b5d7ae6e..d29a4efa741fc72c07bfb65997821fd3496ae88e 100755 (executable)
@@ -44,12 +44,12 @@ my @Tests =
                                 {OUT=>"\\$degenerate  .\\\\foo\n"}],
      ['check-1', '--check', {AUX=> {f=> ''}},
                                 {IN=> {'f.md5' => "$degenerate  f\n"}},
-                                {OUT=>"f: OK\n"}],
+                                {OUT=>"'f': OK\n"}],
 
      # Same as above, but with an added empty line, to provoke --strict.
      ['ck-strict-1', '--check --strict', {AUX=> {f=> ''}},
                                 {IN=> {'f.md5' => "$degenerate  f\n\n"}},
-                                {OUT=>"f: OK\n"},
+                                {OUT=>"'f': OK\n"},
                                 {ERR=>"md5sum: "
                                  . "WARNING: 1 line is improperly formatted\n"},
                                 {EXIT=> 1}],
@@ -58,7 +58,7 @@ my @Tests =
      # lines are processed in spite of the preceding invalid input line.
      ['ck-strict-2', '--check --strict', {AUX=> {f=> ''}},
                                 {IN=> {'in.md5' => "\n$degenerate  f\n"}},
-                                {OUT=>"f: OK\n"},
+                                {OUT=>"'f': OK\n"},
                                 {ERR=>"md5sum: "
                                  . "WARNING: 1 line is improperly formatted\n"},
                                 {EXIT=> 1}],
@@ -69,7 +69,7 @@ my @Tests =
                                 {OUT=>""}],
      ['check-quiet2', '--check', '--quiet',
                                 {IN=>{'f.md5' => "$degenerate  f\n"}},
-                                {AUX=> {f=> 'foo'}}, {OUT=>"f: FAILED\n"},
+                                {AUX=> {f=> 'foo'}}, {OUT=>"'f': FAILED\n"},
                                 {ERR=>"md5sum: WARNING: 1 computed"
                                        . " checksum did NOT match\n"},
                                 {EXIT=> 1}],
@@ -80,7 +80,7 @@ my @Tests =
                                       . "$degenerate  f\n"
                                       . "invalid\n" }},
                                 {AUX=> {f=> 'foo'}},
-                                {OUT=>"f: FAILED\nf: FAILED\n"},
+                                {OUT=>"'f': FAILED\n'f': FAILED\n"},
                 {ERR=>"md5sum: WARNING: 1 line is improperly formatted\n"
                     . "md5sum: WARNING: 2 computed checksums did NOT match\n"},
                                 {EXIT=> 1}],
@@ -91,8 +91,9 @@ my @Tests =
                                       . "$degenerate  f\n"
                                       . "invalid\n" }},
                                 {AUX=> {f=> 'foo'}},
-                                {OUT=>"f: FAILED\nf: FAILED\n"},
-              {ERR=>"md5sum: f.md5: 3: improperly formatted MD5 checksum line\n"
+                                {OUT=>"'f': FAILED\n'f': FAILED\n"},
+              {ERR=>"md5sum: 'f.md5': 3: "
+                              . "improperly formatted MD5 checksum line\n"
                   . "md5sum: WARNING: 1 line is improperly formatted\n"
                   . "md5sum: WARNING: 2 computed checksums did NOT match\n"},
                                 {EXIT=> 1}],
@@ -101,26 +102,26 @@ my @Tests =
      # sha1sum accept BSD format.
      ['check-bsd', '--check', {IN=> {'f.sha1' => "SHA1 (f) = $degenerate\n"}},
                                 {AUX=> {f=> ''}},
-                                {ERR=>"md5sum: f.sha1: no properly formatted "
+                                {ERR=>"md5sum: 'f.sha1': no properly formatted "
                                        . "MD5 checksum lines found\n"},
                                 {EXIT=> 1}],
      ['check-bsd2', '--check', {IN=> {'f.md5' => "MD5 (f) = $degenerate\n"}},
-                                {AUX=> {f=> ''}}, {OUT=>"f: OK\n"}],
+                                {AUX=> {f=> ''}}, {OUT=>"'f': OK\n"}],
      ['check-bsd3', '--check', '--status',
                                 {IN=> {'f.md5' => "MD5 (f) = $degenerate\n"}},
                                 {AUX=> {f=> 'bar'}}, {EXIT=> 1}],
      ['check-openssl', '--check', {IN=> {'f.sha1' => "SHA1(f)= $degenerate\n"}},
                                 {AUX=> {f=> ''}},
-                                {ERR=>"md5sum: f.sha1: no properly formatted "
+                                {ERR=>"md5sum: 'f.sha1': no properly formatted "
                                        . "MD5 checksum lines found\n"},
                                 {EXIT=> 1}],
      ['check-openssl2', '--check', {IN=> {'f.md5' => "MD5(f)= $degenerate\n"}},
-                                {AUX=> {f=> ''}}, {OUT=>"f: OK\n"}],
+                                {AUX=> {f=> ''}}, {OUT=>"'f': OK\n"}],
      ['check-openssl3', '--check', '--status',
                                 {IN=> {'f.md5' => "MD5(f)= $degenerate\n"}},
                                 {AUX=> {f=> 'bar'}}, {EXIT=> 1}],
      ['bsd-segv', '--check', {IN=> {'z' => "MD5 ("}}, {EXIT=> 1},
-      {ERR=> "$prog: z: no properly formatted MD5 checksum lines found\n"}],
+      {ERR=> "$prog: 'z': no properly formatted MD5 checksum lines found\n"}],
 
      # Ensure that when there's a NUL byte among the checksum hex digits
      # we detect the invalid formatting and don't even open the file.
@@ -128,7 +129,7 @@ my @Tests =
      #   h: FAILED
      #   md5sum: WARNING: 1 of 1 computed checksum did NOT match
      ['nul-in-cksum', '--check', {IN=> {'h'=>("\0"x32)."  h\n"}}, {EXIT=> 1},
-      {ERR=> "$prog: h: no properly formatted MD5 checksum lines found\n"}],
+      {ERR=> "$prog: 'h': no properly formatted MD5 checksum lines found\n"}],
     );
 
 # Insert the '--text' argument for each test.
index de923b63638664c5d149a3108fb3cfde262e3ff0..0c22e7a18c1868fefef93b165fc11e654a502476 100755 (executable)
@@ -53,29 +53,29 @@ my @Tests =
      # md5sum accept BSD format.
      ['check-bsd', '--check', {IN=> {'f.md5' => "MD5 (f) = $sha_degenerate\n"}},
                         {AUX=> {f=> ''}},
-                        {ERR=>"sha1sum: f.md5: no properly formatted "
+                        {ERR=>"sha1sum: 'f.md5': no properly formatted "
                           . "SHA1 checksum lines found\n"},
                         {EXIT=> 1}],
      ['check-bsd2', '--check',
                         {IN=> {'f.sha1' => "SHA1 (f) = $sha_degenerate\n"}},
-                        {AUX=> {f=> ''}}, {OUT=>"f: OK\n"}],
+                        {AUX=> {f=> ''}}, {OUT=>"'f': OK\n"}],
      ['check-bsd3', '--check', '--status',
                         {IN=> {'f.sha1' => "SHA1 (f) = $sha_degenerate\n"}},
                         {AUX=> {f=> 'bar'}}, {EXIT=> 1}],
      ['check-openssl', '--check',
                         {IN=> {'f.md5' => "MD5(f)= $sha_degenerate\n"}},
                         {AUX=> {f=> ''}},
-                        {ERR=>"sha1sum: f.md5: no properly formatted "
+                        {ERR=>"sha1sum: 'f.md5': no properly formatted "
                           . "SHA1 checksum lines found\n"},
                         {EXIT=> 1}],
      ['check-openssl2', '--check',
                         {IN=> {'f.sha1' => "SHA1(f)= $sha_degenerate\n"}},
-                        {AUX=> {f=> ''}}, {OUT=>"f: OK\n"}],
+                        {AUX=> {f=> ''}}, {OUT=>"'f': OK\n"}],
      ['check-openssl3', '--check', '--status',
                         {IN=> {'f.sha1' => "SHA1(f)= $sha_degenerate\n"}},
                         {AUX=> {f=> 'bar'}}, {EXIT=> 1}],
      ['bsd-segv', '--check', {IN=> {'z' => "SHA1 ("}}, {EXIT=> 1},
-      {ERR=> "$prog: z: no properly formatted SHA1 checksum lines found\n"}],
+      {ERR=> "$prog: 'z': no properly formatted SHA1 checksum lines found\n"}],
     );
 
 # Insert the '--text' argument for each test.