From 4dd382aaf25132b31eb269749a2cd36daf0cb792 Mon Sep 17 00:00:00 2001 From: Eugene Burkov Date: Mon, 23 Nov 2020 13:59:10 +0300 Subject: [PATCH] all: improve code quality --- internal/aghio/limitedreadcloser.go | 4 ++-- internal/aghio/limitedreadcloser_test.go | 2 +- internal/home/auth_glinet.go | 3 ++- internal/home/middlewares.go | 4 ++++ internal/home/whois.go | 3 ++- internal/update/check.go | 3 ++- internal/update/updater.go | 5 +++-- 7 files changed, 16 insertions(+), 8 deletions(-) diff --git a/internal/aghio/limitedreadcloser.go b/internal/aghio/limitedreadcloser.go index 4f53a2fbdc5..055ee60864b 100644 --- a/internal/aghio/limitedreadcloser.go +++ b/internal/aghio/limitedreadcloser.go @@ -47,9 +47,9 @@ func (lrc *limitedReadCloser) Close() error { // LimitReadCloser wraps ReadCloser to make it's Reader stop with // ErrLimitReached after n bytes read. -func LimitReadCloser(rc io.ReadCloser, n int64) (new io.ReadCloser, err error) { +func LimitReadCloser(rc io.ReadCloser, n int64) (newRC io.ReadCloser, err error) { if n < 0 { - return nil, fmt.Errorf("invalid n: %d", n) + return nil, fmt.Errorf("aghio: invalid n in LimitReadCloser: %d", n) } return &limitedReadCloser{ limit: n, diff --git a/internal/aghio/limitedreadcloser_test.go b/internal/aghio/limitedreadcloser_test.go index e6cb084f452..1f10e32bf7e 100644 --- a/internal/aghio/limitedreadcloser_test.go +++ b/internal/aghio/limitedreadcloser_test.go @@ -26,7 +26,7 @@ func TestLimitReadCloser(t *testing.T) { }, { name: "negative", n: -1, - want: fmt.Errorf("invalid n: -1"), + want: fmt.Errorf("aghio: invalid n in LimitReadCloser: -1"), }} for _, tc := range testCases { diff --git a/internal/home/auth_glinet.go b/internal/home/auth_glinet.go index a99118b119b..228843fa167 100644 --- a/internal/home/auth_glinet.go +++ b/internal/home/auth_glinet.go @@ -83,10 +83,11 @@ func glGetTokenDate(file string) uint32 { log.Error("os.Open: %s", err) return 0 } + defer f.Close() + fileReadCloser, err := aghio.LimitReadCloser(f, MaxFileSize) if err != nil { log.Error("LimitReadCloser: %s", err) - f.Close() return 0 } defer fileReadCloser.Close() diff --git a/internal/home/middlewares.go b/internal/home/middlewares.go index 9dce404017c..4a38160dc00 100644 --- a/internal/home/middlewares.go +++ b/internal/home/middlewares.go @@ -5,6 +5,8 @@ import ( "strings" "github.com/AdguardTeam/AdGuardHome/internal/aghio" + + "github.com/AdguardTeam/golibs/log" ) // middlerware is a wrapper function signature. @@ -31,6 +33,8 @@ func limitRequestBody(h http.Handler) (limited http.Handler) { var err error r.Body, err = aghio.LimitReadCloser(r.Body, RequestBodySizeLimit) if err != nil { + log.Error("limitRequestBody: %s", err) + return } diff --git a/internal/home/whois.go b/internal/home/whois.go index 0d6fbd10e78..4884d776dc0 100644 --- a/internal/home/whois.go +++ b/internal/home/whois.go @@ -129,9 +129,10 @@ func (w *Whois) query(target, serverAddr string) (string, error) { if err != nil { return "", err } + defer conn.Close() + connReadCloser, err := aghio.LimitReadCloser(conn, MaxConnReadSize) if err != nil { - conn.Close() return "", err } defer connReadCloser.Close() diff --git a/internal/update/check.go b/internal/update/check.go index aa2fa675e1a..e83ab5c20f2 100644 --- a/internal/update/check.go +++ b/internal/update/check.go @@ -35,9 +35,10 @@ func (u *Updater) GetVersionResponse(forceRecheck bool) (VersionInfo, error) { if err != nil { return VersionInfo{}, fmt.Errorf("updater: HTTP GET %s: %w", u.VersionURL, err) } + defer resp.Body.Close() + resp.Body, err = aghio.LimitReadCloser(resp.Body, MaxResponseSize) if err != nil { - resp.Body.Close() return VersionInfo{}, fmt.Errorf("updater: LimitReadCloser: %w", err) } defer resp.Body.Close() diff --git a/internal/update/updater.go b/internal/update/updater.go index 032c5a4e04b..34d6681949e 100644 --- a/internal/update/updater.go +++ b/internal/update/updater.go @@ -219,7 +219,8 @@ func (u *Updater) clean() { } // MaxPackageFileSize is a maximum package file length in bytes. The largest -// package whose size is limited by this constant has size of 9MiB for now. +// package whose size is limited by this constant currently has the size of +// approximately 9 MiB. const MaxPackageFileSize = 32 * 1024 * 1024 // Download package file and save it to disk @@ -228,10 +229,10 @@ func (u *Updater) downloadPackageFile(url string, filename string) error { if err != nil { return fmt.Errorf("http request failed: %w", err) } + defer resp.Body.Close() resp.Body, err = aghio.LimitReadCloser(resp.Body, MaxPackageFileSize) if err != nil { - resp.Body.Close() return fmt.Errorf("http request failed: %w", err) } defer resp.Body.Close()