From 8478cd260f31e8592aacdfaf99c1a4e53e4eb400 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 30 Oct 2006 11:56:09 +0000 Subject: [PATCH] * readFile: don't overflow the stack on large files. --- src/libutil/util.cc | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/libutil/util.cc b/src/libutil/util.cc index de2dd12ad..38e32bfa9 100644 --- a/src/libutil/util.cc +++ b/src/libutil/util.cc @@ -194,12 +194,26 @@ Strings readDirectory(const Path & path) } +template +struct AutoDeleteArray +{ + T * p; + AutoDeleteArray(T * p) : p(p) { } + ~AutoDeleteArray() + { + delete [] p; + } +}; + + string readFile(int fd) { struct stat st; if (fstat(fd, &st) == -1) throw SysError("statting file"); - unsigned char buf[st.st_size]; /* !!! stack space */ + + unsigned char * buf = new unsigned char[st.st_size]; + AutoDeleteArray d(buf); readFull(fd, buf, st.st_size); return string((char *) buf, st.st_size);