From 131c8cb4424c475bbdaf41912ba1ca66869322de Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 15 Sep 2020 13:02:59 +0200 Subject: Handle infinite output when extracting metadata (GitHub issue #102) --- libbuild2/file.cxx | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'libbuild2/file.cxx') diff --git a/libbuild2/file.cxx b/libbuild2/file.cxx index e1dde1f..2660b9e 100644 --- a/libbuild2/file.cxx +++ b/libbuild2/file.cxx @@ -3,6 +3,7 @@ #include +#include #include // left, setw() #include @@ -1713,9 +1714,28 @@ namespace build2 try { - ifdstream is (move (pr.in_ofd), fdstream_mode::skip); + ifdstream is (move (pr.in_ofd), ifdstream::badbit); // Note: no skip! + + // What are the odds that we will run some unrelated program which + // will keep writing to stdout until we run out of memory reading? + // Apparently non-negligible (see GitHub issue #102). + // string r; - getline (is, r, '\0'); // Will fail if there is no data. + { + char b[1024]; + while (!eof (is.read (b, sizeof (b)))) + { + r.append (b, sizeof (b)); + if (r.size () > 65536) + { + is.close (); + pr.kill (); + throw_generic_ios_failure (EFBIG, "output too large"); + } + } + r.append (b, static_cast (is.gcount ())); + } + is.close (); // Detect errors. if (pr.wait ()) -- cgit v1.1