From 8478cd260f31e8592aacdfaf99c1a4e53e4eb400 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 30 Oct 2006 11:56:09 +0000 Subject: * readFile: don't overflow the stack on large files. --- src/libutil/util.cc | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'src/libutil') diff --git a/src/libutil/util.cc b/src/libutil/util.cc index de2dd12ad42e..38e32bfa9b58 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); -- cgit 1.4.1