Skip to content

Commit 23939b7

Browse files
authored
cmp: sysread failure guard (#711)
* Input loop terminates if sysread on file1 fails * If sysread failed on file2, $read_in2 would be undef, then $checklength would be undef * It is not correct to compare the two input buffers in this case; $buffer2 possibly wouldn't have been modified since the previous loop iteration * Skip the buffer comparison if $checklength is zero (treat sysread failure in the same way because there are no bytes to compare)
1 parent 70393e7 commit 23939b7

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

bin/cmp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,13 @@ if ($skip2) {
168168
}
169169

170170
READ: while (defined ($read_in1 = sysread $fh1, $buffer1, $chunk_size)) {
171-
$read_in2 = sysread $fh2, $buffer2, $chunk_size;
171+
$read_in2 = sysread $fh2, $buffer2, $chunk_size;
172+
$read_in2 = 0 unless defined $read_in2; # sysread failed
172173

173174
my $checklength = min($read_in1, $read_in2);
174175
$checklength-- if $checklength;
175176

176-
if ($buffer1 ne $buffer2) {
177+
if ($checklength != 0 && $buffer1 ne $buffer2) {
177178
my @bytes1 = unpack 'C*', $buffer1;
178179
my @bytes2 = unpack 'C*', $buffer2;
179180

0 commit comments

Comments
 (0)