public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
* [PATCH] test: Fix printf error when debug is enabled
@ 2025-09-29  6:30 Yumei Huang
  2025-09-29  6:41 ` David Gibson
  2025-09-29 22:24 ` Stefano Brivio
  0 siblings, 2 replies; 6+ messages in thread
From: Yumei Huang @ 2025-09-29  6:30 UTC (permalink / raw)
  To: passt-dev, sbrivio; +Cc: david, yuhuang

Running test pasta/tcp with debug enabled would get stuck with
below error:

  + printf 'DEBUG: ns     socat -u OPEN:__BASEPATH__/big.bin TCP6:[__GW6__%__IFNAME__]:10003\n'
  lib/term: line 38: printf: `_': invalid format character

The error occurs because printf interprets the % character as the
start of a format specifier, and the following '_' isn't one of
them.

Fix it by replacing 'printf "${*}\n"' with 'printf "%s\n" "$*"'.
Also update the docstring.

Link: https://bugs.passt.top/show_bug.cgi?id=154
Fixes: de28c20d8051 ("test: Update lib/term for clearer output when DEBUG is enabled")
Signed-off-by: Yumei Huang <yuhuang@redhat.com>
---
 test/lib/term | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/test/lib/term b/test/lib/term
index 6400746..2293474 100755
--- a/test/lib/term
+++ b/test/lib/term
@@ -29,32 +29,32 @@ PR_NC='\033[0m'
 PR_DELAY_INIT=100 # ms
 
 # info() - Highlight test log pane, print message to it and to log file
-# $@:	Message to print
+# $*:	Message to print
 info() {
 	tmux select-pane -t ${PANE_INFO}
-	printf "${*}\n" >> $STATEBASE/log_pipe
-	printf "${*}\n" >> "${LOGFILE}"
+	printf "%s\n" "$*" >> $STATEBASE/log_pipe
+	printf "%s\n" "$*" >> "${LOGFILE}"
 }
 
 # info_n() - Highlight, print message to pane and to log file without newline
-# $@:	Message to print
+# $*:	Message to print
 info_n() {
 	tmux select-pane -t ${PANE_INFO}
-	printf "${*}" >> $STATEBASE/log_pipe
-	printf "${*}" >> "${LOGFILE}"
+	printf "%s\n" "$*" >> $STATEBASE/log_pipe
+	printf "%s\n" "$*" >> "${LOGFILE}"
 }
 
 # info_nolog() - Highlight test log pane, print message to it
-# $@:	Message to print
+# $*:	Message to print
 info_nolog() {
 	tmux select-pane -t ${PANE_INFO}
-	printf "${*}\n" >> $STATEBASE/log_pipe
+	printf "%s\n" "$*" >> $STATEBASE/log_pipe
 }
 
 # info_nolog() - Print message to log file
-# $@:	Message to print
+# $*:	Message to print
 log() {
-	printf "${*}\n" >> "${LOGFILE}"
+	printf "%s\n" "$*" >> "${LOGFILE}"
 }
 
 # info_nolog_n() - Send message to pane without highlighting it, without newline
@@ -363,8 +363,8 @@ status_test_start() {
 info_check() {
 	switch_pane ${PANE_INFO}
 
-	printf "${PR_YELLOW}?${PR_NC} ${*}" >> $STATEBASE/log_pipe
-	printf "? ${*}" >> "${LOGFILE}"
+	printf "%s" "${PR_YELLOW}?${PR_NC} ${*}" >> $STATEBASE/log_pipe
+	printf "? %s" "${*}" >> "${LOGFILE}"
 }
 
 # info_check_passed() - Display and log a new line when a check passes
-- 
2.47.0


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] test: Fix printf error when debug is enabled
  2025-09-29  6:30 [PATCH] test: Fix printf error when debug is enabled Yumei Huang
@ 2025-09-29  6:41 ` David Gibson
  2025-09-29 22:24 ` Stefano Brivio
  1 sibling, 0 replies; 6+ messages in thread
From: David Gibson @ 2025-09-29  6:41 UTC (permalink / raw)
  To: Yumei Huang; +Cc: passt-dev, sbrivio

[-- Attachment #1: Type: text/plain, Size: 3124 bytes --]

On Mon, Sep 29, 2025 at 02:30:14PM +0800, Yumei Huang wrote:
> Running test pasta/tcp with debug enabled would get stuck with
> below error:
> 
>   + printf 'DEBUG: ns     socat -u OPEN:__BASEPATH__/big.bin TCP6:[__GW6__%__IFNAME__]:10003\n'
>   lib/term: line 38: printf: `_': invalid format character
> 
> The error occurs because printf interprets the % character as the
> start of a format specifier, and the following '_' isn't one of
> them.
> 
> Fix it by replacing 'printf "${*}\n"' with 'printf "%s\n" "$*"'.
> Also update the docstring.
> 
> Link: https://bugs.passt.top/show_bug.cgi?id=154
> Fixes: de28c20d8051 ("test: Update lib/term for clearer output when DEBUG is enabled")
> Signed-off-by: Yumei Huang <yuhuang@redhat.com>

