From 068f4b147d589f2a219ba917537b53a56089c1ba Mon Sep 17 00:00:00 2001 From: Lily Ballard Date: Sat, 19 Oct 2024 00:39:48 -0700 Subject: [PATCH] libstore: fix sign comparison warnings in darwin platform It's not clear to me if `proc_pidinfo()` or `proc_pidfdinfo()` can actually return negative values, the syscall wrappers convert `-1` into zero and the semantics suggest that negative values don't make sense, but just to be safe we'll preserve the int type until we've checked that it's a positive value. Fixes: https://git.lix.systems/lix-project/lix/issues/548 Change-Id: If575aec6b1e27dba63091c7a0316c7b3788747cd --- src/libstore/platform/darwin.cc | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/libstore/platform/darwin.cc b/src/libstore/platform/darwin.cc index 956fb1e9b..c039bd142 100644 --- a/src/libstore/platform/darwin.cc +++ b/src/libstore/platform/darwin.cc @@ -9,6 +9,7 @@ #include #include +#include #include namespace nix { @@ -18,16 +19,17 @@ void DarwinLocalStore::findPlatformRoots(UncheckedRoots & unchecked) auto storePathRegex = regex::storePathRegex(storeDir); std::vector pids; - int pidBufSize = 1; + std::size_t pidBufSize = 1; while (pidBufSize > pids.size() * sizeof(int)) { // Reserve some extra size so we don't fail too much pids.resize((pidBufSize + pidBufSize / 8) / sizeof(int)); - pidBufSize = proc_listpids(PROC_ALL_PIDS, 0, pids.data(), pids.size() * sizeof(int)); + auto size = proc_listpids(PROC_ALL_PIDS, 0, pids.data(), pids.size() * sizeof(int)); - if (pidBufSize <= 0) { + if (size <= 0) { throw SysError("Listing PIDs"); } + pidBufSize = size; } pids.resize(pidBufSize / sizeof(int)); @@ -53,12 +55,12 @@ void DarwinLocalStore::findPlatformRoots(UncheckedRoots & unchecked) // File descriptors std::vector fds; - int fdBufSize = 1; + std::size_t fdBufSize = 1; while (fdBufSize > fds.size() * sizeof(struct proc_fdinfo)) { // Reserve some extra size so we don't fail too much fds.resize((fdBufSize + fdBufSize / 8) / sizeof(struct proc_fdinfo)); errno = 0; - fdBufSize = proc_pidinfo( + auto size = proc_pidinfo( pid, PROC_PIDLISTFDS, 0, fds.data(), fds.size() * sizeof(struct proc_fdinfo) ); @@ -72,13 +74,15 @@ void DarwinLocalStore::findPlatformRoots(UncheckedRoots & unchecked) // https://github.com/apple-opensource/xnu/blob/4f43d4276fc6a87f2461a3ab18287e4a2e5a1cc0/libsyscall/wrappers/libproc/libproc.c#L100-L110 // https://git.lix.systems/lix-project/lix/issues/446#issuecomment-5483 // FB14695751 - if (fdBufSize <= 0) { + if (size <= 0) { if (errno == 0) { + fdBufSize = 0; break; } else { throw SysError("Listing pid %1% file descriptors", pid); } } + fdBufSize = size; } fds.resize(fdBufSize / sizeof(struct proc_fdinfo));