about summary refs log tree commit diff
path: root/third_party/nix/src/libutil
diff options
context:
space:
mode:
authorKane York <kanepyork@gmail.com>2020-07-25T04·09-0700
committerkanepyork <rikingcoding@gmail.com>2020-07-27T21·16+0000
commitef54f5da9fa30b5c302f2a49595ee5d041f9706a (patch)
tree8d1da709a2e2d3b135d1e84eda9c402bde467726 /third_party/nix/src/libutil
parent69f402563a14d4b668980e4228d033d80e3bb05d (diff)
fix(3p/nix): apply all clang-tidy fixes r/1495
Change-Id: I265e763393422ee1881653527c91024458060825
Reviewed-on: https://cl.tvl.fyi/c/depot/+/1432
Tested-by: BuildkiteCI
Reviewed-by: tazjin <mail@tazj.in>
Diffstat (limited to 'third_party/nix/src/libutil')
-rw-r--r--third_party/nix/src/libutil/archive.cc14
-rw-r--r--third_party/nix/src/libutil/args.cc4
-rw-r--r--third_party/nix/src/libutil/compression.cc22
-rw-r--r--third_party/nix/src/libutil/hash.cc15
-rw-r--r--third_party/nix/src/libutil/json.cc2
-rw-r--r--third_party/nix/src/libutil/serialise.cc20
-rw-r--r--third_party/nix/src/libutil/util.cc47
-rw-r--r--third_party/nix/src/libutil/util.hh2
8 files changed, 64 insertions, 62 deletions
diff --git a/third_party/nix/src/libutil/archive.cc b/third_party/nix/src/libutil/archive.cc
index 40898093212a..c26bf8257e63 100644
--- a/third_party/nix/src/libutil/archive.cc
+++ b/third_party/nix/src/libutil/archive.cc
@@ -66,7 +66,7 @@ static void dumpContents(const Path& path, size_t size, Sink& sink) {
 static void dump(const Path& path, Sink& sink, PathFilter& filter) {
   checkInterrupt();
 
-  struct stat st;
+  struct stat st {};
   if (lstat(path.c_str(), &st) != 0) {
     throw SysError(format("getting attributes of path '%1%'") % path);
   }
@@ -80,7 +80,7 @@ static void dump(const Path& path, Sink& sink, PathFilter& filter) {
       sink << "executable"
            << "";
     }
-    dumpContents(path, (size_t)st.st_size, sink);
+    dumpContents(path, static_cast<size_t>(st.st_size), sink);
   }
 
   else if (S_ISDIR(st.st_mode)) {
@@ -159,7 +159,7 @@ static void skipGeneric(Source & source)
 }
 #endif
 
-static void parseContents(ParseSink& sink, Source& source, const Path& path) {
+static void parseContents(ParseSink& sink, Source& source) {
   unsigned long long size = readLongLong(source);
 
   sink.preallocateContents(size);
@@ -170,7 +170,7 @@ static void parseContents(ParseSink& sink, Source& source, const Path& path) {
   while (left != 0u) {
     checkInterrupt();
     auto n = buf.size();
-    if ((unsigned long long)n > left) {
+    if (static_cast<unsigned long long>(n) > left) {
       n = left;
     }
     source(buf.data(), n);
@@ -235,7 +235,7 @@ static void parse(ParseSink& sink, Source& source, const Path& path) {
     }
 
     else if (s == "contents" && type == tpRegular) {
-      parseContents(sink, source, path);
+      parseContents(sink, source);
     }
 
     else if (s == "executable" && type == tpRegular) {
@@ -267,7 +267,7 @@ static void parse(ParseSink& sink, Source& source, const Path& path) {
           name = readString(source);
           if (name.empty() || name == "." || name == ".." ||
               name.find('/') != std::string::npos ||
-              name.find((char)0) != std::string::npos) {
+              name.find('\0') != std::string::npos) {
             throw Error(format("NAR contains invalid file name '%1%'") % name);
           }
           if (name <= prevName) {
@@ -341,7 +341,7 @@ struct RestoreSink : ParseSink {
   }
 
   void isExecutable() override {
-    struct stat st;
+    struct stat st {};
     if (fstat(fd.get(), &st) == -1) {
       throw SysError("fstat");
     }
diff --git a/third_party/nix/src/libutil/args.cc b/third_party/nix/src/libutil/args.cc
index c56383ef88cc..2be8a1b0ce2a 100644
--- a/third_party/nix/src/libutil/args.cc
+++ b/third_party/nix/src/libutil/args.cc
@@ -27,12 +27,12 @@ void Args::parseCmdline(const Strings& _cmdline) {
        `-j3` -> `-j 3`). */
     if (!dashDash && arg.length() > 2 && arg[0] == '-' && arg[1] != '-' &&
         (isalpha(arg[1]) != 0)) {
-      *pos = (std::string) "-" + arg[1];
+      *pos = std::string("-") + arg[1];
       auto next = pos;
       ++next;
       for (unsigned int j = 2; j < arg.length(); j++) {
         if (isalpha(arg[j]) != 0) {
-          cmdline.insert(next, (std::string) "-" + arg[j]);
+          cmdline.insert(next, std::string("-") + arg[j]);
         } else {
           cmdline.insert(next, std::string(arg, j));
           break;
diff --git a/third_party/nix/src/libutil/compression.cc b/third_party/nix/src/libutil/compression.cc
index 779d326eb204..b598927c88f7 100644
--- a/third_party/nix/src/libutil/compression.cc
+++ b/third_party/nix/src/libutil/compression.cc
@@ -17,7 +17,7 @@ namespace nix {
 
 // Don't feed brotli too much at once.
 struct ChunkedCompressionSink : CompressionSink {
-  uint8_t outbuf[32 * 1024];
+  uint8_t outbuf[32 * 1024]{};
 
   void write(const unsigned char* data, size_t len) override {
     const size_t CHUNK_SIZE = sizeof(outbuf) << 2;
@@ -43,7 +43,7 @@ struct NoneSink : CompressionSink {
 
 struct XzDecompressionSink : CompressionSink {
   Sink& nextSink;
-  uint8_t outbuf[BUFSIZ];
+  uint8_t outbuf[BUFSIZ]{};
   lzma_stream strm = LZMA_STREAM_INIT;
   bool finished = false;
 
@@ -89,7 +89,7 @@ struct XzDecompressionSink : CompressionSink {
 
 struct BzipDecompressionSink : ChunkedCompressionSink {
   Sink& nextSink;
-  bz_stream strm;
+  bz_stream strm{};
   bool finished = false;
 
   explicit BzipDecompressionSink(Sink& nextSink) : nextSink(nextSink) {
@@ -99,7 +99,7 @@ struct BzipDecompressionSink : ChunkedCompressionSink {
       throw CompressionError("unable to initialise bzip2 decoder");
     }
 
-    strm.next_out = (char*)outbuf;
+    strm.next_out = reinterpret_cast<char*>(outbuf);
     strm.avail_out = sizeof(outbuf);
   }
 
@@ -128,7 +128,7 @@ struct BzipDecompressionSink : ChunkedCompressionSink {
 
       if (strm.avail_out < sizeof(outbuf) || strm.avail_in == 0) {
         nextSink(outbuf, sizeof(outbuf) - strm.avail_out);
-        strm.next_out = (char*)outbuf;
+        strm.next_out = reinterpret_cast<char*>(outbuf);
         strm.avail_out = sizeof(outbuf);
       }
     }
@@ -205,12 +205,12 @@ ref<CompressionSink> makeDecompressionSink(const std::string& method,
 
 struct XzCompressionSink : CompressionSink {
   Sink& nextSink;
-  uint8_t outbuf[BUFSIZ];
+  uint8_t outbuf[BUFSIZ]{};
   lzma_stream strm = LZMA_STREAM_INIT;
   bool finished = false;
 
   XzCompressionSink(Sink& nextSink, bool parallel) : nextSink(nextSink) {
-    lzma_ret ret;
+    lzma_ret ret{};
     bool done = false;
 
     if (parallel) {
@@ -277,7 +277,7 @@ struct XzCompressionSink : CompressionSink {
 
 struct BzipCompressionSink : ChunkedCompressionSink {
   Sink& nextSink;
-  bz_stream strm;
+  bz_stream strm{};
   bool finished = false;
 
   explicit BzipCompressionSink(Sink& nextSink) : nextSink(nextSink) {
@@ -287,7 +287,7 @@ struct BzipCompressionSink : ChunkedCompressionSink {
       throw CompressionError("unable to initialise bzip2 encoder");
     }
 
-    strm.next_out = (char*)outbuf;
+    strm.next_out = reinterpret_cast<char*>(outbuf);
     strm.avail_out = sizeof(outbuf);
   }
 
@@ -316,7 +316,7 @@ struct BzipCompressionSink : ChunkedCompressionSink {
 
       if (strm.avail_out < sizeof(outbuf) || strm.avail_in == 0) {
         nextSink(outbuf, sizeof(outbuf) - strm.avail_out);
-        strm.next_out = (char*)outbuf;
+        strm.next_out = reinterpret_cast<char*>(outbuf);
         strm.avail_out = sizeof(outbuf);
       }
     }
@@ -325,7 +325,7 @@ struct BzipCompressionSink : ChunkedCompressionSink {
 
 struct BrotliCompressionSink : ChunkedCompressionSink {
   Sink& nextSink;
-  uint8_t outbuf[BUFSIZ];
+  uint8_t outbuf[BUFSIZ]{};
   BrotliEncoderState* state;
   bool finished = false;
 
diff --git a/third_party/nix/src/libutil/hash.cc b/third_party/nix/src/libutil/hash.cc
index 0d80972cceda..9165182ed70f 100644
--- a/third_party/nix/src/libutil/hash.cc
+++ b/third_party/nix/src/libutil/hash.cc
@@ -86,7 +86,7 @@ static std::string printHash32(const Hash& hash) {
   std::string s;
   s.reserve(len);
 
-  for (int n = (int)len - 1; n >= 0; n--) {
+  for (int n = static_cast<int>(len) - 1; n >= 0; n--) {
     unsigned int b = n * 5;
     unsigned int i = b / 8;
     unsigned int j = b % 8;
@@ -119,7 +119,8 @@ std::string Hash::to_string(Base base, bool includeType) const {
     case Base64:
     case SRI:
       std::string b64;
-      absl::Base64Escape(std::string((const char*)hash, hashSize), &b64);
+      absl::Base64Escape(
+          std::string(reinterpret_cast<const char*>(hash), hashSize), &b64);
       s += b64;
       break;
   }
@@ -179,7 +180,7 @@ Hash::Hash(const std::string& s, HashType type) : type(type) {
   else if (!isSRI && size == base32Len()) {
     for (unsigned int n = 0; n < size; ++n) {
       char c = s[pos + size - n - 1];
-      unsigned char digit;
+      unsigned char digit = 0;
       for (digit = 0; digit < base32Chars.size(); ++digit) { /* !!! slow */
         if (base32Chars[digit] == c) {
           break;
@@ -267,16 +268,16 @@ static void finish(HashType ht, Ctx& ctx, unsigned char* hash) {
 }
 
 Hash hashString(HashType ht, const std::string& s) {
-  Ctx ctx;
+  Ctx ctx{};
   Hash hash(ht);
   start(ht, ctx);
-  update(ht, ctx, (const unsigned char*)s.data(), s.length());
+  update(ht, ctx, reinterpret_cast<const unsigned char*>(s.data()), s.length());
   finish(ht, ctx, hash.hash);
   return hash;
 }
 
 Hash hashFile(HashType ht, const Path& path) {
-  Ctx ctx;
+  Ctx ctx{};
   Hash hash(ht);
   start(ht, ctx);
 
@@ -286,7 +287,7 @@ Hash hashFile(HashType ht, const Path& path) {
   }
 
   std::vector<unsigned char> buf(8192);
-  ssize_t n;
+  ssize_t n = 0;
   while ((n = read(fd.get(), buf.data(), buf.size())) != 0) {
     checkInterrupt();
     if (n == -1) {
diff --git a/third_party/nix/src/libutil/json.cc b/third_party/nix/src/libutil/json.cc
index da636e7be9c3..59ff74f5796f 100644
--- a/third_party/nix/src/libutil/json.cc
+++ b/third_party/nix/src/libutil/json.cc
@@ -18,7 +18,7 @@ void toJSON(std::ostream& str, const char* start, const char* end) {
       str << "\\t";
     } else if (*i >= 0 && *i < 32) {
       str << "\\u" << std::setfill('0') << std::setw(4) << std::hex
-          << (uint16_t)*i << std::dec;
+          << static_cast<uint16_t>(*i) << std::dec;
     } else {
       str << *i;
     }
diff --git a/third_party/nix/src/libutil/serialise.cc b/third_party/nix/src/libutil/serialise.cc
index d02a0205af0d..26ab9be959dc 100644
--- a/third_party/nix/src/libutil/serialise.cc
+++ b/third_party/nix/src/libutil/serialise.cc
@@ -93,10 +93,10 @@ std::string Source::drain() {
   std::string s;
   std::vector<unsigned char> buf(8192);
   while (true) {
-    size_t n;
+    size_t n = 0;
     try {
       n = read(buf.data(), buf.size());
-      s.append((char*)buf.data(), n);
+      s.append(reinterpret_cast<char*>(buf.data()), n);
     } catch (EndOfFile&) {
       break;
     }
@@ -126,10 +126,10 @@ size_t BufferedSource::read(unsigned char* data, size_t len) {
 bool BufferedSource::hasData() { return bufPosOut < bufPosIn; }
 
 size_t FdSource::readUnbuffered(unsigned char* data, size_t len) {
-  ssize_t n;
+  ssize_t n = 0;
   do {
     checkInterrupt();
-    n = ::read(fd, (char*)data, len);
+    n = ::read(fd, reinterpret_cast<char*>(data), len);
   } while (n == -1 && errno == EINTR);
   if (n == -1) {
     _good = false;
@@ -149,7 +149,7 @@ size_t StringSource::read(unsigned char* data, size_t len) {
   if (pos == s.size()) {
     throw EndOfFile("end of string reached");
   }
-  size_t n = s.copy((char*)data, len, pos);
+  size_t n = s.copy(reinterpret_cast<char*>(data), len, pos);
   pos += n;
   return n;
 }
@@ -179,7 +179,7 @@ std::unique_ptr<Source> sinkToSource(const std::function<void(Sink&)>& fun,
         coro = coro_t::pull_type([&](coro_t::push_type& yield) {
           LambdaSink sink([&](const unsigned char* data, size_t len) {
             if (len != 0u) {
-              yield(std::string((const char*)data, len));
+              yield(std::string(reinterpret_cast<const char*>(data), len));
             }
           });
           fun(sink);
@@ -200,7 +200,7 @@ std::unique_ptr<Source> sinkToSource(const std::function<void(Sink&)>& fun,
       }
 
       auto n = std::min(cur.size() - pos, len);
-      memcpy(data, (unsigned char*)cur.data() + pos, n);
+      memcpy(data, reinterpret_cast<unsigned char*>(cur.data()) + pos, n);
       pos += n;
 
       return n;
@@ -225,7 +225,7 @@ void writeString(const unsigned char* buf, size_t len, Sink& sink) {
 }
 
 Sink& operator<<(Sink& sink, const std::string& s) {
-  writeString((const unsigned char*)s.data(), s.size(), sink);
+  writeString(reinterpret_cast<const unsigned char*>(s.data()), s.size(), sink);
   return sink;
 }
 
@@ -276,7 +276,7 @@ std::string readString(Source& source, size_t max) {
     throw SerialisationError("string is too long");
   }
   std::string res(len, 0);
-  source((unsigned char*)res.data(), len);
+  source(reinterpret_cast<unsigned char*>(res.data()), len);
   readPadding(len, source);
   return res;
 }
@@ -305,7 +305,7 @@ void StringSink::operator()(const unsigned char* data, size_t len) {
     warnLargeDump();
     warned = true;
   }
-  s->append((const char*)data, len);
+  s->append(reinterpret_cast<const char*>(data), len);
 }
 
 }  // namespace nix
diff --git a/third_party/nix/src/libutil/util.cc b/third_party/nix/src/libutil/util.cc
index 75a8e73eac77..f2580f7b26af 100644
--- a/third_party/nix/src/libutil/util.cc
+++ b/third_party/nix/src/libutil/util.cc
@@ -207,7 +207,7 @@ bool isDirOrInDir(const Path& path, const Path& dir) {
 }
 
 struct stat lstat(const Path& path) {
-  struct stat st;
+  struct stat st {};
   if (lstat(path.c_str(), &st) != 0) {
     throw SysError(format("getting status of '%1%'") % path);
   }
@@ -215,8 +215,8 @@ struct stat lstat(const Path& path) {
 }
 
 bool pathExists(const Path& path) {
-  int res;
-  struct stat st;
+  int res = 0;
+  struct stat st {};
   res = lstat(path.c_str(), &st);
   if (res == 0) {
     return true;
@@ -254,7 +254,7 @@ DirEntries readDirectory(DIR* dir, const Path& path) {
   DirEntries entries;
   entries.reserve(64);
 
-  struct dirent* dirent;
+  struct dirent* dirent = nullptr;
   while (errno = 0, dirent = readdir(dir)) { /* sic */
     checkInterrupt();
     std::string name = dirent->d_name;
@@ -300,7 +300,7 @@ unsigned char getFileType(const Path& path) {
 }
 
 std::string readFile(int fd) {
-  struct stat st;
+  struct stat st {};
   if (fstat(fd, &st) == -1) {
     throw SysError("statting file");
   }
@@ -308,7 +308,7 @@ std::string readFile(int fd) {
   std::vector<unsigned char> buf(st.st_size);
   readFull(fd, buf.data(), st.st_size);
 
-  return std::string((char*)buf.data(), st.st_size);
+  return std::string(reinterpret_cast<char*>(buf.data()), st.st_size);
 }
 
 std::string readFile(absl::string_view path, bool drain) {
@@ -349,7 +349,7 @@ void writeFile(const Path& path, Source& source, mode_t mode) {
   while (true) {
     try {
       auto n = source.read(buf.data(), buf.size());
-      writeFull(fd.get(), (unsigned char*)buf.data(), n);
+      writeFull(fd.get(), static_cast<unsigned char*>(buf.data()), n);
     } catch (EndOfFile&) {
       break;
     }
@@ -360,7 +360,7 @@ std::string readLine(int fd) {
   std::string s;
   while (true) {
     checkInterrupt();
-    char ch;
+    char ch = 0;
     // FIXME: inefficient
     ssize_t rd = read(fd, &ch, 1);
     if (rd == -1) {
@@ -389,7 +389,7 @@ static void _deletePath(int parentfd, const Path& path,
 
   std::string name(baseNameOf(path));
 
-  struct stat st;
+  struct stat st {};
   if (fstatat(parentfd, name.c_str(), &st, AT_SYMLINK_NOFOLLOW) == -1) {
     if (errno == ENOENT) {
       return;
@@ -449,7 +449,7 @@ static void _deletePath(const Path& path, unsigned long long& bytesFreed) {
 }
 
 void deletePath(const Path& path) {
-  unsigned long long dummy;
+  unsigned long long dummy = 0;
   deletePath(path, dummy);
 }
 
@@ -514,8 +514,8 @@ static Lazy<Path> getHome2([]() {
   Path homeDir = getEnv("HOME");
   if (homeDir.empty()) {
     std::vector<char> buf(16384);
-    struct passwd pwbuf;
-    struct passwd* pw;
+    struct passwd pwbuf {};
+    struct passwd* pw = nullptr;
     if (getpwuid_r(geteuid(), &pwbuf, buf.data(), buf.size(), &pw) != 0 ||
         (pw == nullptr) || (pw->pw_dir == nullptr) || (pw->pw_dir[0] == 0)) {
       throw Error("cannot determine user's home directory");
@@ -567,7 +567,7 @@ Paths createDirs(const Path& path) {
     return created;
   }
 
-  struct stat st;
+  struct stat st {};
   if (lstat(path.c_str(), &st) == -1) {
     created = createDirs(dirOf(path));
     if (mkdir(path.c_str(), 0777) == -1 && errno != EEXIST) {
@@ -619,7 +619,7 @@ void replaceSymlink(const Path& target, const Path& link) {
 void readFull(int fd, unsigned char* buf, size_t count) {
   while (count != 0u) {
     checkInterrupt();
-    ssize_t res = read(fd, (char*)buf, count);
+    ssize_t res = read(fd, reinterpret_cast<char*>(buf), count);
     if (res == -1) {
       if (errno == EINTR) {
         continue;
@@ -652,7 +652,8 @@ void writeFull(int fd, const unsigned char* buf, size_t count,
 }
 
 void writeFull(int fd, const std::string& s, bool allowInterrupts) {
-  writeFull(fd, (const unsigned char*)s.data(), s.size(), allowInterrupts);
+  writeFull(fd, reinterpret_cast<const unsigned char*>(s.data()), s.size(),
+            allowInterrupts);
 }
 
 std::string drainFD(int fd, bool block) {
@@ -662,7 +663,7 @@ std::string drainFD(int fd, bool block) {
 }
 
 void drainFD(int fd, Sink& sink, bool block) {
-  int saved;
+  int saved = 0;
 
   Finally finally([&]() {
     if (!block) {
@@ -829,7 +830,7 @@ int Pid::kill() {
 int Pid::wait() {
   assert(pid != -1);
   while (true) {
-    int status;
+    int status = 0;
     int res = waitpid(pid, &status, 0);
     if (res == pid) {
       pid = -1;
@@ -954,7 +955,7 @@ pid_t startProcess(std::function<void()> fun, const ProcessOptions& options) {
 std::vector<char*> stringsToCharPtrs(const Strings& ss) {
   std::vector<char*> res;
   for (auto& s : ss) {
-    res.push_back((char*)s.c_str());
+    res.push_back(const_cast<char*>(s.c_str()));
   }
   res.push_back(nullptr);
   return res;
@@ -1085,7 +1086,7 @@ void runProgram2(const RunOptions& options) {
       try {
         std::vector<unsigned char> buf(8 * 1024);
         while (true) {
-          size_t n;
+          size_t n = 0;
           try {
             n = source->read(buf.data(), buf.size());
           } catch (EndOfFile&) {
@@ -1144,7 +1145,7 @@ void closeMostFDs(const std::set<int>& exceptions) {
 }
 
 void closeOnExec(int fd) {
-  int prev;
+  int prev = 0;
   if ((prev = fcntl(fd, F_GETFD, 0)) == -1 ||
       fcntl(fd, F_SETFD, prev | FD_CLOEXEC) == -1) {
     throw SysError("setting close-on-exec flag");
@@ -1270,7 +1271,7 @@ std::string filterANSIEscapes(const std::string& s, bool filterAll,
   size_t w = 0;
   auto i = s.begin();
 
-  while (w < (size_t)width && i != s.end()) {
+  while (w < static_cast<size_t>(width) && i != s.end()) {
     if (*i == '\e') {
       std::string e;
       e += *i++;
@@ -1305,7 +1306,7 @@ std::string filterANSIEscapes(const std::string& s, bool filterAll,
       i++;
       t += ' ';
       w++;
-      while (w < (size_t)width && ((w % 8) != 0u)) {
+      while (w < static_cast<size_t>(width) && ((w % 8) != 0u)) {
         t += ' ';
         w++;
       }
@@ -1337,7 +1338,7 @@ void callFailure(const std::function<void(std::exception_ptr exc)>& failure,
 static Sync<std::pair<unsigned short, unsigned short>> windowSize{{0, 0}};
 
 static void updateWindowSize() {
-  struct winsize ws;
+  struct winsize ws {};
   if (ioctl(2, TIOCGWINSZ, &ws) == 0) {
     auto windowSize_(windowSize.lock());
     windowSize_->first = ws.ws_row;
diff --git a/third_party/nix/src/libutil/util.hh b/third_party/nix/src/libutil/util.hh
index d9fc1a27b134..09f23706063a 100644
--- a/third_party/nix/src/libutil/util.hh
+++ b/third_party/nix/src/libutil/util.hh
@@ -163,7 +163,7 @@ void drainFD(int fd, Sink& sink, bool block = true);
 class AutoDelete {
   Path path;
   bool del;
-  bool recursive;
+  bool recursive{};
 
  public:
   AutoDelete();