patch: fix segfault against unified diffs which start line is zero stable
authorYuya Nishihara <yuya@tcha.org>
Sat, 12 May 2012 16:10:01 +0900
branchstable
changeset 16650 fcb97d9a26cd
parent 16649 822e75386c16
child 16651 9e40bc4c1bde
patch: fix segfault against unified diffs which start line is zero Since 2b1ec74c961f, if a chunk starts with "@@ -0,1", oldstart turns into a negative value. Because diffhelpers.testhunk() doesn't expect negative bstart, it bypasses "alen > blen - bstart" condition and segfaults at "PyList_GET_ITEM(b, i + bstart)".
mercurial/patch.py
tests/test-import.t
--- a/mercurial/patch.py	Fri May 11 22:48:19 2012 -0700
+++ b/mercurial/patch.py	Sat May 12 16:10:01 2012 +0900
@@ -1014,9 +1014,9 @@
         oldstart = self.starta + top
         newstart = self.startb + top
         # zero length hunk ranges already have their start decremented
-        if self.lena:
+        if self.lena and oldstart > 0:
             oldstart -= 1
-        if self.lenb:
+        if self.lenb and newstart > 0:
             newstart -= 1
         return old, oldstart, new, newstart
 
--- a/tests/test-import.t	Fri May 11 22:48:19 2012 -0700
+++ b/tests/test-import.t	Sat May 12 16:10:01 2012 +0900
@@ -997,6 +997,26 @@
   c3
   c4
 
+no segfault while importing a unified diff which start line is zero but chunk
+size is non-zero
+
+  $ hg init startlinezero
+  $ cd startlinezero
+  $ echo foo > foo
+  $ hg commit -Amfoo
+  adding foo
+
+  $ hg import --no-commit - << EOF
+  > diff a/foo b/foo
+  > --- a/foo
+  > +++ b/foo
+  > @@ -0,1 +0,1 @@
+  >  foo
+  > EOF
+  applying patch from stdin
+
+  $ cd ..
+
 Test corner case involving fuzz and skew
 
   $ hg init morecornercases