Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmp: check length off-by-one #450

Merged
merged 1 commit into from
Feb 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions bin/cmp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use strict;

use File::Basename qw(basename);
use Getopt::Std qw(getopts);
use List::Util qw(min);

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

my $checklength = $chunk_size;
if ($read_in1 < $chunk_size or $read_in2 < $chunk_size) {
$checklength = ( $read_in1 < $read_in2 ?
$read_in1 :
$read_in2 ) - 1;
}
my $checklength = min($read_in1, $read_in2);
$checklength-- if $checklength;

if ($buffer1 ne $buffer2) {
my @bytes1 = unpack 'C*', $buffer1;
Expand Down
Loading