From 99887c13c241f480beb01ea8a62fd1af27c0c3d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Wed, 3 Mar 2021 09:51:44 +0000 Subject: [PATCH] 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. --- main.go | 12 +++++++++--- testdata/scripts/ldflags.txt | 8 +++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index f40debf..ad9c870 100644 --- a/main.go +++ b/main.go @@ -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)) }) diff --git a/testdata/scripts/ldflags.txt b/testdata/scripts/ldflags.txt index 1121447..3a7acc0 100644 --- a/testdata/scripts/ldflags.txt +++ b/testdata/scripts/ldflags.txt @@ -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'