diff options
author | Eelco Dolstra <e.dolstra@tudelft.nl> | 2006-10-30T11·56+0000 |
---|---|---|
committer | Eelco Dolstra <e.dolstra@tudelft.nl> | 2006-10-30T11·56+0000 |
commit | 8478cd260f31e8592aacdfaf99c1a4e53e4eb400 (patch) | |
tree | 79c8f3c1cc503d94dc1d0429284d70911efdd84c /src | |
parent | 8d17265ac4f9202d924a109a38db33dac5619f9f (diff) |
* readFile: don't overflow the stack on large files.
Diffstat (limited to 'src')
-rw-r--r-- | src/libutil/util.cc | 16 |
1 files changed, 15 insertions, 1 deletions
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 <class T> +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<unsigned char> d(buf); readFull(fd, buf, st.st_size); return string((char *) buf, st.st_size); |