Skip to content

Commit

Permalink
patch: segfault fix. Closes 7916
Browse files Browse the repository at this point in the history
Fix segfault on this case (malformed --- line):

    -- dwarves.orig 2015-02-25 01:45:27.753000000 +0000
    +++ dwarves     2015-02-25 01:46:08.199000000 +0000
    @@ -1,7 +1,7 @@
     Bashful
     Doc
     Dopey
    -Grouchy
    +Grumpy
     Happy
     Sleepy
     Sneezy

function                                             old     new   delta
patch_main                                          1903    1957     +54

Signed-off-by: Denys Vlasenko <[email protected]>
  • Loading branch information
Denys Vlasenko committed Mar 11, 2015
1 parent 7b434a6 commit fe8b580
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions editors/patch.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,8 @@ static int apply_one_hunk(void)
// state 1: Found +++ file indicator, look for @@
// state 2: In hunk: counting initial context lines
// state 3: In hunk: getting body
// Like GNU patch, we don't require a --- line before the +++, and
// also allow the --- after the +++ line.

int patch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int patch_main(int argc UNUSED_PARAM, char **argv)
Expand Down Expand Up @@ -462,6 +464,14 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
TT.context = 0;
state = 2;

// If the --- line is missing or malformed, either oldname
// or (for -R) newname could be NULL -- but not both. Like
// GNU patch, proceed based on the +++ line, and avoid SEGVs.
if (!oldname)
oldname = xstrdup("MISSING_FILENAME");
if (!newname)
newname = xstrdup("MISSING_FILENAME");

// If this is the first hunk, open the file.
if (TT.filein == -1) {
int oldsum, newsum, empty = 0;
Expand Down

0 comments on commit fe8b580

Please sign in to comment.