git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Kyle J. McKay" <mackyle@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: Git mailing list <git@vger.kernel.org>,
	Michael Blume <blume.mike@gmail.com>
Subject: [PATCH] test: add git apply whitespace expansion tests
Date: Sun, 18 Jan 2015 02:49:45 -0800	[thread overview]
Message-ID: <102e322e68e78e39a7c227f3f3e102c@74d39fa044aa309eaea14b9f57fe79c> (raw)
In-Reply-To: <xmqqzj9iz3gu.fsf_-_@gitster.dls.corp.google.com>

When git apply fixes whitespace, the result can end up being
longer than the initial text if whitespace ends up being expanded
(such as with the tab-in-indent option).

Since 250b3c6c (apply --whitespace=fix: avoid running over the
postimage buffer, 2013-03-22) an attempt has been made to compute
the correct final length in such a case.

These tests all stress the whitespace-expansion-during-apply
condition and can result in core dump failures when the final
length is not computed correctly.

Signed-off-by: Kyle J. McKay <mackyle@gmail.com>
---

* Here's some tests.  With "apply: make update_pre_post_images() sanity
  check the given postlen" but not "apply: count the size of postimage
  correctly" test 1/4 and 4/4 trigger the 'die("BUG: postlen...' but
  test 2/4 and 3/4 do not although they fail because git apply generates
  garbage.

* After applying "apply: count the size of postimage correctly" all 4
  tests pass whereas they all fail without that.  It's interesting that
  the "BUG: postlen" check does not trigger for 2/4 or 3/4 but the output
  is garbage in those cases without the fix.

* Theses tests can easily trigger core dumps.  It seems to depend on how
  the git binary was built and what exactly ends up getting stepped on as
  I have several different Git builds and some of them core dump on tests
  that others pass or just produce garbage for, but none of them passes
  2/4 or 3/4 without the "count postimage size correctly" fix.

 t/t4138-apply-ws-expansion.sh | 121 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 121 insertions(+)
 create mode 100755 t/t4138-apply-ws-expansion.sh

