diff options
| author | Congcong Kuo <congcong.kuo@gmail.com> | 2025-07-06 22:51:34 +0800 |
|---|---|---|
| committer | Ludovic Courtès <ludo@gnu.org> | 2025-07-16 23:50:38 +0200 |
| commit | bd963ec99d5232df789b20e19b47900b1e27d7e3 (patch) | |
| tree | e5709ff39eea8d68405389db62f0d9fa48f31595 | |
| parent | 02a94e80243b1ed1f84fc3cce2554f2d06fd1664 (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.cc | 4 | ||||
| -rw-r--r-- | nix/libutil/util.cc | 27 | ||||
| -rw-r--r-- | nix/libutil/util.hh | 2 |
3 files changed, 16 insertions, 17 deletions
diff --git a/nix/libutil/hash.cc b/nix/libutil/hash.cc index 9b83ffcdd9b..3cb4d05318b 100644 --- a/nix/libutil/hash.cc +++ b/nix/libutil/hash.cc | |||
| @@ -64,12 +64,12 @@ const string base16Chars = "0123456789abcdef"; | |||
| 64 | 64 | ||
| 65 | string printHash(const Hash & hash) | 65 | string printHash(const Hash & hash) |
| 66 | { | 66 | { |
| 67 | char buf[hash.hashSize * 2]; | 67 | std::vector<char> buf(hash.hashSize * 2); |
| 68 | for (unsigned int i = 0; i < hash.hashSize; i++) { | 68 | for (unsigned int i = 0; i < hash.hashSize; i++) { |
| 69 | buf[i * 2] = base16Chars[hash.hash[i] >> 4]; | 69 | buf[i * 2] = base16Chars[hash.hash[i] >> 4]; |
| 70 | buf[i * 2 + 1] = base16Chars[hash.hash[i] & 0x0f]; | 70 | buf[i * 2 + 1] = base16Chars[hash.hash[i] & 0x0f]; |
| 71 | } | 71 | } |
| 72 | return string(buf, hash.hashSize * 2); | 72 | return string(buf.begin(), buf.end()); |
| 73 | } | 73 | } |
| 74 | 74 | ||
| 75 | 75 | ||
diff --git a/nix/libutil/util.cc b/nix/libutil/util.cc index 8938a213f6d..9a97270af90 100644 --- a/nix/libutil/util.cc +++ b/nix/libutil/util.cc | |||
| @@ -223,14 +223,14 @@ Path readLink(const Path & path) | |||
| 223 | struct stat st = lstat(path); | 223 | struct stat st = lstat(path); |
| 224 | if (!S_ISLNK(st.st_mode)) | 224 | if (!S_ISLNK(st.st_mode)) |
| 225 | throw Error(format("`%1%' is not a symlink") % path); | 225 | throw Error(format("`%1%' is not a symlink") % path); |
| 226 | char buf[st.st_size]; | 226 | std::vector<char> buf(st.st_size); |
| 227 | ssize_t rlsize = readlink(path.c_str(), buf, st.st_size); | 227 | ssize_t rlsize = readlink(path.c_str(), buf.data(), st.st_size); |
| 228 | if (rlsize == -1) | 228 | if (rlsize == -1) |
| 229 | throw SysError(format("reading symbolic link '%1%'") % path); | 229 | throw SysError(format("reading symbolic link '%1%'") % path); |
| 230 | else if (rlsize > st.st_size) | 230 | else if (rlsize > st.st_size) |
| 231 | throw Error(format("symbolic link ‘%1%’ size overflow %2% > %3%") | 231 | throw Error(format("symbolic link ‘%1%’ size overflow %2% > %3%") |
| 232 | % path % rlsize % st.st_size); | 232 | % path % rlsize % st.st_size); |
| 233 | return string(buf, st.st_size); | 233 | return string(buf.begin(), buf.end()); |
| 234 | } | 234 | } |
| 235 | 235 | ||
| 236 | 236 | ||
| @@ -481,11 +481,11 @@ static void copyFileRecursively(int sourceroot, const Path &source, | |||
| 481 | copyFile(sourceFd, destinationFd); | 481 | copyFile(sourceFd, destinationFd); |
| 482 | fchown(destinationFd, st.st_uid, st.st_gid); | 482 | fchown(destinationFd, st.st_uid, st.st_gid); |
| 483 | } else if (S_ISLNK(st.st_mode)) { | 483 | } else if (S_ISLNK(st.st_mode)) { |
| 484 | char target[st.st_size + 1]; | 484 | std::vector<char> target(st.st_size + 1); |
| 485 | ssize_t result = readlinkat(sourceroot, source.c_str(), target, st.st_size); | 485 | ssize_t result = readlinkat(sourceroot, source.c_str(), target.data(), st.st_size); |
| 486 | if (result != st.st_size) throw SysError("reading symlink target"); | 486 | if (result != st.st_size) throw SysError("reading symlink target"); |
| 487 | target[st.st_size] = '\0'; | 487 | target[st.st_size] = '\0'; |
| 488 | int err = symlinkat(target, destinationroot, destination.c_str()); | 488 | int err = symlinkat(target.data(), destinationroot, destination.c_str()); |
| 489 | if (err != 0) | 489 | if (err != 0) |
| 490 | throw SysError(format("creating symlink `%1%'") % destination); | 490 | throw SysError(format("creating symlink `%1%'") % destination); |
| 491 | fchownat(destinationroot, destination.c_str(), | 491 | fchownat(destinationroot, destination.c_str(), |
| @@ -749,12 +749,11 @@ string drainFD(int fd) | |||
| 749 | 749 | ||
| 750 | 750 | ||
| 751 | /* Wait on FD until MESSAGE has been read. */ | 751 | /* Wait on FD until MESSAGE has been read. */ |
| 752 | void waitForMessage(int fd, const char *message) | 752 | void waitForMessage(int fd, const string & message) |
| 753 | { | 753 | { |
| 754 | size_t size = strlen(message); | 754 | string str(message.length(), '\0'); |
| 755 | char str[size] = { '\0' }; | 755 | readFull(fd, (unsigned char*)str.data(), message.length()); |
| 756 | readFull(fd, (unsigned char*)str, size); | 756 | if (str != message) |
| 757 | if (strncmp(str, message, size) != 0) | ||
| 758 | throw Error(format("did not receive message '%1%' on file descriptor %2%") | 757 | throw Error(format("did not receive message '%1%' on file descriptor %2%") |
| 759 | % message % fd); | 758 | % message % fd); |
| 760 | } | 759 | } |
| @@ -1348,9 +1347,9 @@ bool hasSuffix(const string & s, const string & suffix) | |||
| 1348 | 1347 | ||
| 1349 | void expect(std::istream & str, const string & s) | 1348 | void expect(std::istream & str, const string & s) |
| 1350 | { | 1349 | { |
| 1351 | char s2[s.size()]; | 1350 | std::vector<char> s2(s.size()); |
| 1352 | str.read(s2, s.size()); | 1351 | str.read(s2.data(), s2.size()); |
| 1353 | if (string(s2, s.size()) != s) | 1352 | if (string(s2.begin(), s2.end()) != s) |
| 1354 | throw FormatError(format("expected string `%1%'") % s); | 1353 | throw FormatError(format("expected string `%1%'") % s); |
| 1355 | } | 1354 | } |
| 1356 | 1355 | ||
diff --git a/nix/libutil/util.hh b/nix/libutil/util.hh index 648d6f19a4c..7b50dfa5f58 100644 --- a/nix/libutil/util.hh +++ b/nix/libutil/util.hh | |||
| @@ -179,7 +179,7 @@ MakeError(EndOfFile, Error) | |||
| 179 | /* Read a file descriptor until EOF occurs. */ | 179 | /* Read a file descriptor until EOF occurs. */ |
| 180 | string drainFD(int fd); | 180 | string drainFD(int fd); |
| 181 | 181 | ||
| 182 | void waitForMessage(int fd, const char *message); | 182 | void waitForMessage(int fd, const string & message); |
| 183 | 183 | ||
| 184 | 184 | ||
| 185 | 185 | ||
