Skip to content

Commit cba333a

Browse files
authored
cmp: check length off-by-one (#450)
* $checklength is the maximum offset in buffer to check which byte(s) in the 2 buffers are not equal * The subsequent loop is from 0 to $checklength, so avoid potentially accessing $bytes1[$chunk_size] * If $read_in1 and $read_in2 are equal to $chunk_size, the value of $checklength is not decremented * Calculate minimum of $read_in1 and $read_in2 (these will never exceed $chunk_size), then decrement by 1
1 parent 8edad69 commit cba333a

File tree

1 file changed

+3
-6
lines changed

1 file changed

+3
-6
lines changed

bin/cmp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use strict;
3838

3939
use File::Basename qw(basename);
4040
use Getopt::Std qw(getopts);
41+
use List::Util qw(min);
4142

4243
use constant EX_SUCCESS => 0;
4344
use constant EX_DIFFERENT => 1;
@@ -174,12 +175,8 @@ if ($skip2) {
174175
READ: while (defined ($read_in1 = sysread $fh1, $buffer1, $chunk_size)) {
175176
$read_in2 = sysread $fh2, $buffer2, $chunk_size;
176177

177-
my $checklength = $chunk_size;
178-
if ($read_in1 < $chunk_size or $read_in2 < $chunk_size) {
179-
$checklength = ( $read_in1 < $read_in2 ?
180-
$read_in1 :
181-
$read_in2 ) - 1;
182-
}
178+
my $checklength = min($read_in1, $read_in2);
179+
$checklength-- if $checklength;
183180

184181
if ($buffer1 ne $buffer2) {
185182
my @bytes1 = unpack 'C*', $buffer1;

0 commit comments

Comments
 (0)