1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-12 13:36:02 +01:00

fix(libstore/find-cycles): use readFull for robust file reading

Replaced raw `read()` with `readFull()` helper, which properly
handles partial reads and `EINTR`. The previous code manually checked
for errors but didn't handle the case where `read()` returns fewer
bytes than requested.
This commit is contained in:
Bernardo Meurer Costa 2025-10-11 19:36:51 +00:00
parent 2e9ea55795
commit 591851cd5e
No known key found for this signature in database

View file

@ -107,14 +107,13 @@ void scanForCycleEdges2(const std::string & path, CycleEdgeScanSink & sink)
// Stream file contents into sink // Stream file contents into sink
// RefScanSink handles buffer boundaries automatically // RefScanSink handles buffer boundaries automatically
std::vector<char> buf(65536); std::vector<char> buf(65536);
while (true) { size_t remaining = st.st_size;
ssize_t n = read(fd.get(), buf.data(), buf.size());
if (n == -1)
throw SysError("reading file '%1%'", path);
if (n == 0)
break;
sink(std::string_view(buf.data(), n)); while (remaining > 0) {
size_t toRead = std::min(remaining, buf.size());
readFull(fd.get(), buf.data(), toRead);
sink(std::string_view(buf.data(), toRead));
remaining -= toRead;
} }
} else if (S_ISDIR(st.st_mode)) { } else if (S_ISDIR(st.st_mode)) {
// Handle directories - recursively scan contents // Handle directories - recursively scan contents