diff --git a/t/t4138-apply-ws-expansion.sh b/t/t4138-apply-ws-expansion.sh
new file mode 100755
index 00000000..140ed9ac
--- /dev/null
+++ b/t/t4138-apply-ws-expansion.sh
@@ -0,0 +1,121 @@
+#!/bin/sh
+#
+# Copyright (C) 2015 Kyle J. McKay
+#
+
+# NOTE: To facilitate separate testing, this script can be run
+# standalone to just create the test files and do nothing else
+# by first setting the environment variable MAKE_PATCHES=1.
+
+test_description='git apply test patches with whitespace expansion.'
+
+[ -n "$MAKE_PATCHES" ] || \
+. ./test-lib.sh
+
+#
+## create test-N, patchN.patch, expect-N files
+#
+
+# test 1
+printf '\t%s\n' 1 2 3 4 5 6 > before
+printf '\t%s\n' 1 2 3 > after
+printf '%64s\n' a b c $x >> after
+printf '\t%s\n' 4 5 6 >> after
+git diff --no-index before after | \
+sed -e 's/before/test-1/' -e 's/after/test-1/' > patch1.patch
+printf '%64s\n' 1 2 3 4 5 6 > test-1
+printf '%64s\n' 1 2 3 a b c 4 5 6 > expect-1
+
+# test 2
+printf '\t%s\n' a b c d e f > before
+printf '\t%s\n' a b c > after
+n=10
+x=1
+while [ $x -lt $n ]; do
+	printf '%63s%d\n' '' $x >> after
+	x=$(( $x + 1 ))
+done
+printf '\t%s\n' d e f >> after
+git diff --no-index before after | \
+sed -e 's/before/test-2/' -e 's/after/test-2/' > patch2.patch
+printf '%64s\n' a b c d e f > test-2
+printf '%64s\n' a b c > expect-2
+x=1
+while [ $x -lt $n ]; do
+	printf '%63s%d\n' '' $x >> expect-2
+	x=$(( $x + 1 ))
+done
+printf '%64s\n' d e f >> expect-2
+
+# test 3
+printf '\t%s\n' a b c d e f > before
+printf '\t%s\n' a b c > after
+n=100
+x=0
+while [ $x -lt $n ]; do
+	printf '%63s%02d\n' '' $x >> after
+	x=$(( $x + 1 ))
+done
+printf '\t%s\n' d e f >> after
+git diff --no-index before after | \
+sed -e 's/before/test-3/' -e 's/after/test-3/' > patch3.patch
+printf '%64s\n' a b c d e f > test-3
+printf '%64s\n' a b c > expect-3
+x=0
+while [ $x -lt $n ]; do
+	printf '%63s%02d\n' '' $x >> expect-3
+	x=$(( $x + 1 ))
+done
+printf '%64s\n' d e f >> expect-3
+
+# test 4
+> before
+x=0
+while [ $x -lt 50 ]; do
+	printf '\t%02d\n' $x >> before
+	x=$(( $x + 1 ))
+done
+cat before > after
+printf '%64s\n' a b c >> after
+while [ $x -lt 100 ]; do
+	printf '\t%02d\n' $x >> before
+	printf '\t%02d\n' $x >> after
+	x=$(( $x + 1 ))
+done
+git diff --no-index before after | \
+sed -e 's/before/test-4/' -e 's/after/test-4/' > patch4.patch
+> test-4
+x=0
+while [ $x -lt 50 ]; do
+	printf '%63s%02d\n' '' $x >> test-4
+	x=$(( $x + 1 ))
+done
+cat test-4 > expect-4
+printf '%64s\n' a b c >> expect-4
+while [ $x -lt 100 ]; do
+	printf '%63s%02d\n' '' $x >> test-4
+	printf '%63s%02d\n' '' $x >> expect-4
+	x=$(( $x + 1 ))
+done
+
+# cleanup
+rm before after
+
+[ -z "$MAKE_PATCHES" ] || exit 0
+
+#
+## Run the tests
+#
+
+# Note that `patch` can successfully apply all patches when run
+# with the --ignore-whitespace option.
+
+for t in 1 2 3 4; do
+	test_expect_success "apply with ws expansion (t=$t)" '
+		git -c core.whitespace=tab-in-indent,tabwidth=63 \
+			apply --whitespace=fix patch$t.patch &&
+		test_cmp test-$t expect-$t
+	'
+done
+
+test_done
--

  reply	other threads:[~2015-01-18 10:50 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-14 18:20 Segmentation fault in git apply Michael Blume
2015-01-14 18:40 ` Michael Blume
2015-01-14 18:44   ` Michael Blume
2015-01-14 18:48     ` Michael Blume
2015-01-14 18:58       ` Michael Blume
2015-01-14 19:09         ` Michael Blume
2015-01-15  8:26           ` Kyle J. McKay
2015-01-15  9:10             ` Kyle J. McKay
2015-01-16 19:58               ` Junio C Hamano
2015-01-16 23:54                 ` [PATCH] apply: count the size of postimage correctly Junio C Hamano
2015-01-18 10:49                   ` Kyle J. McKay [this message]
2015-01-18 22:11                     ` [PATCH] test: add git apply whitespace expansion tests Junio C Hamano
2015-01-19  3:54                       ` Kyle J. McKay
2015-01-21 22:33                         ` Junio C Hamano
2015-01-22  6:55                           ` Kyle J. McKay
2015-01-22 19:23                             ` Junio C Hamano
2015-01-23  0:12                               ` Kyle J. McKay
2015-01-22 22:58                           ` [PATCH v2 0/4] apply --whitespace=fix buffer corruption fix Junio C Hamano
2015-01-22 22:58                             ` [PATCH v2 1/4] apply.c: typofix Junio C Hamano
2015-01-22 23:17                               ` Stefan Beller
2015-01-22 23:42                                 ` Junio C Hamano
2015-01-22 23:48                                   ` Stefan Beller
2015-01-22 22:58                             ` [PATCH v2 2/4] apply: make update_pre_post_images() sanity check the given postlen Junio C Hamano
2015-01-22 22:58                             ` [PATCH v2 3/4] apply: count the size of postimage correctly Junio C Hamano
2015-01-22 22:58                             ` [PATCH v2 4/4] apply: detect and mark whitespace errors in context lines when fixing Junio C Hamano
2015-01-14 18:50 ` Segmentation fault in git apply Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=102e322e68e78e39a7c227f3f3e102c@74d39fa044aa309eaea14b9f57fe79c \
    --to=mackyle@gmail.com \
    --cc=blume.mike@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).