[gmime: 7/12] Properly handle fseek/lseek errors



commit af98de5b7e51a208da1bd99027b8b8f48427d3a3
Author: Jeffrey Stedfast <jestedfa microsoft com>
Date:   Thu Apr 6 17:09:18 2017 -0400

    Properly handle fseek/lseek errors

 gmime/gmime-stream-file.c |   14 ++++++++++----
 gmime/gmime-stream-fs.c   |   13 +++++++++----
 2 files changed, 19 insertions(+), 8 deletions(-)
---
diff --git a/gmime/gmime-stream-file.c b/gmime/gmime-stream-file.c
index d69a77b..9573eac 100644
--- a/gmime/gmime-stream-file.c
+++ b/gmime/gmime-stream-file.c
@@ -154,7 +154,8 @@ stream_read (GMimeStream *stream, char *buf, size_t len)
                len = (size_t) MIN (stream->bound_end - stream->position, (gint64) len);
        
        /* make sure we are at the right position */
-       fseek (fstream->fp, (long) stream->position, SEEK_SET);
+       if ((fseek (fstream->fp, (long) stream->position, SEEK_SET)) == -1)
+               return -1;
        
        if ((nread = fread (buf, 1, len, fstream->fp)) > 0)
                stream->position += nread;
@@ -182,7 +183,8 @@ stream_write (GMimeStream *stream, const char *buf, size_t len)
                len = (size_t) MIN (stream->bound_end - stream->position, (gint64) len);
        
        /* make sure we are at the right position */
-       fseek (fstream->fp, (long) stream->position, SEEK_SET);
+       if (fseek (fstream->fp, (long) stream->position, SEEK_SET) == -1)
+               return -1;
 
        if ((nwritten = fwrite (buf, 1, len, fstream->fp)) > 0)
                stream->position += nwritten;
@@ -328,9 +330,13 @@ stream_length (GMimeStream *stream)
        if (stream->bound_start != -1 && stream->bound_end != -1)
                return stream->bound_end - stream->bound_start;
        
-       fseek (fstream->fp, (long) 0, SEEK_END);
+       if (fseek (fstream->fp, (long) 0, SEEK_END) == -1)
+               return -1;
+       
        bound_end = ftell (fstream->fp);
-       fseek (fstream->fp, (long) stream->position, SEEK_SET);
+       
+       if (fseek (fstream->fp, (long) stream->position, SEEK_SET) == -1)
+               return -1;
        
        if (bound_end < stream->bound_start) {
                errno = EINVAL;
diff --git a/gmime/gmime-stream-fs.c b/gmime/gmime-stream-fs.c
index b9f2c21..4d02544 100644
--- a/gmime/gmime-stream-fs.c
+++ b/gmime/gmime-stream-fs.c
@@ -162,7 +162,8 @@ stream_read (GMimeStream *stream, char *buf, size_t len)
                len = (size_t) MIN (stream->bound_end - stream->position, (gint64) len);
        
        /* make sure we are at the right position */
-       lseek (fs->fd, (off_t) stream->position, SEEK_SET);
+       if (lseek (fs->fd, (off_t) stream->position, SEEK_SET) == -1)
+               return -1;
        
        do {
                nread = read (fs->fd, buf, len);
@@ -198,7 +199,8 @@ stream_write (GMimeStream *stream, const char *buf, size_t len)
                len = (size_t) MIN (stream->bound_end - stream->position, (gint64) len);
        
        /* make sure we are at the right position */
-       lseek (fs->fd, (off_t) stream->position, SEEK_SET);
+       if (lseek (fs->fd, (off_t) stream->position, SEEK_SET) == -1)
+               return -1;
        
        do {
                do {
@@ -378,8 +380,11 @@ stream_length (GMimeStream *stream)
        if (stream->bound_end != -1)
                return stream->bound_end - stream->bound_start;
        
-       bound_end = lseek (fs->fd, (off_t) 0, SEEK_END);
-       lseek (fs->fd, (off_t) stream->position, SEEK_SET);
+       if ((bound_end = lseek (fs->fd, (off_t) 0, SEEK_END)) == -1)
+               return -1;
+       
+       if (lseek (fs->fd, (off_t) stream->position, SEEK_SET) == -1)
+               return -1;
        
        if (bound_end < stream->bound_start) {
                errno = EINVAL;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]