From 82e5363634227131a75216895b29edf6f48dd55a Mon Sep 17 00:00:00 2001 From: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com> Date: Tue, 6 Aug 2024 16:41:35 -0400 Subject: [PATCH] fix: handle logging empty args in strings (#19324) Signed-off-by: Michael Crenshaw <350466+crenshaw-dev@users.noreply.github.com> --- util/exec/exec.go | 5 +++++ util/exec/exec_test.go | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/util/exec/exec.go b/util/exec/exec.go index 5bd7325695390..493d8855c8c80 100644 --- a/util/exec/exec.go +++ b/util/exec/exec.go @@ -66,6 +66,11 @@ func RunWithExecRunOpts(cmd *exec.Cmd, opts ExecRunOpts) (string, error) { func GetCommandArgsToLog(cmd *exec.Cmd) string { var argsToLog []string for _, arg := range cmd.Args { + if arg == "" { + argsToLog = append(argsToLog, `""`) + continue + } + containsSpace := false for _, r := range arg { if unicode.IsSpace(r) { diff --git a/util/exec/exec_test.go b/util/exec/exec_test.go index 6d99f70531a3f..a3ae4888f2cf4 100644 --- a/util/exec/exec_test.go +++ b/util/exec/exec_test.go @@ -71,6 +71,11 @@ func Test_getCommandArgsToLog(t *testing.T) { args: []string{"sh", "-c", `echo "hello world"`}, expected: `sh -c "echo \"hello world\""`, }, + { + name: "empty string arg", + args: []string{"sh", "-c", ""}, + expected: `sh -c ""`, + }, } for _, tc := range testCases {