ignore -ldflags=-X flags mentioning unknown packages

That would panic, since the *listedPackage would be nil for a package
path we aren't aware of:

	panic: runtime error: invalid memory address or nil pointer dereference
	[signal SIGSEGV: segmentation violation code=0x1 addr=0x88 pc=0x126b57d]

	goroutine 1 [running]:
	main.transformLink.func1(0x7ffeefbff28b, 0x5d)
		mvdan.cc/garble@v0.0.0-20210302140807-b03cd08c0946/main.go:1260 +0x17d
	main.flagValueIter(0xc0000a8e20, 0x2f, 0x2f, 0x12e278e, 0x2, 0xc000129e28)
		mvdan.cc/garble@v0.0.0-20210302140807-b03cd08c0946/main.go:1410 +0x1e9
	main.transformLink(0xc0000a8e20, 0x30, 0x36, 0x4, 0xc000114648, 0x23, 0x12dfd60, 0x0)
		mvdan.cc/garble@v0.0.0-20210302140807-b03cd08c0946/main.go:1241 +0x1b9
	main.mainErr(0xc0000a8e10, 0x31, 0x37, 0x37, 0x0)
		mvdan.cc/garble@v0.0.0-20210302140807-b03cd08c0946/main.go:287 +0x389
	main.main1(0xc000096058)
		mvdan.cc/garble@v0.0.0-20210302140807-b03cd08c0946/main.go:150 +0xe7
	main.main()
		mvdan.cc/garble@v0.0.0-20210302140807-b03cd08c0946/main.go:83 +0x25

The linker ignores such unknown references, so we should too.

Fixes #259.
pull/262/head
Daniel Martí 3 years ago committed by Andrew LeFevre
parent a397a8e94e
commit 99887c13c2

@ -1257,11 +1257,17 @@ func transformLink(args []string) ([]string, error) {
if pkgPath == "main" {
pkgPath = cache.MainImportPath
}
id := cache.ListedPackages[pkgPath].GarbleActionID
newName := hashWith(id, name)
lpkg := cache.ListedPackages[pkgPath]
if lpkg == nil {
// We couldn't find the package.
// Perhaps a typo, perhaps not part of the build.
// cmd/link ignores those, so we should too.
return
}
newName := hashWith(lpkg.GarbleActionID, name)
newPkg := pkg
if pkg != "main" && isPrivate(pkg) {
newPkg = hashWith(id, pkg)
newPkg = hashWith(lpkg.GarbleActionID, pkg)
}
flags = append(flags, fmt.Sprintf("-X=%s.%s=%s", newPkg, newName, str))
})

@ -1,19 +1,21 @@
# Note the proper domain, since the dot adds an edge case.
env GOPRIVATE=domain.test/main
garble build -ldflags='-X=main.unexportedVersion=v1.0.0 -X=domain.test/main/imported.ExportedVar=replaced'
env LDFLAGS='-X=main.unexportedVersion=v1.0.0 -X=domain.test/main/imported.ExportedVar=replaced -X=domain.test/missing/path.missingVar=value'
garble build -ldflags=${LDFLAGS}
exec ./main
cmp stderr main.stderr
! binsubstr main$exe 'unexportedVersion'
[short] stop # no need to verify this with -short
garble -tiny build -ldflags='-X=main.unexportedVersion=v1.0.0 -X=domain.test/main/imported.ExportedVar=replaced'
garble -tiny build -ldflags=${LDFLAGS}
exec ./main
cmp stderr main.stderr
! binsubstr main$exe 'unexportedVersion'
exec go build -ldflags='-X=main.unexportedVersion=v1.0.0 -X=domain.test/main/imported.ExportedVar=replaced'
exec go build -ldflags=${LDFLAGS}
exec ./main
cmp stderr main.stderr
binsubstr main$exe 'unexportedVersion'

Loading…
Cancel
Save