From d056b8543276213679a3de8c245e426ce16be01d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zlatko=20=C4=8Calu=C5=A1i=C4=87?= Date: Wed, 25 Oct 2017 18:31:34 +0200 Subject: [PATCH] Check errors in many places Admittedly, in some places just document the fact that we ignore error return values, 'cause we don't know what to do with it. At least, the linter is happy. --- handlers.go | 20 ++++++++++++-------- htpasswd.go | 6 ++++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/handlers.go b/handlers.go index cb73852..3459332 100644 --- a/handlers.go +++ b/handlers.go @@ -159,7 +159,7 @@ func GetConfig(w http.ResponseWriter, r *http.Request) { return } - w.Write(bytes) + _, _ = w.Write(bytes) } // SaveConfig allows for a config to be saved. @@ -272,7 +272,7 @@ func ListBlobs(w http.ResponseWriter, r *http.Request) { return } - w.Write(data) + _, _ = w.Write(data) } // CheckBlob tests whether a blob exists. @@ -322,7 +322,11 @@ func GetBlob(w http.ResponseWriter, r *http.Request) { wc := datacounter.NewResponseWriterCounter(w) http.ServeContent(wc, r, "", time.Unix(0, 0), file) - file.Close() + + if err = file.Close(); err != nil { + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } if Config.Prometheus { labels := prometheus.Labels{"repo": getRepo(r), "type": pat.Param(r, "type")} @@ -354,8 +358,8 @@ func SaveBlob(w http.ResponseWriter, r *http.Request) { written, err := io.Copy(tf, r.Body) if err != nil { - tf.Close() - os.Remove(path) + _ = tf.Close() + _ = os.Remove(path) if Config.Debug { log.Print(err) } @@ -364,8 +368,8 @@ func SaveBlob(w http.ResponseWriter, r *http.Request) { } if err := tf.Sync(); err != nil { - tf.Close() - os.Remove(path) + _ = tf.Close() + _ = os.Remove(path) if Config.Debug { log.Print(err) } @@ -374,7 +378,7 @@ func SaveBlob(w http.ResponseWriter, r *http.Request) { } if err := tf.Close(); err != nil { - os.Remove(path) + _ = os.Remove(path) if Config.Debug { log.Print(err) } diff --git a/htpasswd.go b/htpasswd.go index 1a6e4d1..3dca31b 100644 --- a/htpasswd.go +++ b/htpasswd.go @@ -88,7 +88,6 @@ func (h *HtpasswdFile) Reload() error { if err != nil { return err } - defer r.Close() cr := csv.NewReader(r) cr.Comma = ':' @@ -97,6 +96,7 @@ func (h *HtpasswdFile) Reload() error { records, err := cr.ReadAll() if err != nil { + _ = r.Close() return err } users := make(map[string]string) @@ -108,6 +108,8 @@ func (h *HtpasswdFile) Reload() error { h.mutex.Lock() h.Users = users h.mutex.Unlock() + + _ = r.Close() return nil } @@ -160,7 +162,7 @@ func (h *HtpasswdFile) Validate(user string, password string) bool { } if realPassword[:5] == "{SHA}" { d := sha1.New() - d.Write([]byte(password)) + _, _ = d.Write([]byte(password)) if realPassword[5:] == base64.StdEncoding.EncodeToString(d.Sum(nil)) { return true }