about summary refs log tree commit diff
path: root/third_party/nix/src/libstore/references.cc
diff options
context:
space:
mode:
authorKane York <kanepyork@gmail.com>2020-07-28T00·07-0700
committerkanepyork <rikingcoding@gmail.com>2020-07-28T02·00+0000
commit976a36c2e482f416acd79a624e6d96cce2564b5b (patch)
tree7a9cd35b3b587253b0a1f816447201cb6f0fb137 /third_party/nix/src/libstore/references.cc
parentd9262bd6c68ddf39cc22c147ecf40867f4ec3fb9 (diff)
chore(3p/nix/hash): eliminate exposed global variable r/1503
Change-Id: I3b34e3e17a751e225831ae599c6c6bb782a25679
Reviewed-on: https://cl.tvl.fyi/c/depot/+/1486
Tested-by: BuildkiteCI
Reviewed-by: tazjin <mail@tazj.in>
Diffstat (limited to 'third_party/nix/src/libstore/references.cc')
-rw-r--r--third_party/nix/src/libstore/references.cc54
1 files changed, 18 insertions, 36 deletions
diff --git a/third_party/nix/src/libstore/references.cc b/third_party/nix/src/libstore/references.cc
index b70d282e8df8..879f126f63ee 100644
--- a/third_party/nix/src/libstore/references.cc
+++ b/third_party/nix/src/libstore/references.cc
@@ -13,38 +13,21 @@ namespace nix {
 
 static unsigned int refLength = 32; /* characters */
 
-static void search(const unsigned char* s, size_t len, StringSet& hashes,
+static void search(const char* s, size_t len, StringSet& hashes,
                    StringSet& seen) {
-  static bool initialised = false;
-  static bool isBase32[256];
-  if (!initialised) {
-    for (bool& i : isBase32) {
-      i = false;
-    }
-    for (char base32Char : base32Chars) {
-      isBase32[static_cast<unsigned char>(base32Char)] = true;
-    }
-    initialised = true;
-  }
-
   for (size_t i = 0; i + refLength <= len;) {
-    int j = 0;
-    bool match = true;
-    for (j = refLength - 1; j >= 0; --j) {
-      if (!isBase32[s[i + j]]) {
-        i += j + 1;
-        match = false;
-        break;
-      }
-    }
+    absl::string_view ref(s + i, refLength);
+    bool match = Hash::IsValidBase32(ref);
     if (!match) {
       continue;
     }
-    std::string ref(reinterpret_cast<const char*>(s) + i, refLength);
-    if (hashes.find(ref) != hashes.end()) {
+    // TODO(kanepyork): convert StringSet to flat_hash_set, delay owned string
+    // conversion into the 'if'
+    std::string sref(ref);
+    if (hashes.find(sref) != hashes.end()) {
       DLOG(INFO) << "found reference to '" << ref << "' at offset " << i;
-      seen.insert(ref);
-      hashes.erase(ref);
+      seen.insert(sref);
+      hashes.erase(sref);
     }
     ++i;
   }
@@ -65,22 +48,21 @@ struct RefScanSink : Sink {
 void RefScanSink::operator()(const unsigned char* data, size_t len) {
   hashSink(data, len);
 
+  const char* data_ = reinterpret_cast<const char*>(data);
+
   /* It's possible that a reference spans the previous and current
      fragment, so search in the concatenation of the tail of the
      previous fragment and the start of the current fragment. */
-  std::string s = tail + std::string(reinterpret_cast<const char*>(data),
-                                     len > refLength ? refLength : len);
-  search(reinterpret_cast<const unsigned char*>(s.data()), s.size(), hashes,
-         seen);
+  std::string s = tail + std::string(data_, len > refLength ? refLength : len);
+  search(s.data(), s.size(), hashes, seen);
 
-  search(data, len, hashes, seen);
+  search(data_, len, hashes, seen);
 
   size_t tailLen = len <= refLength ? len : refLength;
-  tail =
-      std::string(tail, tail.size() < refLength - tailLen
-                            ? 0
-                            : tail.size() - (refLength - tailLen)) +
-      std::string(reinterpret_cast<const char*>(data) + len - tailLen, tailLen);
+  tail = std::string(tail, tail.size() < refLength - tailLen
+                               ? 0
+                               : tail.size() - (refLength - tailLen)) +
+         std::string(data_ + len - tailLen, tailLen);
 }
 
 PathSet scanForReferences(const std::string& path, const PathSet& refs,