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>
This commit is contained in:
Congcong Kuo 2025-07-06 22:51:34 +08:00 committed by Ludovic Courtès
parent 02a94e8024
commit bd963ec99d
No known key found for this signature in database
GPG key ID: 090B11993D9AEBB5
3 changed files with 16 additions and 17 deletions

View file

@ -64,12 +64,12 @@ const string base16Chars = "0123456789abcdef";
string printHash(const Hash & hash) 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++) { for (unsigned int i = 0; i < hash.hashSize; i++) {
buf[i * 2] = base16Chars[hash.hash[i] >> 4]; buf[i * 2] = base16Chars[hash.hash[i] >> 4];
buf[i * 2 + 1] = base16Chars[hash.hash[i] & 0x0f]; buf[i * 2 + 1] = base16Chars[hash.hash[i] & 0x0f];
} }
return string(buf, hash.hashSize * 2); return string(buf.begin(), buf.end());
} }

View file

@ -223,14 +223,14 @@ Path readLink(const Path & path)
struct stat st = lstat(path); struct stat st = lstat(path);
if (!S_ISLNK(st.st_mode)) if (!S_ISLNK(st.st_mode))
throw Error(format("`%1%' is not a symlink") % path); throw Error(format("`%1%' is not a symlink") % path);
char buf[st.st_size]; std::vector<char> buf(st.st_size);
ssize_t rlsize = readlink(path.c_str(), buf, st.st_size); ssize_t rlsize = readlink(path.c_str(), buf.data(), st.st_size);
if (rlsize == -1) if (rlsize == -1)
throw SysError(format("reading symbolic link '%1%'") % path); throw SysError(format("reading symbolic link '%1%'") % path);
else if (rlsize > st.st_size) else if (rlsize > st.st_size)
throw Error(format("symbolic link %1% size overflow %2% > %3%") throw Error(format("symbolic link %1% size overflow %2% > %3%")
% path % rlsize % st.st_size); % 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); copyFile(sourceFd, destinationFd);
fchown(destinationFd, st.st_uid, st.st_gid); fchown(destinationFd, st.st_uid, st.st_gid);
} else if (S_ISLNK(st.st_mode)) { } else if (S_ISLNK(st.st_mode)) {
char target[st.st_size + 1]; std::vector<char> target(st.st_size + 1);
ssize_t result = readlinkat(sourceroot, source.c_str(), target, st.st_size); ssize_t result = readlinkat(sourceroot, source.c_str(), target.data(), st.st_size);
if (result != st.st_size) throw SysError("reading symlink target"); if (result != st.st_size) throw SysError("reading symlink target");
target[st.st_size] = '\0'; 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) if (err != 0)
throw SysError(format("creating symlink `%1%'") % destination); throw SysError(format("creating symlink `%1%'") % destination);
fchownat(destinationroot, destination.c_str(), fchownat(destinationroot, destination.c_str(),
@ -749,12 +749,11 @@ string drainFD(int fd)
/* Wait on FD until MESSAGE has been read. */ /* 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); string str(message.length(), '\0');
char str[size] = { '\0' }; readFull(fd, (unsigned char*)str.data(), message.length());
readFull(fd, (unsigned char*)str, size); if (str != message)
if (strncmp(str, message, size) != 0)
throw Error(format("did not receive message '%1%' on file descriptor %2%") throw Error(format("did not receive message '%1%' on file descriptor %2%")
% message % fd); % message % fd);
} }
@ -1348,9 +1347,9 @@ bool hasSuffix(const string & s, const string & suffix)
void expect(std::istream & str, const string & s) void expect(std::istream & str, const string & s)
{ {
char s2[s.size()]; std::vector<char> s2(s.size());
str.read(s2, s.size()); str.read(s2.data(), s2.size());
if (string(s2, s.size()) != s) if (string(s2.begin(), s2.end()) != s)
throw FormatError(format("expected string `%1%'") % s); throw FormatError(format("expected string `%1%'") % s);
} }

View file

@ -179,7 +179,7 @@ MakeError(EndOfFile, Error)
/* Read a file descriptor until EOF occurs. */ /* Read a file descriptor until EOF occurs. */
string drainFD(int fd); string drainFD(int fd);
void waitForMessage(int fd, const char *message); void waitForMessage(int fd, const string & message);