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

2 messages from 2012-02-25 to 2012-02-26. Participants: Jim Meyering, Junio C Hamano.
Thread: https://gitlist.dev/t/29736

## Jim Meyering, 2012-02-25 17:34

Subject: [PATCH] am: don't infloop for an empty input file
Message-ID: <87399y24wt.fsf@rho.meyering.net>
URL: https://gitlist.dev/e/87399y24wt.fsf%40rho.meyering.net

```
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(-)

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, 2012-02-26 23:58

Subject: Re: [PATCH] am: don't infloop for an empty input file
Message-ID: <7vty2dcflb.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vty2dcflb.fsf%40alter.siamese.dyndns.org
In-Reply-To: <87399y24wt.fsf@rho.meyering.net>

```
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.

```
