From a09b197fe2b784009b299a1ea6f9f3b45a504ac9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sat, 13 Jun 2020 00:26:04 +0100 Subject: [PATCH] remove the code to handle a nil file.Imports I could not reproduce the supposed panic, even after I was able to reach a nil x.Imports in that line in question with the modified test. --- strings.go | 11 ++--------- testdata/scripts/strings.txt | 20 ++++++++++++++------ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/strings.go b/strings.go index bdf6a02..05cbb27 100644 --- a/strings.go +++ b/strings.go @@ -42,15 +42,8 @@ func obfuscateLiterals(files []*ast.File) []*ast.File { } x.Decls = append(x.Decls, funcStmt) x.Decls = append(x.Decls, keyStmt(key)) - - if x.Imports == nil { - newDecls := []ast.Decl{cryptoAesImportSpec} - newDecls = append(newDecls, x.Decls...) - x.Decls = newDecls - } else { - astutil.AddImport(fset, x, "crypto/aes") - astutil.AddImport(fset, x, "crypto/cipher") - } + astutil.AddImport(fset, x, "crypto/aes") + astutil.AddImport(fset, x, "crypto/cipher") addedToPkg = true case *ast.BasicLit: diff --git a/testdata/scripts/strings.txt b/testdata/scripts/strings.txt index 40f686c..bb15277 100644 --- a/testdata/scripts/strings.txt +++ b/testdata/scripts/strings.txt @@ -1,6 +1,6 @@ -garble -literals build main.go -exec ./main +garble -literals build +exec ./main$exe cmp stdout main.stdout ! binsubstr main$exe 'Lorem' 'ipsum' 'dolor' 'first assign' 'second assign' 'First Line' 'Second Line' 'map value' 'to obfuscate' 'also obfuscate' @@ -11,9 +11,11 @@ binsubstr main$exe 'Skip this block,' 'also skip this' # Also check that the binary is reproducible. cp main$exe main_old$exe rm main$exe -garble -literals build main.go +garble -literals build bincmp main$exe main_old$exe +-- go.mod -- +module test/main -- main.go -- package main @@ -48,8 +50,6 @@ var array [arrayLen]byte type typeAlias [arrayLen]byte -var variable = "ipsum" - func main() { empty := "" @@ -83,6 +83,14 @@ func main() { fmt.Println(i, foo, bar) } +-- decl_without_imports.go -- +package main + +// The lack of imports is an edge case, since we have to add imports like +// crypto/aes. + +var variable = "ipsum" + -- main.stdout -- Lorem First Line @@ -96,4 +104,4 @@ also obfuscate map value another literal Skip this block, also skip this -1 0 1 \ No newline at end of file +1 0 1