Skip to content

fix: avoid mutating input slice in cmd.prettyCmd() #7448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 12, 2025

Conversation

stasadev
Copy link
Member

@stasadev stasadev commented Jul 11, 2025

The Issue

I noticed a flaw in cmd.prettyCmd():

https://go.dev/play/p/EVaaARj1uYn

cmd := []string{"bash", "-c", `echo "test"`}
fmt.Printf("original cmd: %v\n", cmd)            # original cmd: [bash -c echo "test"]
fmt.Printf("  pretty cmd: %v\n", prettyCmd(cmd)) #   pretty cmd: bash -c "echo "test""
fmt.Printf("original cmd: %v\n", cmd)            # original cmd: [bash -c "echo "test""] - bad

How This PR Solves The Issue

Makes a copy, escapes double quotes.

Manual Testing Instructions

https://go.dev/play/p/ZDcMH1TgqE1

cmd := []string{"bash", "-c", `echo "test"`}
fmt.Printf("original cmd: %v\n", cmd)            # original cmd: [bash -c echo "test"]
fmt.Printf("  pretty cmd: %v\n", prettyCmd(cmd)) #   pretty cmd: bash -c "echo \"test\""
fmt.Printf("original cmd: %v\n", cmd)            # original cmd: [bash -c echo "test"] - good

Automated Testing Overview

Release/Deployment Notes

@stasadev stasadev requested a review from a team as a code owner July 11, 2025 21:19
Copy link
Member

@rfay rfay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I remember that this class of problem was fixed up in latest golang?

@stasadev
Copy link
Member Author

stasadev commented Jul 11, 2025

I'm testing it using the latest go1.24.5.

It was a rather random discovery, I was trying to debug ddev auth ssh and used cmd.prettyCmd() in several places and then noticed this mutation.

It doesn't affect our current code, since it's only used in the final error output in ddev auth ssh (the original input isn't used afterward) and in ddev debug rebuild (where the command args don't have spaces, so they are not modified), but it should be fixed.

@rfay
Copy link
Member

rfay commented Jul 11, 2025

Thanks. I guess I was thinking of this change in go 1.22, https://go.dev/blog/loopvar-preview

Copy link
Member

@rfay rfay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The go playground approach to demo was really nice too.

@rfay rfay merged commit f4fe3c5 into ddev:main Jul 12, 2025
22 checks passed
@rfay rfay deleted the 20250711_stasadev_pretty_cmd branch July 12, 2025 18:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants