Skip to content

Commit 8a6bce8

Browse files
authored
Escape backslashes and newlines in response files (#4297)
<!-- Thanks for sending a PR! Before submitting: 1. If this is your first PR, please read CONTRIBUTING.md and sign the CLA first. We cannot review code without a signed CLA. 2. Please file an issue *first*. All features and most bug fixes should have an associated issue with a design discussed and decided upon. Small bug fixes and documentation improvements don't need issues. 3. New features and bug fixes must have tests. Documentation may need to be updated. If you're unsure what to update, send the PR, and we'll discuss in review. 4. Note that PRs updating dependencies and new Go versions are not accepted. Please file an issue instead. --> **What type of PR is this?** > Uncomment one line below and remove others. > Bug fix **What does this PR do? Why is it needed?** It escapes backlashes by doubling them when using response files. Without this, builds that trigger the usage of response files fail with: ``` <unknown line number>: internal compiler error: panic: badly formatted input ``` This comes from [this line of code](https://cs.opensource.google/go/go/+/master:src/cmd/internal/objabi/flag.go;l=193;drc=c7ea87132f4e6f3c81e525c396a64471c9af0091;bpv=0?q=badly%20formatted%20input&ss=go%2Fgo) in the Go compiler, which complains if a backlash is not followed either by another `\` or by `n`. **Which issues(s) does this PR fix?** Fixes #4112 (I'd expect). **Other notes for review** I'd be happy to add a test but that function is not written in a very testable way (and I'm not sure the response files feature as a whole has any tests). As it is and without more significant changes I don't think it would be possible to write a test for it.
1 parent 6eed86d commit 8a6bce8

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

go/tools/builders/env.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,11 @@ func passLongArgsInResponseFiles(cmd *exec.Cmd) (cleanup func()) {
543543
cleanup = func() { os.Remove(tf.Name()) }
544544
var buf bytes.Buffer
545545
for _, arg := range cmd.Args[1:] {
546-
fmt.Fprintf(&buf, "%s\n", arg)
546+
// Slashes need to be doubled for escaping
547+
escaped_arg := strings.ReplaceAll(arg, "\\", "\\\\")
548+
// Newlines too, so that they don't get split when read
549+
escaped_arg = strings.ReplaceAll(escaped_arg, "\n", "\\n")
550+
fmt.Fprintf(&buf, "%s\n", escaped_arg)
547551
}
548552
if _, err := tf.Write(buf.Bytes()); err != nil {
549553
tf.Close()

0 commit comments

Comments
 (0)