From 35cd28d9ff113cb848a5f4cef68aa22aa901ea4e Mon Sep 17 00:00:00 2001 From: Placebo27 Date: Wed, 19 Apr 2023 14:25:37 +0800 Subject: [PATCH] fix: catch some close error catch the return value of some close function or log an exception when it is not nil Log: catch some close error --- auth.go | 40 ++++++++++++++++---------- util/cursor/cursor.go | 51 ++++++++++++++++++++++----------- util/tool/gen-keysymdef/main.go | 12 ++++++-- 3 files changed, 70 insertions(+), 33 deletions(-) diff --git a/auth.go b/auth.go index 3c0371f..222f0c5 100644 --- a/auth.go +++ b/auth.go @@ -14,6 +14,7 @@ import ( "encoding/binary" "errors" "io" + "log" "os" ) @@ -34,7 +35,7 @@ func readAuthority(hostname, display string) ( if len(hostname) == 0 || hostname == "localhost" { hostname, err = os.Hostname() if err != nil { - return "", nil, err + return } } @@ -43,41 +44,50 @@ func readAuthority(hostname, display string) ( home := os.Getenv("HOME") if len(home) == 0 { err = errors.New("Xauthority not found: $XAUTHORITY, $HOME not set") - return "", nil, err + return } fname = home + "/.Xauthority" } r, err := os.Open(fname) if err != nil { - return "", nil, err + return } - defer r.Close() + defer func() { + closeErr := r.Close() + if err == nil { + err = closeErr + } else { + log.Printf("error on close file %v: %v", r.Name(), closeErr) + } + }() for { var family uint16 - if err := binary.Read(r, binary.BigEndian, &family); err != nil { - return "", nil, err + if err = binary.Read(r, binary.BigEndian, &family); err != nil { + return } - addr, err := getString(r, b) + var addr string + addr, err = getString(r, b) if err != nil { - return "", nil, err + return } - disp, err := getString(r, b) + var disp string + disp, err = getString(r, b) if err != nil { - return "", nil, err + return } - name0, err := getString(r, b) + name, err = getString(r, b) if err != nil { - return "", nil, err + return } - data0, err := getBytes(r, b) + data, err = getBytes(r, b) if err != nil { - return "", nil, err + return } addrmatch := (family == familyWild) || @@ -85,7 +95,7 @@ func readAuthority(hostname, display string) ( dispmatch := (disp == "") || (disp == display) if addrmatch && dispmatch { - return name0, data0, nil + return } } } diff --git a/util/cursor/cursor.go b/util/cursor/cursor.go index b68ae40..7981de6 100644 --- a/util/cursor/cursor.go +++ b/util/cursor/cursor.go @@ -11,6 +11,7 @@ import ( "image/color/palette" "image/draw" "image/gif" + "log" "os" "github.com/linuxdeepin/go-x11-client" @@ -62,30 +63,39 @@ func LoadImageFromFile(filename string, size int) (*Image, error) { return loadImageFromFile(f, size) } -func loadImageFromFile(f *os.File, size int) (*Image, error) { +func loadImageFromFile(f *os.File, size int) (img *Image, err error) { d, err := newDecoder(f) if err != nil { - return nil, err + return } - defer d.close() + defer func() { + closeErr := d.close() + if err == nil { + err = closeErr + } else { + log.Printf("error on close decoder %v: %v", f.Name(), closeErr) + } + }() bestSize, _ := d.findBestSize(size) if bestSize == 0 { - return nil, errImageNotFound + err = errImageNotFound + return } for _, toc := range d.tocs { if toc.Type == typeImage && int(toc.Subtype) == bestSize { - img, err := d.readImage(toc) + img, err = d.readImage(toc) if err != nil { - return nil, err + return } - return img, nil + return } } - return nil, errImageNotFound + err = errImageNotFound + return } func dist(a, b int) int { @@ -121,32 +131,41 @@ func LoadImagesFromFile(filename string, size int) (Images, error) { return loadImagesFromFile(f, size) } -func loadImagesFromFile(f *os.File, size int) (Images, error) { +func loadImagesFromFile(f *os.File, size int) (images Images, err error) { d, err := newDecoder(f) if err != nil { - return nil, err + return } - defer d.close() + defer func() { + closeErr := d.close() + if err == nil { + err = closeErr + } else { + log.Printf("error on close decoder %v: %v", f.Name(), closeErr) + } + }() bestSize, nSizes := d.findBestSize(size) if bestSize == 0 { - return nil, errImageNotFound + err = errImageNotFound + return } - images := make(Images, nSizes) + images = make(Images, nSizes) var idx int for _, toc := range d.tocs { if toc.Type == typeImage && int(toc.Subtype) == bestSize { - img, err := d.readImage(toc) + var img *Image + img, err = d.readImage(toc) if err != nil { - return nil, err + return } images[idx] = img idx++ } } - return images, nil + return } func (images Images) ToGIF() *gif.GIF { diff --git a/util/tool/gen-keysymdef/main.go b/util/tool/gen-keysymdef/main.go index a575058..6914d3b 100644 --- a/util/tool/gen-keysymdef/main.go +++ b/util/tool/gen-keysymdef/main.go @@ -93,7 +93,11 @@ func processKeysymdef() []Keysym { if err != nil { log.Fatal(err) } - defer fh.Close() + defer func() { + if err := fh.Close(); err != nil { + log.Fatal(err) + } + }() var currentSection string var currentSectionOk bool = true @@ -191,7 +195,11 @@ func processXF86Keysymdef() []Keysym { if err != nil { log.Fatal(err) } - defer fh.Close() + defer func() { + if err := fh.Close(); err != nil { + log.Fatal(err) + } + }() scanner := bufio.NewScanner(fh) var defOtherCount int var def3Count int