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

Re: [PATCH v3] test-lib.sh: do not "echo" externally supplied strings

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 18, 2014, 18:47 UTC
Message-ID
<xmqqvbvb8ht7.fsf@gitster.dls.corp.google.com>
In-Reply-To
<20140318001410.GA4813@ibr.ch>
Uwe Storbeck <uwe@ibr.ch> writes:
Show 11 quoted lines
> In some places we "echo" a string that is supplied by the calling
> test script and may contain backslash sequences. The echo command
> of some shells, most notably "dash", interprets these backslash
> sequences (POSIX.1 allows this) which may scramble the test
> output.
>
> Signed-off-by: Uwe Storbeck <uwe@ibr.ch>
> ---
>
> Commit message rewritten to avoid title continuation in the body.
> Thanks Junio C Hamano for the hint.

Here is what I queued yesterday. I was wrong to say "control characters"; a backslash sequence is not necessarily a control character (e.g. \c at the end that suppresses the final LF), so I'll replace it with your version.

The test titles are not externally supplied but under our control, so it is less problematic than the "rebase -i" case where we do get bitten by user supplied commit title string. Still it is a good idea to apply this change to protect ourselves.

Thanks.  
commit 215cd588caebe86fe77115bdda97930b4659aecd
Author: Uwe Storbeck <uwe@ibr.ch>
Date:   Sat Mar 15 00:57:36 2014 +0100
    test-lib.sh: do not "echo" test titles
    
    The test title could be a string with backslash in it, and can be
    interpreted as control characters by the echo command of some shells
    (e.g. dash).
    
    Signed-off-by: Uwe Storbeck <uwe@ibr.ch>
    Signed-off-by: Junio C Hamano <gitster@pobox.com>
diff --git a/t/test-lib.sh b/t/test-lib.sh
index 1531c24..3c7cb1d 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -277,7 +277,7 @@ error "Test script did not set test_description."
 
 if test "$help" = "t"
 then
-	echo "$test_description"
+	printf '%s\n' "$test_description"
 	exit 0
 fi
 
@@ -328,7 +328,7 @@ test_failure_ () {
 	test_failure=$(($test_failure + 1))
 	say_color error "not ok $test_count - $1"
 	shift
-	echo "$@" | sed -e 's/^/#	/'
+	printf '%s\n' "$*" | sed -e 's/^/#	/'
 	test "$immediate" = "" || { GIT_EXIT_OK=t; exit 1; }
 }
Previous: Uwe Storbeck
Message 14 of 14 in “test-lib.sh: use printf instead of echo”
  1. test-lib.sh: use printf instead of echoUwe Storbeck, Mar 14, 2014
  2. Jonathan NiederMar 15, 2014
  3. Junio C HamanoMar 17, 2014
  4. Jonathan NiederMar 18, 2014
  5. Junio C HamanoMar 19, 2014
  6. David KastrupMar 19, 2014
  7. Junio C HamanoMar 19, 2014
  8. Jonathan NiederMar 20, 2014
  9. Junio C HamanoMar 20, 2014
  10. Johannes SixtMar 15, 2014
  11. Uwe StorbeckMar 17, 2014
  12. test-lib.sh: use printf instead of echoUwe Storbeck, Mar 17, 2014
  13. test-lib.sh: do not "echo" externally supplied stringsUwe Storbeck, Mar 18, 2014
  14. Junio C HamanoMar 18, 2014

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.