threads / patch / 29736

patcham: don't infloop for an empty input file

Subject: [PATCH] am: don't infloop for an empty input file

## tl;dr

2 messages between Feb 25, 2012 and Feb 26, 2012. Diffs are folded; open one to read it.

replies: 1people: 2as markdown or json

Jim Meyering· Feb 25, 2012, 17:34 UTC · lore

Today, "git am" surprised me. I mistakenly ran it on an empty file and it went into an infinite loop.

  : > e && git am e

To fix it, I made a failing bourne shell "read" break out of the offending loop. Looking through git-am.sh for other instances, I did find one, but didn't try to address it here.

	    action=again
	    while test "$action" = again
	    do
		gettextln "Commit Body is:"
		echo "--------------------------"
		cat "$dotest/final-commit"
		echo "--------------------------"
		# TRANSLATORS: Make sure to include [y], [n], [e], [v] and [a]
		# in your translation. The program will only accept English
		# input at this point.
		gettext "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
may infloop->	read reply
		case "$reply" in

In that case (when someone hits ^D in response to that prompt?), you may want to exit altogether.

In the test addition, I didn't try to handle potentially-inflooping code. In coreutils tests, it's easy (since it includes the timeout program): I would just prefix the command with something like "timeout 10", but the timeout command is not universally available. And besides, the infloop is supposed to be fixed, now.

Here's a patch for the infloop I triggered:
[Noticed with and tested against master v1.7.9.2-262-gba998d3,
 but seems to apply also to maint. ]
-- >8 --

git-am.sh's check_patch_format function would attempt to preview the patch to guess its format, but would go into an infinite loop when the patch file happened to be empty. The solution: exit the loop when "read" fails, not when the line var, "$l1" becomes empty.

Signed-off-by: Jim Meyering <meyering@redhat.com>
---
 git-am.sh     |    2 +-
 t/t4150-am.sh |   10 ++++++++++
 2 files changed, 11 insertions(+), 1 deletion(-)
Show changes to 2 files +11 −2

git-am.sh, t/t4150-am.sh

diff --git a/git-am.sh b/git-am.sh
index 64d8e2a..906f91f 100755
--- a/git-am.sh
+++ b/git-am.sh
@@ -202,7 +202,7 @@ check_patch_format () {
 		l1=
 		while test -z "$l1"
 		do
-			read l1
+			read l1 || break
 		done
 		read l2
 		read l3
diff --git a/t/t4150-am.sh b/t/t4150-am.sh
index f1b60b8..6f77fff 100755
--- a/t/t4150-am.sh
+++ b/t/t4150-am.sh
@@ -505,4 +505,14 @@ test_expect_success 'am -q is quiet' '
 	! test -s output.out
 '

+test_expect_success 'am empty-file does not infloop' '
+	rm -fr .git/rebase-apply &&
+	git reset --hard &&
+	touch empty-file &&
+	test_tick &&
+	{ git am empty-file > actual 2>&1 && false || :; } &&
+	echo Patch format detection failed. >expected &&
+	test_cmp expected actual
+'
+
 test_done
--
1.7.9.2.263.g9be8b7
Junio C Hamano· Feb 26, 2012, 23:58 UTC · re: Jim Meyering · lore

Re: [PATCH] am: don't infloop for an empty input file

Jim Meyering <jim@meyering.net> writes:
> Today, "git am" surprised me.
> I mistakenly ran it on an empty file and it went into an infinite loop.
Yeah, that is an embarrassing regression in 1.7.7.
Thanks for noticing.

← back to recent threads