1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-27 12:41:15 +02:00
Cherry-pick https://gerrit.lix.systems/c/lix/+/2100

This change fixes a potential concurrency failure when accessing random
which is not thread safe.

Co-authored-by: Lily Ballard <lily@ballards.net>
This commit is contained in:
Farid Zakaria 2025-05-20 19:13:49 -07:00
parent e22142e11a
commit 6aed9d877c
7 changed files with 35 additions and 37 deletions

View file

@ -8,12 +8,12 @@
#include "nix/util/util.hh"
#include <atomic>
#include <random>
#include <cerrno>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <sstream>
#include <filesystem>
#include <fcntl.h>
@ -27,10 +27,6 @@
# include <io.h>
#endif
#include "nix/util/strings-inline.hh"
#include "util-config-private.hh"
namespace nix {
DirectoryIterator::DirectoryIterator(const std::filesystem::path& p) {
@ -641,6 +637,13 @@ std::pair<AutoCloseFD, Path> createTempFile(const Path & prefix)
return {std::move(fd), tmpl};
}
Path makeTempPath(const Path & root, const Path & suffix)
{
// start the counter at a random value to minimize issues with preexisting temp paths
static std::atomic_uint_fast32_t counter(std::random_device{}());
return fmt("%1%%2%-%3%-%4%", root, suffix, getpid(), counter.fetch_add(1, std::memory_order_relaxed));
}
void createSymlink(const Path & target, const Path & link)
{
try {