From c02a14117d9d7454b5c14f4043b410c61376d863 Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Mon, 10 Jul 2023 19:04:31 +0300 Subject: [PATCH] Pull request 1912: 5896-safe-browsing-ptr Updates #5896. Squashed commit of the following: commit 49340544a2a8762283397cdb54b91ed534591fa0 Author: Ainar Garipov Date: Mon Jul 10 17:59:45 2023 +0300 hashprefix: fix loop pointer --- internal/filtering/hashprefix/cache.go | 22 +++++++++++---------- internal/filtering/hashprefix/hashprefix.go | 2 +- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/internal/filtering/hashprefix/cache.go b/internal/filtering/hashprefix/cache.go index d4211b72..1b609474 100644 --- a/internal/filtering/hashprefix/cache.go +++ b/internal/filtering/hashprefix/cache.go @@ -47,7 +47,7 @@ func fromCacheItem(item *cacheItem) (data []byte) { data = binary.BigEndian.AppendUint64(data, uint64(expiry)) for _, v := range item.hashes { - // nolint:looppointer // The subsilce is used for a copy. + // nolint:looppointer // The subsilce of v is used for a copy. data = append(data, v[:]...) } @@ -63,7 +63,7 @@ func (c *Checker) findInCache( i := 0 for _, hash := range hashes { - // nolint:looppointer // The subsilce is used for a safe cache lookup. + // nolint:looppointer // The has subsilce is used for a cache lookup. data := c.cache.Get(hash[:prefixLen]) if data == nil { hashes[i] = hash @@ -98,34 +98,36 @@ func (c *Checker) storeInCache(hashesToRequest, respHashes []hostnameHash) { for _, hash := range respHashes { var pref prefix - // nolint:looppointer // The subsilce is used for a copy. + // nolint:looppointer // The hash subsilce is used for a copy. copy(pref[:], hash[:]) hashToStore[pref] = append(hashToStore[pref], hash) } for pref, hash := range hashToStore { - // nolint:looppointer // The subsilce is used for a safe cache lookup. - c.setCache(pref[:], hash) + c.setCache(pref, hash) } for _, hash := range hashesToRequest { - // nolint:looppointer // The subsilce is used for a safe cache lookup. - pref := hash[:prefixLen] - val := c.cache.Get(pref) + // nolint:looppointer // The hash subsilce is used for a cache lookup. + val := c.cache.Get(hash[:prefixLen]) if val == nil { + var pref prefix + // nolint:looppointer // The hash subsilce is used for a copy. + copy(pref[:], hash[:]) + c.setCache(pref, nil) } } } // setCache stores hash in cache. -func (c *Checker) setCache(pref []byte, hashes []hostnameHash) { +func (c *Checker) setCache(pref prefix, hashes []hostnameHash) { item := &cacheItem{ expiry: time.Now().Add(c.cacheTime), hashes: hashes, } - c.cache.Set(pref, fromCacheItem(item)) + c.cache.Set(pref[:], fromCacheItem(item)) log.Debug("%s: stored in cache: %v", c.svc, pref) } diff --git a/internal/filtering/hashprefix/hashprefix.go b/internal/filtering/hashprefix/hashprefix.go index ed0e3ae2..219ded66 100644 --- a/internal/filtering/hashprefix/hashprefix.go +++ b/internal/filtering/hashprefix/hashprefix.go @@ -173,7 +173,7 @@ func (c *Checker) getQuestion(hashes []hostnameHash) (q string) { b := &strings.Builder{} for _, hash := range hashes { - // nolint:looppointer // The subsilce is used for safe hex encoding. + // nolint:looppointer // The hash subsilce is used for hex encoding. stringutil.WriteToBuilder(b, hex.EncodeToString(hash[:prefixLen]), ".") }