gmime r1517 - in trunk: . gmime



Author: fejj
Date: Fri Mar 27 14:41:16 2009
New Revision: 1517
URL: http://svn.gnome.org/viewvc/gmime?rev=1517&view=rev

Log:
2009-03-27  Jeffrey Stedfast  <fejj novell com>

	* gmime/gmime-stream-buffer.c: Fixed to be more diligent in setting
	errno on error.

	* gmime/gmime-stream-mmap.c: Fixed to be more diligent in setting
	errno on error.

	* gmime/gmime-stream-mem.c: Fixed to be more diligent in setting
	errno on error.
	(stream_seek): If the user seeks beyond the end of the buffer and
	bound_end is unbound, then grow the buffer (e.g. a sparse file).

	* gmime/gmime-stream-file.c: Fixed to be more diligent in setting
	errno on error.

	* gmime/gmime-stream-fs.c: Fixed to be more diligent in setting
	errno on error.



Modified:
   trunk/ChangeLog
   trunk/gmime/gmime-stream-buffer.c
   trunk/gmime/gmime-stream-file.c
   trunk/gmime/gmime-stream-fs.c
   trunk/gmime/gmime-stream-mem.c
   trunk/gmime/gmime-stream-mmap.c

Modified: trunk/gmime/gmime-stream-buffer.c
==============================================================================
--- trunk/gmime/gmime-stream-buffer.c	(original)
+++ trunk/gmime/gmime-stream-buffer.c	Fri Mar 27 14:41:16 2009
@@ -24,6 +24,7 @@
 #endif
 
 #include <string.h>
+#include <errno.h>
 
 #include "gmime-stream-buffer.h"
 
@@ -144,6 +145,11 @@
 	ssize_t n, nread = 0;
 	size_t offset;
 	