Reviewed-by: David Gibson <david@gibson.dropbear.id.au>

I'm a little embarrassed I didn't spot this gotcha when I suggested
the initial change.

> ---
>  test/lib/term | 24 ++++++++++++------------
>  1 file changed, 12 insertions(+), 12 deletions(-)
> 
> diff --git a/test/lib/term b/test/lib/term
> index 6400746..2293474 100755
> --- a/test/lib/term
> +++ b/test/lib/term
> @@ -29,32 +29,32 @@ PR_NC='\033[0m'
>  PR_DELAY_INIT=100 # ms
>  
>  # info() - Highlight test log pane, print message to it and to log file
> -# $@:	Message to print
> +# $*:	Message to print
>  info() {
>  	tmux select-pane -t ${PANE_INFO}
> -	printf "${*}\n" >> $STATEBASE/log_pipe
> -	printf "${*}\n" >> "${LOGFILE}"
> +	printf "%s\n" "$*" >> $STATEBASE/log_pipe
> +	printf "%s\n" "$*" >> "${LOGFILE}"
>  }
>  
>  # info_n() - Highlight, print message to pane and to log file without newline
> -# $@:	Message to print
> +# $*:	Message to print
>  info_n() {
>  	tmux select-pane -t ${PANE_INFO}
> -	printf "${*}" >> $STATEBASE/log_pipe
> -	printf "${*}" >> "${LOGFILE}"
> +	printf "%s\n" "$*" >> $STATEBASE/log_pipe
> +	printf "%s\n" "$*" >> "${LOGFILE}"
>  }
>  
>  # info_nolog() - Highlight test log pane, print message to it
> -# $@:	Message to print
> +# $*:	Message to print
>  info_nolog() {
>  	tmux select-pane -t ${PANE_INFO}
> -	printf "${*}\n" >> $STATEBASE/log_pipe
> +	printf "%s\n" "$*" >> $STATEBASE/log_pipe
>  }
>  
>  # info_nolog() - Print message to log file
> -# $@:	Message to print
> +# $*:	Message to print
>  log() {
> -	printf "${*}\n" >> "${LOGFILE}"
> +	printf "%s\n" "$*" >> "${LOGFILE}"
>  }
>  
>  # info_nolog_n() - Send message to pane without highlighting it, without newline
> @@ -363,8 +363,8 @@ status_test_start() {
>  info_check() {
>  	switch_pane ${PANE_INFO}
>  
> -	printf "${PR_YELLOW}?${PR_NC} ${*}" >> $STATEBASE/log_pipe
> -	printf "? ${*}" >> "${LOGFILE}"
> +	printf "%s" "${PR_YELLOW}?${PR_NC} ${*}" >> $STATEBASE/log_pipe
> +	printf "? %s" "${*}" >> "${LOGFILE}"
>  }
>  
>  # info_check_passed() - Display and log a new line when a check passes
> -- 
> 2.47.0
> 

-- 
David Gibson (he or they)	| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
				| around.
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] test: Fix printf error when debug is enabled
  2025-09-29  6:30 [PATCH] test: Fix printf error when debug is enabled Yumei Huang
  2025-09-29  6:41 ` David Gibson
@ 2025-09-29 22:24 ` Stefano Brivio
  2025-09-30  0:23   ` David Gibson
  1 sibling, 1 reply; 6+ messages in thread
From: Stefano Brivio @ 2025-09-29 22:24 UTC (permalink / raw)
  To: Yumei Huang; +Cc: passt-dev, david

On Mon, 29 Sep 2025 14:30:14 +0800
Yumei Huang <yuhuang@redhat.com> wrote:

> Running test pasta/tcp with debug enabled would get stuck with
> below error:
> 
>   + printf 'DEBUG: ns     socat -u OPEN:__BASEPATH__/big.bin TCP6:[__GW6__%__IFNAME__]:10003\n'
>   lib/term: line 38: printf: `_': invalid format character
> 
> The error occurs because printf interprets the % character as the
> start of a format specifier, and the following '_' isn't one of
> them.
> 
> Fix it by replacing 'printf "${*}\n"' with 'printf "%s\n" "$*"'.

