bug: defer DeferClose before error check in multiple cmd tools #19
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Parent: #1
Description
Several command-line tools call
defer extractor.DeferClose(b)ordefer extractor.DeferClose(doc)before checking if the creation call returned an error:sites/duckduckgo/cmd/duckduckgo/main.go:76-79:sites/google/cmd/google/main.go:62-66:sites/wegmans/wegmans.go:63-66andsites/aislegopher/aislegopher.go:53-56: Same pattern withb.Open().While
DeferClosenil-checks its argument, this pattern is error-prone and inconsistent with Go best practices. If a function returns both a non-nil value and an error, the defer could interact with a partially-initialized object.Additionally, powerball and megamillions cmd tools don't call
DeferClose(b)on the browser at all — the browser is leaked.Fix
Move defer after error check:
Add
defer extractor.DeferClose(b)to powerball and megamillions cmd tools.Starting work on this as part of PR 5 (also includes #8). Will move
defer DeferClose(x)after the error check in all 6 affected locations.Work finished. PR: #36
Moved
defer DeferClose(x)after error checks in all 6 affected locations.Resolved by PR #36 — moved all
defer DeferClose(x)calls after error checks across all affected files.