Skip to content

Commit 506039e

Browse files
Fix gosec issues
1 parent 9dd48d5 commit 506039e

6 files changed

Lines changed: 23 additions & 13 deletions

File tree

pkg/chain/ethereum/ethutil/ethutil.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func AddressFromHex(hex string) (common.Address, error) {
3535

3636
// DecryptKeyFile reads in a key file and uses the password to decrypt it.
3737
func DecryptKeyFile(keyFile, password string) (*keystore.Key, error) {
38-
data, err := ioutil.ReadFile(keyFile)
38+
data, err := ioutil.ReadFile(keyFile) // #nosec
3939
if err != nil {
4040
return nil, fmt.Errorf("unable to read KeyFile %s [%v]", keyFile, err)
4141
}
@@ -141,7 +141,7 @@ func CallAtBlock(
141141
// the true gas limit requirement as other transactions may be added or removed by miners,
142142
// but it should provide a basis for setting a reasonable default.
143143
func EstimateGas(
144-
from common.Address,
144+
from common.Address,
145145
to common.Address,
146146
method string,
147147
contractABI *abi.ABI,

pkg/generate/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func OrganizeImports(codeBuffer *bytes.Buffer, filePath string) error {
3838
// error writing the file.
3939
func SaveBufferToFile(buffer *bytes.Buffer, filePath string) error {
4040
file, err := os.Create(filePath)
41-
defer file.Close()
41+
defer file.Close() // #nosec
4242
if err != nil {
4343
return fmt.Errorf("output file %s creation failed [%v]", filePath, err)
4444
}

pkg/persistence/disk_persistence.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,26 +123,29 @@ func write(filePath string, data []byte) error {
123123
return err
124124
}
125125

126-
defer writeFile.Close()
126+
defer closeFile(writeFile)
127127

128128
_, err = writeFile.Write(data)
129129
if err != nil {
130130
return err
131131
}
132132

133-
writeFile.Sync()
133+
err = writeFile.Sync()
134+
if err != nil {
135+
return err
136+
}
134137

135138
return nil
136139
}
137140

138141
// read a file from a file system
139142
func read(filePath string) ([]byte, error) {
140-
readFile, err := os.Open(filePath)
143+
readFile, err := os.Open(filePath) // #nosec
141144
if err != nil {
142145
return nil, err
143146
}
144147

145-
defer readFile.Close()
148+
defer closeFile(readFile)
146149

147150
data, err := ioutil.ReadAll(readFile)
148151
if err != nil {
@@ -152,6 +155,13 @@ func read(filePath string) ([]byte, error) {
152155
return data, nil
153156
}
154157

158+
func closeFile(file *os.File) {
159+
err := file.Close()
160+
if err != nil {
161+
logger.Errorf("could not close file [%v]: [%v]", file.Name(), err)
162+
}
163+
}
164+
155165
// readAll reads all files from the provided directoryPath and outputs them
156166
// as DataDescriptors into the first returned output channel. All errors
157167
// occurred during file system reading are sent to the second output channel

tools/generators/ethereum/contract.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func main() {
6161
contractOutputPath := flag.Arg(1)
6262
commandOutputPath := flag.Arg(2)
6363

64-
abiFile, err := ioutil.ReadFile(abiPath)
64+
abiFile, err := ioutil.ReadFile(abiPath) // #nosec
6565
if err != nil {
6666
panic(fmt.Sprintf(
6767
"Failed to read ABI file at [%v]: [%v].",
@@ -223,7 +223,7 @@ func organizeImports(outFile string, buf *bytes.Buffer) error {
223223
// Stores the Buffer `buf` content to a file in `filePath`
224224
func saveBufferToFile(buf *bytes.Buffer, filePath string) error {
225225
file, err := os.Create(filePath)
226-
defer file.Close()
226+
defer file.Close() // #nosec
227227
if err != nil {
228228
return fmt.Errorf("output file %s creation failed [%v]", filePath, err)
229229
}

tools/generators/promise/promise.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ func generatePromisesCode(generationDir string, promisesConfig []promiseConfig)
9696
return fmt.Errorf("template creation failed [%v]", err)
9797
}
9898

99-
for _, promiseConfig := range promisesConfig {
100-
outputFile := promiseConfig.Filename
99+
for i := range promisesConfig {
100+
outputFile := promisesConfig[i].Filename
101101
outputFilePath := path.Join(generationDir, outputFile)
102102

103103
// Generate promise code.
104-
buffer, err := generateCode(promiseTemplate, &promiseConfig, outputFilePath)
104+
buffer, err := generateCode(promiseTemplate, &promisesConfig[i], outputFilePath)
105105
if err != nil {
106106
return fmt.Errorf("promise generation failed [%v]", err)
107107
}

tools/generators/template/template.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func main() {
3030
}
3131

3232
templateFile := os.Args[templateFileArgIndex]
33-
templateContents, err := ioutil.ReadFile(templateFile)
33+
templateContents, err := ioutil.ReadFile(templateFile) // #nosec
3434
if err != nil {
3535
errorAndExit(fmt.Sprintf("Failed to open template file: [%v].", err))
3636
}

0 commit comments

Comments
 (0)