From 829af22759b8a99c3b44697365390a945f3acc04 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Tue, 10 Jun 2014 13:30:09 +0200 Subject: Print a warning when loading a large path into memory I.e. if you have a derivation with src = ./huge-directory; you'll get a warning that this is not a good idea. --- src/libutil/serialise.cc | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/libutil/serialise.cc') diff --git a/src/libutil/serialise.cc b/src/libutil/serialise.cc index 6b71f52c151b..92417507508a 100644 --- a/src/libutil/serialise.cc +++ b/src/libutil/serialise.cc @@ -54,8 +54,24 @@ FdSink::~FdSink() } +size_t threshold = 256 * 1024 * 1024; + +static void warnLargeDump() +{ + printMsg(lvlError, "warning: dumping very large path (> 256 MiB); this may run out of memory"); +} + + void FdSink::write(const unsigned char * data, size_t len) { + static bool warned = false; + if (warn && !warned) { + written += len; + if (written > threshold) { + warnLargeDump(); + warned = true; + } + } writeFull(fd, data, len); } @@ -256,4 +272,15 @@ template Paths readStrings(Source & source); template PathSet readStrings(Source & source); +void StringSink::operator () (const unsigned char * data, size_t len) +{ + static bool warned = false; + if (!warned && s.size() > threshold) { + warnLargeDump(); + warned = true; + } + s.append((const char *) data, len); +} + + } -- cgit 1.4.1