+	if (buffer->source == NULL) {
+		errno = EBADF;
+		return -1;
+	}
+	
 	switch (buffer->mode) {
 	case GMIME_STREAM_BUFFER_BLOCK_READ:
 		while (len > 0) {
@@ -233,6 +239,11 @@
 	ssize_t n, nwritten = 0;
 	size_t left = len;
 	
+	if (buffer->source == NULL) {
+		errno = EBADF;
+		return -1;
+	}
+	
 	switch (buffer->mode) {
 	case GMIME_STREAM_BUFFER_BLOCK_WRITE:
 		while (left > 0) {
@@ -344,6 +355,9 @@
 {
 	GMimeStreamBuffer *buffer = (GMimeStreamBuffer *) stream;
 	
+	if (buffer->source == NULL)
+		return TRUE;
+	
 	if (!g_mime_stream_eos (buffer->source))
 		return FALSE;
 	
@@ -364,6 +378,11 @@
 {
 	GMimeStreamBuffer *buffer = (GMimeStreamBuffer *) stream;
 	
+	if (buffer->source == NULL) {
+		errno = EBADF;
+		return -1;
+	}
+	
 	switch (buffer->mode) {
 	case GMIME_STREAM_BUFFER_BLOCK_READ:
 	case GMIME_STREAM_BUFFER_BLOCK_WRITE:
@@ -401,8 +420,11 @@
 		if (stream->position == offset)
 			return stream->position;
 		
-		if (offset < 0)
+		if (offset < 0) {
+			/* not allowed to seek to a negative position */
+			errno = EINVAL;
 			return -1;
+		}
 		
 		offset -= stream->position;
 		break;
@@ -420,12 +442,17 @@
 			return real;
 		}
 		
-		if (offset > 0)
+		if (offset > 0) {
+			/* not allowed to seek past bound_end */
+			errno = EINVAL;
 			return -1;
+		}
 		
 		offset += stream->bound_end;
 		break;
 	default:
+		/* invalid whence argument */
+		errno = EINVAL;
 		return -1;
 	}
 	
@@ -474,16 +501,27 @@
 		break;
 	case GMIME_STREAM_SEEK_END:
 		if (stream->bound_end == -1) {
-			real = g_mime_stream_seek (buffer->source, offset, whence);
-			if (real == -1 || real < stream->bound_start)
+			/* we have to do a real seek because the end boundary is unknown */
+			if ((real = g_mime_stream_seek (buffer->source, offset, whence)) == -1)
+				return -1;
+			
+			if (real < stream->bound_start) {
+				/* seek offset out of bounds */
+				errno = EINVAL;
 				return -1;
+			}
 		} else {
 			real = stream->bound_end + offset;
-			if (real > stream->bound_end || real < stream->bound_start)
+			if (real > stream->bound_end || real < stream->bound_start) {
+				/* seek offset out of bounds */
+				errno = EINVAL;
 				return -1;
+			}
 		}
 		break;
 	default:
+		/* invalid whence argument */
+		errno = EINVAL;
 		return -1;
 	}
 	
@@ -518,9 +556,12 @@
 		if (total < len) {
 			/* we failed to seek that far so reset our bufptr */
 			buffer->bufptr = buffer->buffer + pos;
+			errno = EINVAL;
 			return -1;
 		}
 	} else if (real < stream->bound_start) {
+		/* seek offset out of bounds */
+		errno = EINVAL;
 		return -1;
 	} else {
 		/* seek our cache pointer backwards */
@@ -535,10 +576,14 @@
 static gint64
 stream_seek (GMimeStream *stream, gint64 offset, GMimeSeekWhence whence)
 {
-	/* FIXME: set errno appropriately?? */
 	GMimeStreamBuffer *buffer = (GMimeStreamBuffer *) stream;
 	gint64 real;
 	
+	if (buffer->source == NULL) {
+		errno = EBADF;
+		return -1;
+	}
+	
 	switch (buffer->mode) {
 	case GMIME_STREAM_BUFFER_BLOCK_WRITE:
 		if (stream_flush (stream) != 0)
@@ -555,6 +600,8 @@
 	case GMIME_STREAM_BUFFER_CACHE_READ:
 		return stream_seek_cache_read (stream, offset, whence);
 	default:
+		/* invalid whence argument */
+		errno = EINVAL;
 		return -1;
 	}
 }
@@ -562,13 +609,27 @@
 static gint64
 stream_tell (GMimeStream *stream)
 {
+	GMimeStreamBuffer *buffer = (GMimeStreamBuffer *) stream;
+	
+	if (buffer->source == NULL) {
+		errno = EBADF;
+		return -1;
+	}
+	
 	return stream->position;
 }
 
 static ssize_t
 stream_length (GMimeStream *stream)
 {
-	return g_mime_stream_length (GMIME_STREAM_BUFFER (stream)->source);
+	GMimeStreamBuffer *buffer = (GMimeStreamBuffer *) stream;
+	
+	if (buffer->source == NULL) {
+		errno = EBADF;
+		return -1;
+	}
+	
+	return g_mime_stream_length (buffer->source);
 }
 
 static GMimeStream *

Modified: trunk/gmime/gmime-stream-file.c
==============================================================================
--- trunk/gmime/gmime-stream-file.c	(original)
+++ trunk/gmime/gmime-stream-file.c	Fri Mar 27 14:41:16 2009
@@ -23,6 +23,8 @@
 #include <config.h>
 #endif
 
+#include <errno.h>
+
 #include "gmime-stream-file.h"
 
 
@@ -131,8 +133,15 @@
 	GMimeStreamFile *fstream = (GMimeStreamFile *) stream;
 	size_t nread;
 	
-	if (stream->bound_end != -1 && stream->position >= stream->bound_end)
+	if (fstream->fp == NULL) {
+		errno = EBADF;
+		return -1;
+	}
+	
+	if (stream->bound_end != -1 && stream->position >= stream->bound_end) {
+		errno = EINVAL;
 		return -1;
+	}
 	
 	if (stream->bound_end != -1)
 		len = MIN (stream->bound_end - stream->position, (gint64) len);
@@ -152,8 +161,15 @@
 	GMimeStreamFile *fstream = (GMimeStreamFile *) stream;
 	size_t nwritten;
 	
-	if (stream->bound_end != -1 && stream->position >= stream->bound_end)
+	if (fstream->fp == NULL) {
+		errno = EBADF;
 		return -1;
+	}
+	
+	if (stream->bound_end != -1 && stream->position >= stream->bound_end) {
+		errno = EINVAL;
+		return -1;
+	}
 	
 	if (stream->bound_end != -1)
 		len = MIN (stream->bound_end - stream->position, (gint64) len);
@@ -172,7 +188,10 @@
 {
 	GMimeStreamFile *fstream = (GMimeStreamFile *) stream;
 	
-	g_return_val_if_fail (fstream->fp != NULL, -1);
+	if (fstream->fp == NULL) {
+		errno = EBADF;
+		return -1;
+	}
 	
 	return fflush (fstream->fp);
 }
@@ -197,7 +216,8 @@
 {
 	GMimeStreamFile *fstream = (GMimeStreamFile *) stream;
 	
-	g_return_val_if_fail (fstream->fp != NULL, TRUE);
+	if (fstream->fp == NULL)
+		return TRUE;
 	
 	if (stream->bound_end == -1)
 		return feof (fstream->fp) ? TRUE : FALSE;
@@ -210,8 +230,10 @@
 {
 	GMimeStreamFile *fstream = (GMimeStreamFile *) stream;
 	
-	if (fstream->fp == NULL)
+	if (fstream->fp == NULL) {
+		errno = EBADF;
 		return -1;
+	}
 	
 	if (stream->position == stream->bound_start)
 		return 0;
@@ -229,7 +251,10 @@
 	gint64 real = stream->position;
 	FILE *fp = fstream->fp;
 	
-	g_return_val_if_fail (fstream->fp != NULL, -1);
+	if (fstream->fp == NULL) {
+		errno = EBADF;
+		return -1;
+	}
 	
 	switch (whence) {
 	case GMIME_STREAM_SEEK_SET:
@@ -258,11 +283,15 @@
 	}
 	
 	/* sanity check the resultant offset */
-	if (real < stream->bound_start)
+	if (real < stream->bound_start) {
+		errno = EINVAL;
 		return -1;
+	}
 	
-	if (stream->bound_end != -1 && real > stream->bound_end)
+	if (stream->bound_end != -1 && real > stream->bound_end) {
+		errno = EINVAL;
 		return -1;
+	}
 	
 	if (fseek (fp, (long) real, SEEK_SET) == -1 || (real = ftell (fp)) == -1)
 		return -1;
@@ -284,6 +313,11 @@
 	GMimeStreamFile *fstream = (GMimeStreamFile *) stream;
 	gint64 bound_end;
 	
+	if (fstream->fp == NULL) {
+		errno = EBADF;
+		return -1;
+	}
+	
 	if (stream->bound_start != -1 && stream->bound_end != -1)
 		return stream->bound_end - stream->bound_start;
 	
@@ -291,8 +325,10 @@
 	bound_end = ftell (fstream->fp);
 	fseek (fstream->fp, (long) stream->position, SEEK_SET);
 	
-	if (bound_end < stream->bound_start)
+	if (bound_end < stream->bound_start) {
+		errno = EINVAL;
 		return -1;
+	}
 	
 	return bound_end - stream->bound_start;
 }

Modified: trunk/gmime/gmime-stream-fs.c
==============================================================================
--- trunk/gmime/gmime-stream-fs.c	(original)
+++ trunk/gmime/gmime-stream-fs.c	Fri Mar 27 14:41:16 2009
@@ -138,8 +138,15 @@
 	GMimeStreamFs *fs = (GMimeStreamFs *) stream;
 	ssize_t nread;
 	
-	if (stream->bound_end != -1 && stream->position >= stream->bound_end)
+	if (fs->fd == -1) {
+		errno = EBADF;
 		return -1;
+	}
+	
+	if (stream->bound_end != -1 && stream->position >= stream->bound_end) {
+		errno = EINVAL;
+		return -1;
+	}
 	
 	if (stream->bound_end != -1)
 		len = MIN (stream->bound_end - stream->position, (gint64) len);
@@ -166,8 +173,15 @@
 	size_t nwritten = 0;
 	ssize_t n;
 	
-	if (stream->bound_end != -1 && stream->position >= stream->bound_end)
+	if (fs->fd == -1) {
+		errno = EBADF;
 		return -1;
+	}
+	
+	if (stream->bound_end != -1 && stream->position >= stream->bound_end) {
+		errno = EINVAL;
+		return -1;
+	}
 	
 	if (stream->bound_end != -1)
 		len = MIN (stream->bound_end - stream->position, (gint64) len);
@@ -200,7 +214,10 @@
 {
 	GMimeStreamFs *fs = (GMimeStreamFs *) stream;
 	
-	g_return_val_if_fail (fs->fd != -1, -1);
+	if (fs->fd == -1) {
+		errno = EBADF;
+		return -1;
+	}
 	
 	return fsync (fs->fd);
 }
@@ -227,7 +244,8 @@
 {
 	GMimeStreamFs *fs = (GMimeStreamFs *) stream;
 	
-	g_return_val_if_fail (fs->fd != -1, TRUE);
+	if (fs->fd == -1)
+		return TRUE;
 	
 	return fs->eos;
 }
@@ -237,8 +255,10 @@
 {
 	GMimeStreamFs *fs = (GMimeStreamFs *) stream;
 	
-	if (fs->fd == -1)
+	if (fs->fd == -1) {
+		errno = EBADF;
 		return -1;
+	}
 	
 	if (stream->position == stream->bound_start) {
 		fs->eos = FALSE;
@@ -262,7 +282,10 @@
 	GMimeStreamFs *fs = (GMimeStreamFs *) stream;
 	gint64 real;
 	
-	g_return_val_if_fail (fs->fd != -1, -1);
+	if (fs->fd == -1) {
+		errno = EBADF;
+		return -1;
+	}
 	
 	switch (whence) {
 	case GMIME_STREAM_SEEK_SET:
@@ -295,15 +318,19 @@
 	}
 	
 	/* sanity check the resultant offset */
-	if (real < stream->bound_start)
+	if (real < stream->bound_start) {
+		errno = EINVAL;
 		return -1;
+	}
 	
 	/* short-cut if we are seeking to our current position */
 	if (real == stream->position)
 		return real;
 	
-	if (stream->bound_end != -1 && real > stream->bound_end)
+	if (stream->bound_end != -1 && real > stream->bound_end) {
+		errno = EINVAL;
 		return -1;
+	}
 	
 	if ((real = lseek (fs->fd, (off_t) real, SEEK_SET)) == -1)
 		return -1;
@@ -330,14 +357,21 @@
 	GMimeStreamFs *fs = (GMimeStreamFs *) stream;
 	gint64 bound_end;
 	
+	if (fs->fd == -1) {
+		errno = EBADF;
+		return -1;
+	}
+	
 	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 < stream->bound_start)
+	if (bound_end < stream->bound_start) {
+		errno = EINVAL;
 		return -1;
+	}
 	
 	return bound_end - stream->bound_start;
 }

Modified: trunk/gmime/gmime-stream-mem.c
==============================================================================
--- trunk/gmime/gmime-stream-mem.c	(original)
+++ trunk/gmime/gmime-stream-mem.c	Fri Mar 27 14:41:16 2009
@@ -24,6 +24,7 @@
 #endif
 
 #include <string.h>
+#include <errno.h>
 
 #include "gmime-stream-mem.h"
 
@@ -131,7 +132,10 @@
 	gint64 bound_end;
 	ssize_t n;
 	
-	g_return_val_if_fail (mem->buffer != NULL, -1);
+	if (mem->buffer == NULL) {
+		errno = EBADF;
+		return -1;
+	}
 	
 	bound_end = stream->bound_end != -1 ? stream->bound_end : (gint64) mem->buffer->len;
 	
@@ -140,7 +144,7 @@
 		memcpy (buf, mem->buffer->data + stream->position, n);
 		stream->position += n;
 	} else if (n < 0) {
-		/* set errno?? */
+		errno = EINVAL;
 		n = -1;
 	}
 	
@@ -154,7 +158,10 @@
 	gint64 bound_end;
 	ssize_t n;
 	
-	g_return_val_if_fail (mem->buffer != NULL, -1);
+	if (mem->buffer == NULL) {
+		errno = EBADF;
+		return -1;
+	}
 	
 	if (stream->bound_end == -1 && stream->position + len > mem->buffer->len) {
 		g_byte_array_set_size (mem->buffer, stream->position + len);
@@ -167,7 +174,7 @@
 		memcpy (mem->buffer->data + stream->position, buf, n);
 		stream->position += n;
 	} else if (n < 0) {
-		/* FIXME: set errno?? */
+		errno = EINVAL;
 		n = -1;
 	}
 	
@@ -177,7 +184,13 @@
 static int
 stream_flush (GMimeStream *stream)
 {
-	/* no-op */
+	GMimeStreamMem *mem = (GMimeStreamMem *) stream;
+	
+	if (mem->buffer == NULL) {
+		errno = EBADF;
+		return -1;
+	}
+	
 	return 0;
 }
 
@@ -190,7 +203,6 @@
 		g_byte_array_free (mem->buffer, TRUE);
 	
 	mem->buffer = NULL;
-	stream->position = 0;
 	
 	return 0;
 }
@@ -201,7 +213,8 @@
 	GMimeStreamMem *mem = (GMimeStreamMem *) stream;
 	gint64 bound_end;
 	
-	g_return_val_if_fail (mem->buffer != NULL, TRUE);
+	if (mem->buffer == NULL)
+		return TRUE;
 	
 	bound_end = stream->bound_end != -1 ? stream->bound_end : (gint64) mem->buffer->len;
 	
@@ -213,7 +226,12 @@
 {
 	GMimeStreamMem *mem = (GMimeStreamMem *) stream;
 	
-	return mem->buffer ? 0 : -1;
+	if (mem->buffer == NULL) {
+		errno = EBADF;
+		return -1;
+	}
+	
+	return 0;
 }
 
 static gint64
@@ -222,7 +240,10 @@
 	GMimeStreamMem *mem = (GMimeStreamMem *) stream;
 	gint64 bound_end, real = stream->position;
 	
-	g_return_val_if_fail (mem->buffer != NULL, -1);
+	if (mem->buffer == NULL) {
+		errno = EBADF;
+		return -1;
+	}
 	
 	bound_end = stream->bound_end != -1 ? stream->bound_end : (gint64) mem->buffer->len;
 	
@@ -238,10 +259,24 @@
 		break;
 	}
 	
-	if (real < stream->bound_start)
-		real = stream->bound_start;
-	else if (real > bound_end)
-		real = bound_end;
+	if (real < stream->bound_start) {
+		errno = EINVAL;
+		return -1;
+	}
+	
+	if (stream->bound_end != -1 && real > bound_end) {
+		errno = EINVAL;
+		return -1;
+	}
+	
+	if (real > bound_end) {
+		if (real > G_MAXUINT) {
+			errno = ENOSPC;
+			return -1;
+		}
+		
+		g_byte_array_set_size (mem->buffer, real);
+	}
 	
 	stream->position = real;
 	
@@ -253,7 +288,10 @@
 {
 	GMimeStreamMem *mem = (GMimeStreamMem *) stream;
 	
-	g_return_val_if_fail (mem->buffer != NULL, -1);
+	if (mem->buffer == NULL) {
+		errno = EBADF;
+		return -1;
+	}
 	
 	return stream->position;
 }
@@ -264,7 +302,10 @@
 	GMimeStreamMem *mem = GMIME_STREAM_MEM (stream);
 	gint64 bound_end;
 	
-	g_return_val_if_fail (mem->buffer != NULL, -1);
+	if (mem->buffer == NULL) {
+		errno = EBADF;
+		return -1;
+	}
 	
 	bound_end = stream->bound_end != -1 ? stream->bound_end : (gint64) mem->buffer->len;
 	

Modified: trunk/gmime/gmime-stream-mmap.c
==============================================================================
--- trunk/gmime/gmime-stream-mmap.c	(original)
+++ trunk/gmime/gmime-stream-mmap.c	Fri Mar 27 14:41:16 2009
@@ -151,8 +151,15 @@
 	register char *mapptr;
 	ssize_t nread;
 	
-	if (stream->bound_end != -1 && stream->position >= stream->bound_end)
+	if (mstream->fd == -1) {
+		errno = EBADF;
 		return -1;
+	}
+	
+	if (stream->bound_end != -1 && stream->position >= stream->bound_end) {
+		errno = EINVAL;
+		return -1;
+	}
 	
 	/* make sure we are at the right position */
 	mapptr = mstream->map + stream->position;
@@ -178,8 +185,15 @@
 	register char *mapptr;
 	ssize_t nwritten;
 	
-	if (stream->bound_end != -1 && stream->position >= stream->bound_end)
+	if (mstream->fd == -1) {
+		errno = EBADF;
 		return -1;
+	}
+	
+	if (stream->bound_end != -1 && stream->position >= stream->bound_end) {
+		errno = EINVAL;
+		return -1;
+	}
 	
 	/* make sure we are at the right position */
 	mapptr = mstream->map + stream->position;
@@ -202,7 +216,10 @@
 {
 	GMimeStreamMmap *mstream = (GMimeStreamMmap *) stream;
 	
-	g_return_val_if_fail (mstream->fd != -1, -1);
+	if (mstream->fd == -1) {
+		errno = EBADF;
+		return -1;
+	}
 	
 #ifdef HAVE_MSYNC
 	return msync (mstream->map, mstream->maplen, MS_SYNC /* | MS_INVALIDATE */);
@@ -237,7 +254,8 @@
 {
 	GMimeStreamMmap *mstream = (GMimeStreamMmap *) stream;
 	
-	g_return_val_if_fail (mstream->fd != -1, TRUE);
+	if (mstream->fd == -1)
+		return TRUE;
 	
 	return mstream->eos;
 }
@@ -247,8 +265,10 @@
 {
 	GMimeStreamMmap *mstream = (GMimeStreamMmap *) stream;
 	
-	if (mstream->fd == -1)
+	if (mstream->fd == -1) {
+		errno = EBADF;
 		return -1;
+	}
 	
 	mstream->eos = FALSE;
 	
@@ -261,7 +281,10 @@
 	GMimeStreamMmap *mstream = (GMimeStreamMmap *) stream;
 	gint64 real = stream->position;
 	
-	g_return_val_if_fail (mstream->fd != -1, -1);
+	if (mstream->fd == -1) {
+		errno = EBADF;
+		return -1;
+	}
 	
 	switch (whence) {
 	case GMIME_STREAM_SEEK_SET:
@@ -286,11 +309,15 @@
 	}
 	
 	/* sanity check the resultant offset */
-	if (real < stream->bound_start)
+	if (real < stream->bound_start) {
+		errno = EINVAL;
 		return -1;
+	}
 	
-	if (stream->bound_end != -1 && real > stream->bound_end)
+	if (stream->bound_end != -1 && real > stream->bound_end) {
+		errno = EINVAL;
 		return -1;
+	}
 	
 	/* reset eos if appropriate */
 	if ((stream->bound_end != -1 && real < stream->bound_end) ||
@@ -305,6 +332,13 @@
 static gint64
 stream_tell (GMimeStream *stream)
 {
+	GMimeStreamMmap *mstream = (GMimeStreamMmap *) stream;
+	
+	if (mstream->fd == -1) {
+		errno = EBADF;
+		return -1;
+	}
+	
 	return stream->position;
 }
 
@@ -313,6 +347,11 @@
 {
 	GMimeStreamMmap *mstream = (GMimeStreamMmap *) stream;
 	
+	if (mstream->fd == -1) {
+		errno = EBADF;
+		return -1;
+	}
+	
 	if (stream->bound_start != -1 && stream->bound_end != -1)
 		return stream->bound_end - stream->bound_start;
 	
@@ -356,8 +395,7 @@
 	gint64 start;
 	char *map;
 	
-	start = lseek (fd, 0, SEEK_CUR);
-	if (start == -1)
+	if ((start = lseek (fd, 0, SEEK_CUR)) == -1)
 		return NULL;
 	
 	if (fstat (fd, &st) == -1)



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