summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCongcong Kuo <congcong.kuo@gmail.com>2025-07-06 22:51:34 +0800
committerLudovic Courtès <ludo@gnu.org>2025-07-16 23:50:38 +0200
commitbd963ec99d5232df789b20e19b47900b1e27d7e3 (patch)
treee5709ff39eea8d68405389db62f0d9fa48f31595
parent02a94e80243b1ed1f84fc3cce2554f2d06fd1664 (diff)
daemon: Use std::string or std::vector instead of variable-length array (VLA).
* libutil/util.h (waitForMessage): Use std::string instead of char* to unify coding style. * libutil/util.cc (waitForMessage): Use std::string instead of variable-length array (VLA). (readLink, copyFileRecursively, expect): Use std::vector instead of VLA. * libutil/hash.cc (printHash): Use std::vector instead of VLA. Signed-off-by: Ludovic Courtès <ludo@gnu.org>
-rw-r--r--nix/libutil/hash.cc4
-rw-r--r--nix/libutil/util.cc27
-rw-r--r--nix/libutil/util.hh2
3 files changed, 16 insertions, 17 deletions
diff --git a/nix/libutil/hash.cc b/nix/libutil/hash.cc
index 9b83ffcdd9..3cb4d05318 100644
--- a/nix/libutil/hash.cc
+++ b/nix/libutil/hash.cc
@@ -64,12 +64,12 @@ const string base16Chars = "0123456789abcdef";
string printHash(const Hash & hash)
{
- char buf[hash.hashSize * 2];
+ std::vector<char> buf(hash.hashSize * 2);
for (unsigned int i = 0; i < hash.hashSize; i++) {
buf[i * 2] = base16Chars[hash.hash[i] >> 4];
buf[i * 2 + 1] = base16Chars[hash.hash[i] & 0x0f];
}
- return string(buf, hash.hashSize * 2);
+ return string(buf.begin(), buf.end());
}
diff --git a/nix/libutil/util.cc b/nix/libutil/util.cc
index 8938a213f6..9a97270af9 100644
--- a/nix/libutil/util.cc
+++ b/nix/libutil/util.cc
@@ -223,14 +223,14 @@ Path readLink(const Path & path)
struct stat st = lstat(path);
if (!S_ISLNK(st.st_mode))
throw Error(format("`%1%' is not a symlink") % path);
- char buf[st.st_size];
- ssize_t rlsize = readlink(path.c_str(), buf, st.st_size);
+ std::vector<char> buf(st.st_size);
+ ssize_t rlsize = readlink(path.c_str(), buf.data(), st.st_size);
if (rlsize == -1)
throw SysError(format("reading symbolic link '%1%'") % path);
else if (rlsize > st.st_size)
throw Error(format("symbolic link ‘%1%’ size overflow %2% > %3%")
% path % rlsize % st.st_size);
- return string(buf, st.st_size);
+ return string(buf.begin(), buf.end());
}
@@ -481,11 +481,11 @@ static void copyFileRecursively(int sourceroot, const Path &source,
copyFile(sourceFd, destinationFd);
fchown(destinationFd, st.st_uid, st.st_gid);
} else if (S_ISLNK(st.st_mode)) {
- char target[st.st_size + 1];
- ssize_t result = readlinkat(sourceroot, source.c_str(), target, st.st_size);
+ std::vector<char> target(st.st_size + 1);
+ ssize_t result = readlinkat(sourceroot, source.c_str(), target.data(), st.st_size);
if (result != st.st_size) throw SysError("reading symlink target");
target[st.st_size] = '\0';
- int err = symlinkat(target, destinationroot, destination.c_str());
+ int err = symlinkat(target.data(), destinationroot, destination.c_str());
if (err != 0)
throw SysError(format("creating symlink `%1%'") % destination);
fchownat(destinationroot, destination.c_str(),
@@ -749,12 +749,11 @@ string drainFD(int fd)
/* Wait on FD until MESSAGE has been read. */
-void waitForMessage(int fd, const char *message)
+void waitForMessage(int fd, const string & message)
{
- size_t size = strlen(message);
- char str[size] = { '\0' };
- readFull(fd, (unsigned char*)str, size);
- if (strncmp(str, message, size) != 0)
+ string str(message.length(), '\0');
+ readFull(fd, (unsigned char*)str.data(), message.length());
+ if (str != message)
throw Error(format("did not receive message '%1%' on file descriptor %2%")
% message % fd);
}
@@ -1348,9 +1347,9 @@ bool hasSuffix(const string & s, const string & suffix)
void expect(std::istream & str, const string & s)
{
- char s2[s.size()];
- str.read(s2, s.size());
- if (string(s2, s.size()) != s)
+ std::vector<char> s2(s.size());
+ str.read(s2.data(), s2.size());
+ if (string(s2.begin(), s2.end()) != s)
throw FormatError(format("expected string `%1%'") % s);
}
diff --git a/nix/libutil/util.hh b/nix/libutil/util.hh
index 648d6f19a4..7b50dfa5f5 100644
--- a/nix/libutil/util.hh
+++ b/nix/libutil/util.hh
@@ -179,7 +179,7 @@ MakeError(EndOfFile, Error)
/* Read a file descriptor until EOF occurs. */
string drainFD(int fd);
-void waitForMessage(int fd, const char *message);
+void waitForMessage(int fd, const string & message);