I'm not sure why, but this breaks colour highlights for me. Instead of
seeing a part of this message in blue, now I get:

  Test layout: \033[1;34msingle passt instance with guest\033[0m.\n

I can look into it if it only happens for me, or if needed.

By the way (I'm not sure if it's related), I used curly brackets
everywhere to unambiguously distinguish names of variables, and '*' is
a name.

The curly brackets are almost always optional, see POSIX.1-2024 for the
cases where they are needed:

  https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02

...to avoid possible issues, I just used them everywhere, and I think
it would be good to keep this consistent. That is, the $* should remain
as ${*}.

-- 
Stefano


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] test: Fix printf error when debug is enabled
  2025-09-29 22:24 ` Stefano Brivio
@ 2025-09-30  0:23   ` David Gibson
  2025-09-30  1:32     ` Yumei Huang
  2025-09-30 11:13     ` Stefano Brivio
  0 siblings, 2 replies; 6+ messages in thread
From: David Gibson @ 2025-09-30  0:23 UTC (permalink / raw)
  To: Stefano Brivio; +Cc: Yumei Huang, passt-dev

[-- Attachment #1: Type: text/plain, Size: 1660 bytes --]

On Tue, Sep 30, 2025 at 12:24:17AM +0200, Stefano Brivio wrote:
> On Mon, 29 Sep 2025 14:30:14 +0800
> Yumei Huang <yuhuang@redhat.com> wrote:
> 
> > Running test pasta/tcp with debug enabled would get stuck with
> > below error:
> > 
> >   + printf 'DEBUG: ns     socat -u OPEN:__BASEPATH__/big.bin TCP6:[__GW6__%__IFNAME__]:10003\n'
> >   lib/term: line 38: printf: `_': invalid format character
> > 
> > The error occurs because printf interprets the % character as the
> > start of a format specifier, and the following '_' isn't one of
> > them.
> > 
> > Fix it by replacing 'printf "${*}\n"' with 'printf "%s\n" "$*"'.
> 
> I'm not sure why, but this breaks colour highlights for me. Instead of
> seeing a part of this message in blue, now I get:
> 
>   Test layout: \033[1;34msingle passt instance with guest\033[0m.\n
> 
> I can look into it if it only happens for me, or if needed.

Oh, that's interesting.  I believe the colouring has been broken
forever on Fedora, but I never got around to looking into it.
Figuring this out might reveal why.  Maybe.

I realised I think I know why: those colour codes rely on turning
"\033" into a terminal escape.  printf(1) will do that in the format
string, but not in %s parameters

	$ printf "a\tb%sc\n " "\t"
	a	b\tc

We can fix this either by moving the escape codes back into the format
string, or using %b instead of %s, which explicitly interprets string
escape codes.

-- 
David Gibson (he or they)	| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you, not the other way
				| around.
http://www.ozlabs.org/~dgibson

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] test: Fix printf error when debug is enabled
  2025-09-30  0:23   ` David Gibson
@ 2025-09-30  1:32     ` Yumei Huang
  2025-09-30 11:13     ` Stefano Brivio
  1 sibling, 0 replies; 6+ messages in thread
From: Yumei Huang @ 2025-09-30  1:32 UTC (permalink / raw)
  To: David Gibson; +Cc: Stefano Brivio, passt-dev

On Tue, Sep 30, 2025 at 8:23 AM David Gibson
<david@gibson.dropbear.id.au> wrote:
>
> On Tue, Sep 30, 2025 at 12:24:17AM +0200, Stefano Brivio wrote:
> > On Mon, 29 Sep 2025 14:30:14 +0800
> > Yumei Huang <yuhuang@redhat.com> wrote:
> >
> > > Running test pasta/tcp with debug enabled would get stuck with
> > > below error:
> > >
> > >   + printf 'DEBUG: ns     socat -u OPEN:__BASEPATH__/big.bin TCP6:[__GW6__%__IFNAME__]:10003\n'
> > >   lib/term: line 38: printf: `_': invalid format character
> > >
> > > The error occurs because printf interprets the % character as the
> > > start of a format specifier, and the following '_' isn't one of
> > > them.
> > >
> > > Fix it by replacing 'printf "${*}\n"' with 'printf "%s\n" "$*"'.
> >
> > I'm not sure why, but this breaks colour highlights for me. Instead of
> > seeing a part of this message in blue, now I get:
> >
> >   Test layout: \033[1;34msingle passt instance with guest\033[0m.\n
> >
> > I can look into it if it only happens for me, or if needed.
>
> Oh, that's interesting.  I believe the colouring has been broken
> forever on Fedora, but I never got around to looking into it.

Well, the color worked fine on fedora before I changed to using "%s".

> Figuring this out might reveal why.  Maybe.
>
> I realised I think I know why: those colour codes rely on turning
> "\033" into a terminal escape.  printf(1) will do that in the format
> string, but not in %s parameters
>
>         $ printf "a\tb%sc\n " "\t"
>         a       b\tc
>
> We can fix this either by moving the escape codes back into the format
> string, or using %b instead of %s, which explicitly interprets string
> escape codes.

Yes, %b fixes this.

I will send v2 to update it and the brackets. Thanks.

>
> --
> David Gibson (he or they)       | I'll have my music baroque, and my code
> david AT gibson.dropbear.id.au  | minimalist, thank you, not the other way
>                                 | around.
> http://www.ozlabs.org/~dgibson



-- 
Thanks,

Yumei Huang


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] test: Fix printf error when debug is enabled
  2025-09-30  0:23   ` David Gibson
  2025-09-30  1:32     ` Yumei Huang
@ 2025-09-30 11:13     ` Stefano Brivio
  1 sibling, 0 replies; 6+ messages in thread
From: Stefano Brivio @ 2025-09-30 11:13 UTC (permalink / raw)
  To: David Gibson; +Cc: Yumei Huang, passt-dev

On Tue, 30 Sep 2025 10:23:39 +1000
David Gibson <david@gibson.dropbear.id.au> wrote:

> On Tue, Sep 30, 2025 at 12:24:17AM +0200, Stefano Brivio wrote:
> > On Mon, 29 Sep 2025 14:30:14 +0800
> > Yumei Huang <yuhuang@redhat.com> wrote:
> >   
> > > Running test pasta/tcp with debug enabled would get stuck with
> > > below error:
> > > 
> > >   + printf 'DEBUG: ns     socat -u OPEN:__BASEPATH__/big.bin TCP6:[__GW6__%__IFNAME__]:10003\n'
> > >   lib/term: line 38: printf: `_': invalid format character
> > > 
> > > The error occurs because printf interprets the % character as the
> > > start of a format specifier, and the following '_' isn't one of
> > > them.
> > > 
> > > Fix it by replacing 'printf "${*}\n"' with 'printf "%s\n" "$*"'.  
> > 
> > I'm not sure why, but this breaks colour highlights for me. Instead of
> > seeing a part of this message in blue, now I get:
> > 
> >   Test layout: \033[1;34msingle passt instance with guest\033[0m.\n
> > 
> > I can look into it if it only happens for me, or if needed.  
> 
> Oh, that's interesting.  I believe the colouring has been broken
> forever on Fedora, but I never got around to looking into it.
> Figuring this out might reveal why.  Maybe.
> 
> I realised I think I know why: those colour codes rely on turning
> "\033" into a terminal escape.  printf(1) will do that in the format
> string, but not in %s parameters
> 
> 	$ printf "a\tb%sc\n " "\t"
> 	a	b\tc
> 
> We can fix this either by moving the escape codes back into the format
> string, or using %b instead of %s, which explicitly interprets string
> escape codes.

Oh, I didn't know about %b, nice. It's also in POSIX.1-2024, see point
7. here:

  https://pubs.opengroup.org/onlinepubs/9799919799/utilities/printf.html#tag_20_96_13

...side note for Yumei: the whole test suite _should_ be POSIX shell,
that is, it doesn't rely on specific shell (e.g. Bash) implementations.

For example, on Debian, Ubuntu, and others, the default shell for
non-interactive usage (not what you have on terminal, but what executes
scripts) is dash:

  https://wiki.ubuntu.com/DashAsBinSh
  https://wiki.archlinux.org/title/Dash

So, if it's described in the POSIX specification (the one I just
linked) it will almost certainly work everywhere. If not, it might
break (but not necessarily).

-- 
Stefano


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-09-30 11:13 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-09-29  6:30 [PATCH] test: Fix printf error when debug is enabled Yumei Huang
2025-09-29  6:41 ` David Gibson
2025-09-29 22:24 ` Stefano Brivio
2025-09-30  0:23   ` David Gibson
2025-09-30  1:32     ` Yumei Huang
2025-09-30 11:13     ` Stefano Brivio

Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).