cg = decl.Doc
}
if cg != nil {
+ if strings.ContainsAny(abspath, "\r\n") {
+ // This should have been checked when the file path was first resolved,
+ // but we double check here just to be sure.
+ fatalf("internal error: ParseGo: abspath contains unexpected newline character: %q", abspath)
+ }
f.Preamble += fmt.Sprintf("#line %d %q\n", sourceLine(cg), abspath)
f.Preamble += commentText(cg) + "\n"
f.Preamble += "#line 1 \"cgo-generated-wrapper\"\n"
// Apply trimpath to the file path. The path won't be read from after this point.
input, _ = objabi.ApplyRewrites(input, *trimpath)
+ if strings.ContainsAny(input, "\r\n") {
+ // ParseGo, (*Package).writeOutput, and printer.Fprint in SourcePos mode
+ // all emit line directives, which don't permit newlines in the file path.
+ // Bail early if we see anything newline-like in the trimmed path.
+ fatalf("input path contains newline character: %q", input)
+ }
goFiles[i] = input
f := new(File)
// Write Go output: Go input with rewrites of C.xxx to _C_xxx.
fmt.Fprintf(fgo1, "// Code generated by cmd/cgo; DO NOT EDIT.\n\n")
+ if strings.ContainsAny(srcfile, "\r\n") {
+ // This should have been checked when the file path was first resolved,
+ // but we double check here just to be sure.
+ fatalf("internal error: writeOutput: srcfile contains unexpected newline character: %q", srcfile)
+ }
fmt.Fprintf(fgo1, "//line %s:1:1\n", srcfile)
fgo1.Write(f.Edit.Bytes())
go list -compiled -e -f '{{with .CompiledGoFiles}}{{.}}{{end}}' .
! stdout .
! stderr .
+! exists obj_
+
+
+# The cgo tool should only accept the source file if the working directory
+# is not written in line directives in the resulting files.
+
+[cgo] ! go tool cgo main.go
+[cgo] stderr 'cgo: input path contains newline character: .*uh-oh'
+[cgo] ! exists _obj
+
+[cgo] go tool cgo -trimpath=$PWD main.go
+[cgo] grep '//line main\.go:1:1' _obj/main.cgo1.go
+[cgo] ! grep 'uh-oh' _obj/main.cgo1.go
+[cgo] rm _obj
# Since we do preserve $PWD (or set it appropriately) for commands, and we do
! stderr panic
stdout '^ok$' # 'go test' combines the test's stdout into stderr
+[cgo] go tool cgo main.go
+[cgo] grep '//line .*'${/}'link'${/}'main\.go:1:1' _obj/main.cgo1.go
+[cgo] ! grep 'uh-oh' _obj/main.cgo1.go
-- $WORK/go.mod --
module example