Skip to content

Commit 513e3d0

Browse files
authored
patch: -D argument validation
* When patching a file with -D option (aka --ifdef), "-Did" and "-D id" usages are accepted * I found that an empty string argument is sometimes ignored, and sometimes raises an error * The OpenBSD version raises an error if the argument to -D is not a valid identifier, i.e. starts with alpha followed by alphanumeric & underscore * Add validation in this version; as a benefit the invalid empty string usage is rejected * test1: perl patch -D'' ---> already caught by Getopt::Long * test2: perl patch -D '' file ---> now errors; previously ignored * test3: perl patch -DValid_id0 file ---> ifdef name accepted as before * test4: perl patch -D 'd/e;f~ine#' file ---> now errors; previously accepted
1 parent 7bbdcbd commit 513e3d0

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

bin/patch

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ sub type_conflict {
5454
return;
5555
}
5656

57+
sub ifdef_id_check {
58+
my $id = shift;
59+
return if (length($id) != 0 && $id =~ m/\A[A-Za-z]\w*\Z/);
60+
warn "$0: argument to -D is not an identifier\n";
61+
exit EX_FAILURE;
62+
}
63+
5764
my ($patchfile, @options);
5865

5966
if (@ARGV) {
@@ -93,6 +100,7 @@ if (@ARGV) {
93100
my %opts;
94101
Getopt::Long::GetOptions(\%opts, @desc) or die("Bad options\n");
95102
type_conflict(\%opts);
103+
ifdef_id_check($opts{'ifdef'}) if defined $opts{'ifdef'};
96104
$opts{origfile} = shift;
97105
$_ = \%opts;
98106
$patchfile = shift unless $next++;

0 commit comments

Comments
 (0)