apply: reject a hunk that does not do anything
authorJunio C Hamano <gitster@pobox.com>
Mon, 1 Jun 2015 19:12:04 +0000 (12:12 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 1 Jun 2015 19:12:04 +0000 (12:12 -0700)
A hunk like this in a hand-edited patch without correctly adjusting
the line counts:

@@ -660,2 +660,2 @@ inline struct sk_buff *ieee80211_authentic...
auth = (struct ieee80211_authentication *)
skb_put(skb, sizeof(struct ieee80211_authentication));
- some old text
+ some new text
--
2.1.0

dev mailing list

at the end of the input does not have a good way for us to diagnose
it as a corrupt patch. We just read two context lines and discard
the remainder as cruft, which we must do in order to ignore the
e-mail footer. Notice that the patch does not change anything and
signal an error.

Note that this fix will not help if the hand-edited hunk header were
"@@ -660,3, +660,2" to include the removal. We would just remove
the old text without adding the new one, and treat "+ some new text"
and everything after that line as trailing cruft. So it is dubious
that this patch alone would help very much in practice, but it may
be better than nothing.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/apply.c
t/t4136-apply-check.sh
index 6696ea4c3ffd4f7af04f3f56609a78a74a0a0b55..606eddd5f6dcd6b3982f9acc6eb6f93eee235649 100644 (file)
@@ -1639,6 +1639,9 @@ static int parse_fragment(const char *line, unsigned long size,
        }
        if (oldlines || newlines)
                return -1;
+       if (!deleted && !added)
+               return -1;
+
        fragment->leading = leading;
        fragment->trailing = trailing;
 
index a321f7c245da531cb8fea495a5e7ba02b3a6463b..4b0a374b63f4128405c88fdb2410fb679bdd06a1 100755 (executable)
@@ -16,4 +16,17 @@ test_expect_success 'apply --check exits non-zero with unrecognized input' '
        EOF
 '
 
+test_expect_success 'apply exits non-zero with no-op patch' '
+       cat >input <<-\EOF &&
+       diff --get a/1 b/1
+       index 6696ea4..606eddd 100644
+       --- a/1
+       +++ b/1
+       @@ -1,1 +1,1 @@
+        1
+       EOF
+       test_must_fail git apply --stat input &&
+       test_must_fail git apply --check input
+'
+
 test_done