From 88b26facf4b055f1b9bb6059b10d47d3c0e136c2 Mon Sep 17 00:00:00 2001 From: Michael Mikonos <127171689+mknos@users.noreply.github.com> Date: Fri, 3 Nov 2023 10:05:52 +0800 Subject: [PATCH 1/2] cmp: read hex input with hex() * Hex and decimal offsets are permitted for arguments skip1 and skip2 * eval() was being used but hex() is good enough * hex() can handle the "0x" prefix so we don't need to strip it --- bin/cmp | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/bin/cmp b/bin/cmp index b8de6282..59aca4a6 100755 --- a/bin/cmp +++ b/bin/cmp @@ -118,18 +118,8 @@ if ($stat1[ST_SIZE] == 0 || $stat2[ST_SIZE] == 0) { } } -if (@ARGV) { - $skip1 = shift; - usage() unless $skip1 =~ /^(0x[A-F\d]+|\d+)$/; # exits; -} - -if (@ARGV) { - $skip2 = shift; - usage() unless $skip2 =~ /^(0x[A-F\d]+|\d+)$/; # exits; -} - -$skip1 = eval "$skip1" if defined $skip1; -$skip2 = eval "$skip2" if defined $skip2; +$skip1 = skipnum(shift) if @ARGV; +$skip2 = skipnum(shift) if @ARGV; if( -d $file1 ) { warn( "$Program: $file1 is a directory\n" ); @@ -201,6 +191,14 @@ close FILE2; exit $saw_difference; +sub skipnum { + my $n = shift; + return hex($n) if ($n =~ m/\A0x/); + return int($n) if ($n =~ m/\A\d+\Z/); + warn "$Program: invalid offset number '$n'\n"; + usage(); +} + sub usage { warn "usage: $Program [-l] [-s] file1 file2 [skip1 [skip2]]\n"; exit EX_USAGE; From c42f5c8293b97a1ccc8043ee159eaeb4c90316b9 Mon Sep 17 00:00:00 2001 From: brian d foy Date: Fri, 3 Nov 2023 18:06:29 -0400 Subject: [PATCH 2/2] Restrict to decimal digits \d is locale-dependent and matches much more than we want. --- bin/cmp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/cmp b/bin/cmp index 59aca4a6..33746d75 100755 --- a/bin/cmp +++ b/bin/cmp @@ -194,7 +194,7 @@ exit $saw_difference; sub skipnum { my $n = shift; return hex($n) if ($n =~ m/\A0x/); - return int($n) if ($n =~ m/\A\d+\Z/); + return int($n) if ($n =~ m/\A[0-9]+\z/); warn "$Program: invalid offset number '$n'\n"; usage(); }