]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
cksum: fix off-by-1 bug with \r stripping
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 16 Sep 2021 07:17:18 +0000 (00:17 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 16 Sep 2021 07:25:26 +0000 (00:25 -0700)
Problem reported by Jim Meyering (Bug#50611).
* src/digest.c (digest_check): When stripping trailing \r,
avoid subscript error before start of line.

src/digest.c

index 0d7ce3265e977f30a7fce8b8e3fb5e97065682fe..fdf01b21b9f07a9dee7022484a1b7746eee1c36d 100644 (file)
@@ -1070,11 +1070,10 @@ digest_check (char const *checkfile_name)
         continue;
 
       /* Remove any trailing newline.  */
-      if (line[line_length - 1] == '\n')
-        line[--line_length] = '\0';
+      line_length -= line[line_length - 1] == '\n';
       /* Remove any trailing carriage return.  */
-      if (line[line_length - 1] == '\r')
-        line[--line_length] = '\0';
+      line_length -= line[line_length - (0 < line_length)] == '\r';
+      line[line_length] = '\0';
 
       if (! (split_3 (line, line_length, &hex_digest, &binary, &filename)
              && ! (is_stdin && STREQ (filename, "-"))))