Skip to content

Commit

Permalink
[pkg] fix deferring unsafe method "Close" on type "*os.File" (#3548)
Browse files Browse the repository at this point in the history
  • Loading branch information
huof6829 authored Jul 20, 2022
1 parent a969b74 commit e209d4e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
18 changes: 10 additions & 8 deletions ioctl/doc/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import (
// GenMarkdownTreeCustom is the the same as GenMarkdownTree, but
// with custom filePrepender and linkHandler.
func GenMarkdownTreeCustom(c *cobra.Command, dir string, name string, path string, filePrepender func(string) string,
linkHandler func(*cobra.Command, string) string) error {
linkHandler func(*cobra.Command, string) string) (err error) {
for _, child := range c.Commands() {
if !child.IsAvailableCommand() || child.IsAdditionalHelpTopicCommand() {
continue
}
if err := GenMarkdownTreeCustom(child, dir, name, path, filePrepender, linkHandler); err != nil {
if err = GenMarkdownTreeCustom(child, dir, name, path, filePrepender, linkHandler); err != nil {
return err
}
}
Expand All @@ -32,19 +32,21 @@ func GenMarkdownTreeCustom(c *cobra.Command, dir string, name string, path strin
filename = filepath.Join(path, "README.md")
}

f, err := os.Create(filepath.Clean(filename))
var f *os.File
f, err = os.Create(filepath.Clean(filename))
if err != nil {
return err
}
defer f.Close()

if _, err := io.WriteString(f, filePrepender(filename)); err != nil {
defer func() {
err = f.Close()
}()
if _, err = io.WriteString(f, filePrepender(filename)); err != nil {
return err
}
if err := GenMarkdownCustom(c, f, linkHandler); err != nil {
if err = GenMarkdownCustom(c, f, linkHandler); err != nil {
return err
}
return nil
return err
}

// GenMarkdownCustom creates custom markdown output.
Expand Down
8 changes: 7 additions & 1 deletion pkg/recovery/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,15 @@ func writeHeapProfile(path string) {
log.S().Errorf("crashlog: open heap profile error: %v", err)
return
}
defer f.Close()
defer func() {
if err = f.Close(); err != nil {
log.S().Errorf("crashlog: close heap profile error: %v", err)
return
}
}()
if err := pprof.WriteHeapProfile(f); err != nil {
log.S().Errorf("crashlog: write heap profile error: %v", err)
return
}
}

Expand Down

0 comments on commit e209d4e

Please sign in to comment.