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

Re: Bug: git-rebase goofs up \n in commit messages

From
Jeff King <peff@peff.net>
Date
May 26, 2007, 03:42 UTC
Message-ID
<20070526034236.GA18169@coredump.intra.peff.net>
In-Reply-To
<20070526011036.GA4169@gondor.apana.org.au>
On Sat, May 26, 2007 at 11:10:36AM +1000, Herbert Xu wrote:
> If you need to echo something that may have escapes in it, the portable
> way to do it is
> 
> printf '%s\n' "$test"

Ah, I see. I had thought the problem was coming from some dash interpolation magic, but yes, it's just echo doing the conversion. And POSIX is very clear that this is an implementation defined behavior. Thanks very much for the response, Herbert.

Junio, patch is below. I have no idea how prevalent this issue is within our scripts, but this at least fixes the reported bug.

-- >8 -- git-am: use printf instead of echo on user-supplied strings

Under some implementations of echo (such as that provided by dash), backslash escapes are recognized without any other options. This means that echo-ing user-supplied strings may cause any backslash sequences in them to be converted. Using printf resolves the ambiguity.

This bug can be seen when using git-am to apply a patch whose subject contains the character sequence "\n"; the characters are converted to a literal newline. Noticed by Szekeres Istvan.

Signed-off-by: Jeff King <peff@peff.net>
---
 git-am.sh |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/git-am.sh b/git-am.sh
index c9f66e2..543efd0 100755
--- a/git-am.sh
+++ b/git-am.sh
@@ -331,7 +331,7 @@ do
 		ADD_SIGNOFF=
 	    fi
 	    {
-		echo "$SUBJECT"
+		printf '%s\n' "$SUBJECT"
 		if test -s "$dotest/msg-clean"
 		then
 			echo
@@ -394,7 +394,7 @@ do
 	fi
 
 	echo
-	echo "Applying '$SUBJECT'"
+	printf 'Applying %s\n' "$SUBJECT"
 	echo
 
 	case "$resolved" in
-- 
1.5.2.818.g9b59-dirty
Previous: Herbert XuNext: Junio C Hamano
Message 4 of 13 in “Bug: git-rebase goofs up \n in commit messages”
  1. Szekeres IstvanMay 25, 2007
  2. Jeff KingMay 26, 2007
  3. Herbert XuMay 26, 2007
  4. Jeff KingMay 26, 2007
  5. Junio C HamanoMay 26, 2007
  6. Junio C HamanoMay 26, 2007
  7. Jeff KingMay 26, 2007
  8. Junio C HamanoMay 26, 2007
  9. Herbert XuMay 26, 2007
  10. Jeff KingMay 26, 2007
  11. Herbert XuMay 26, 2007
  12. Jeff KingMay 26, 2007
  13. Junio C HamanoMay 26, 2007

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.