1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-27 00:11:17 +02:00

Merge branch 'fix/avoid-large-stack-buffers' of https://github.com/dtzWill/nix

This commit is contained in:
Eelco Dolstra 2018-03-22 13:19:25 +01:00
commit f87e286e82
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
5 changed files with 27 additions and 26 deletions

View file

@ -230,16 +230,17 @@ bool pathExists(const Path & path)
Path readLink(const Path & path)
{
checkInterrupt();
std::vector<char> buf;
for (ssize_t bufSize = PATH_MAX/4; true; bufSize += bufSize/2) {
char buf[bufSize];
ssize_t rlSize = readlink(path.c_str(), buf, bufSize);
buf.resize(bufSize);
ssize_t rlSize = readlink(path.c_str(), buf.data(), bufSize);
if (rlSize == -1)
if (errno == EINVAL)
throw Error("'%1%' is not a symlink", path);
else
throw SysError("reading symbolic link '%1%'", path);
else if (rlSize < bufSize)
return string(buf, rlSize);
return string(buf.data(), rlSize);
}
}
@ -294,10 +295,10 @@ string readFile(int fd)
if (fstat(fd, &st) == -1)
throw SysError("statting file");
auto buf = std::make_unique<unsigned char[]>(st.st_size);
readFull(fd, buf.get(), st.st_size);
std::vector<unsigned char> buf(st.st_size);
readFull(fd, buf.data(), st.st_size);
return string((char *) buf.get(), st.st_size);
return string((char *) buf.data(), st.st_size);
}
@ -439,10 +440,10 @@ Path createTempDir(const Path & tmpRoot, const Path & prefix,
static Lazy<Path> getHome2([]() {
Path homeDir = getEnv("HOME");
if (homeDir.empty()) {
char buf[16384];
std::vector<char> buf(16384);
struct passwd pwbuf;
struct passwd * pw;
if (getpwuid_r(getuid(), &pwbuf, buf, sizeof(buf), &pw) != 0
if (getpwuid_r(getuid(), &pwbuf, buf.data(), buf.size(), &pw) != 0
|| !pw || !pw->pw_dir || !pw->pw_dir[0])
throw Error("cannot determine user's home directory");
homeDir = pw->pw_dir;