git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Jim Meyering <jim@meyering.net>
Date
Feb 25, 2012, 17:34 UTC
Message-ID
<87399y24wt.fsf@rho.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
Next: Junio C Hamano
Message 1 of 2 in “am: don't infloop for an empty input file”
  1. am: don't infloop for an empty input fileJim Meyering, Feb 25, 2012
  2. Junio C HamanoFeb 26, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.