diff options
author | Niels Provos <provos@gmail.com> | 2009-01-29 03:22:47 +0000 |
---|---|---|
committer | Niels Provos <provos@gmail.com> | 2009-01-29 03:22:47 +0000 |
commit | a0cae310d05d99a1f235a1814ba5ef7893959ec8 (patch) | |
tree | f842095932253da16e5be4ab4837ce71c060f1da /buffer.c | |
parent | 66b2a7ffb72e68a059c7479362bfe4863e2331df (diff) | |
download | libevent-a0cae310d05d99a1f235a1814ba5ef7893959ec8.tar.gz |
make it so that evbuffer_add_file where we read the complete contents of the file can fail without side effects
svn:r1069
Diffstat (limited to 'buffer.c')
-rw-r--r-- | buffer.c | 18 |
1 files changed, 15 insertions, 3 deletions
@@ -1496,21 +1496,33 @@ evbuffer_add_file(struct evbuffer *outbuf, int fd, #endif { /* the default implementation */ + struct evbuffer *tmp = evbuffer_new(); ssize_t read; - if (lseek(fd, offset, SEEK_SET) == -1) + if (tmp == NULL) + return (-1); + + if (lseek(fd, offset, SEEK_SET) == -1) { + evbuffer_free(tmp); return (-1); + } + /* we add everything to a temporary buffer, so that we + * can abort without side effects if the read fails. + */ while (length) { - read = evbuffer_read(outbuf, fd, length); + read = evbuffer_read(tmp, fd, length); if (read == -1) { - /* TODO(niels): deal with rewinding */ + evbuffer_free(tmp); return (-1); } length -= read; } + evbuffer_add_buffer(outbuf, tmp); + evbuffer_free(tmp); + close(fd); } |