Skip to content
This repository was archived by the owner on Oct 10, 2020. It is now read-only.

Commit 6dddc4c

Browse files
ashcrowrh-atomic-bot
authored andcommitted
dockertar-sha256-helper: Defer Close
Defer Close calls to ensure closing at the end of the function. Signed-off-by: Steve Milner <[email protected]> Closes: #1117 Approved by: giuseppe
1 parent eced7be commit 6dddc4c

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

dockertar-sha256-helper.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ func main() {
1414
panic("One positional argument is required")
1515
}
1616
reader, err := os.Open(os.Args[1])
17+
// Close the input file at the end of the function
18+
defer reader.Close()
1719
if err != nil {
1820
os.Exit(1)
1921
}
@@ -33,7 +35,8 @@ func main() {
3335
panic(err)
3436
}
3537
}
38+
// We must explicitly close before executing the sum
39+
// as defer will cause an incorrect result
3640
w.Close()
37-
3841
fmt.Printf("%x\n", sha_256.Sum(nil))
3942
}

0 commit comments

Comments
 (0)