Skip to content

Commit

Permalink
don't allocate large buffers on the stack
Browse files Browse the repository at this point in the history
  • Loading branch information
dtzWill committed Mar 1, 2018
1 parent f346523 commit b538660
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/libutil/archive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ static string caseHackSuffix = "~nix~case~hack~";

PathFilter defaultPathFilter = [](const Path &) { return true; };

const size_t BUFFER_SIZE = 65536;

static void dumpContents(const Path & path, size_t size,
Sink & sink)
Expand All @@ -40,14 +41,14 @@ static void dumpContents(const Path & path, size_t size,
AutoCloseFD fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
if (!fd) throw SysError(format("opening file '%1%'") % path);

unsigned char buf[65536];
auto buf = std::make_unique<unsigned char[]>(BUFFER_SIZE);
size_t left = size;

while (left > 0) {
size_t n = left > sizeof(buf) ? sizeof(buf) : left;
readFull(fd.get(), buf, n);
size_t n = left > BUFFER_SIZE ? BUFFER_SIZE : left;
readFull(fd.get(), buf.get(), n);
left -= n;
sink(buf, n);
sink(buf.get(), n);
}

writePadding(size, sink);
Expand Down Expand Up @@ -146,14 +147,14 @@ static void parseContents(ParseSink & sink, Source & source, const Path & path)
sink.preallocateContents(size);

unsigned long long left = size;
unsigned char buf[65536];
auto buf = std::make_unique<unsigned char[]>(BUFFER_SIZE);

while (left) {
checkInterrupt();
unsigned int n = sizeof(buf);
if ((unsigned long long) n > left) n = left;
source(buf, n);
sink.receiveContents(buf, n);
auto n = BUFFER_SIZE;
if ((unsigned long long)n > left) n = left;
source(buf.get(), n);
sink.receiveContents(buf.get(), n);
left -= n;
}

Expand Down

0 comments on commit b538660

Please sign in to comment.