From: Paul Eggert Date: Thu, 16 Sep 2021 07:17:18 +0000 (-0700) Subject: cksum: fix off-by-1 bug with \r stripping X-Git-Tag: v9.0~26 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=121f74dfc2e060a2ae174a636cd773f4d461a852;p=thirdparty%2Fcoreutils.git cksum: fix off-by-1 bug with \r stripping Problem reported by Jim Meyering (Bug#50611). * src/digest.c (digest_check): When stripping trailing \r, avoid subscript error before start of line. --- diff --git a/src/digest.c b/src/digest.c index 0d7ce3265e..fdf01b21b9 100644 --- a/src/digest.c +++ b/src/digest.c @@ -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, "-"))))