Skip to content

Commit fa977b3

Browse files
authored
patch: crash for invalid context diff input (#979)
* I generated a Context diff with 2 hunks * I manually swapped the 2 numbers at the top of hunk1, from "1,4" to "4,1" (the line number range for the hunk) * GNU patch correctly rejected this invalid diff, but this version crashed with an ARRAY error * Add validation for backwards range, based on code previously added for Normal diffs %cat diff.ctx *** id Wed Mar 5 09:16:33 2025 --- id2 Wed Mar 5 09:14:36 2025 *************** *** 4,1 **** - #!/usr/bin/perl =begin metadata --- 1,3 ---- *************** *** 12,24 **** =cut - # - # An implementation of the 'id' utility in Perl. Written for the Perl Power - # Tools (PPT) project by Theo Van Dinter (felicity@kluge.net). - # - # $Id: id,v 1.2 2004/08/05 14:17:43 cwest Exp $ - # - use strict; use File::Basename qw(basename); --- 11,16 ---- %perl -Mwarnings -Mdiagnostics patch -c id diff.ctx Hmm... Looks like a context diff to me... Patching file id using Plan A... Short hunk ignored...no second line range. 1 out of 1 hunks ignored--saving rejects to id.rej Can't use an undefined value as an ARRAY reference at patch line 382, <$in_fh> line 249 (#1) (F) A value used as either a hard reference or a symbolic reference must be a defined value. This helps to delurk some insidious errors. Uncaught exception from user code: Can't use an undefined value as an ARRAY reference at patch line 382, <$in_fh> line 249. Patch::bless(Patch::Context=HASH(0x55908a4f50)) called at patch line 161
1 parent db94949 commit fa977b3

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

bin/patch

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ while (<PATCH>) {
159159
my ($i_range, $i_start, $i_end, @i_hunk) = ($1, $2, $3 || $2);
160160
my ($o_range, $o_start, $o_end, @o_hunk);
161161
$patch->bless('context') or next PATCH;
162+
if ($i_start > $i_end) {
163+
$patch->note("Malformed input at line $.: $i_range\n");
164+
$patch->reject($i_range);
165+
last;
166+
}
162167
my $o_hunk = qr/^$space(--- (\d+)(?:,(\d+))? ----\n)/;
163168
my $re = qr/^$space([ !-] )/;
164169
$_ = <PATCH>;

0 commit comments

Comments
 (0)