From a1447899103a0f0294d6931567f20a4eb32594a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sun, 5 Dec 2021 22:33:26 +0000 Subject: [PATCH] only obfuscate literals in packages to obfuscate Add a regression test in gogarble.txt, as that test is already set up with packages to not obfuscate. This bug manifested in the form of a build failure for GOOS=plan9 with -literals turned on: [...]/os/file_plan9.go:151:12: invalid operation: cannot call non-function append (variable of type bool) In this case, the "os" package is not to be obfuscated, but we would still obfuscate its literals as per the bug. But, since the package's identifiers were not obfuscated, names like "append" were not replaced as per ea2e0bdf71, meaning that the shadowing would still affect us. Fixes #417. --- main.go | 4 +++- testdata/scripts/gogarble.txt | 15 +++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 0e3443d..8874aa7 100644 --- a/main.go +++ b/main.go @@ -1227,7 +1227,9 @@ func (tf *transformer) recordType(t types.Type) { // transformGo obfuscates the provided Go syntax file. func (tf *transformer) transformGo(file *ast.File) *ast.File { - if opts.ObfuscateLiterals { + // Only obfuscate the literals here if the flag is on + // and if the package in question is to be obfuscated. + if opts.ObfuscateLiterals && curPkg.ToObfuscate { file = literals.Obfuscate(file, tf.info, fset, tf.ignoreObjects) } diff --git a/testdata/scripts/gogarble.txt b/testdata/scripts/gogarble.txt index 6acdbf5..6ac0ea2 100644 --- a/testdata/scripts/gogarble.txt +++ b/testdata/scripts/gogarble.txt @@ -8,8 +8,12 @@ env GOPRIVATE=match-absolutely/nothing ! garble build -o=out ./standalone stderr '^GOGARBLE="match-absolutely/nothing" does not match any packages to be built$' +# A build where just some packages are obfuscated. env GOGARBLE=test/main/imported -garble build ./importer +garble -literals build -o=out ./importer + +! binsubstr out 'some long string to obfuscate' +binsubstr out 'some long string to not obfuscate' # Obfuscated packages which import non-obfuscated std packages. # Some of the imported std packages use "import maps" due to vendoring, @@ -49,15 +53,18 @@ package main func main() {} -- importer/importer.go -- -package importer +package main import "test/main/imported" -var _ = imported.Name +func main() { + println(imported.LongString) + println("some long string to not obfuscate") +} -- imported/imported.go -- package imported -var Name = "value" +var LongString = "some long string to obfuscate" -- stdimporter/main.go -- package main