Changeset: cef25fb5a7f3 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=cef25fb5a7f3
Modified Files:
        common/stream/stream.c
Branch: Jan2014
Log Message:

Do a lot of sanity checking in the stream library.
Also, if opening a file fails, return NULL instead of a stream with
the error flag set.


diffs (truncated from 1197 to 300 lines):

diff --git a/common/stream/stream.c b/common/stream/stream.c
--- a/common/stream/stream.c
+++ b/common/stream/stream.c
@@ -194,6 +194,8 @@ mnstr_init(void)
 ssize_t
 mnstr_read(stream *s, void *buf, size_t elmsize, size_t cnt)
 {
+       if (s == NULL)
+               return -1;
 #ifdef STREAM_DEBUG
        printf("read %s " SZFMT " " SZFMT "\n", s->name ? s->name : 
"<unnamed>", elmsize, cnt);
 #endif
@@ -211,6 +213,8 @@ mnstr_readline(stream *s, void *buf, siz
 {
        char *b = buf, *start = buf;
 
+       if (s == NULL)
+               return -1;
 #ifdef STREAM_DEBUG
        printf("readline %s " SZFMT "\n", s->name ? s->name : "<unnamed>", 
maxcnt);
 #endif
@@ -266,6 +270,8 @@ mnstr_readline(stream *s, void *buf, siz
 ssize_t
 mnstr_write(stream *s, const void *buf, size_t elmsize, size_t cnt)
 {
+       if (s == NULL || buf == NULL)
+               return -1;
 #ifdef STREAM_DEBUG
        printf("write %s " SZFMT " " SZFMT "\n", s->name ? s->name : 
"<unnamed>", elmsize, cnt);
 #endif
@@ -278,9 +284,11 @@ mnstr_write(stream *s, const void *buf, 
 void
 mnstr_settimeout(stream *s, unsigned int secs)
 {
-       s->timeout = secs;
-       if (s->update_timeout)
-               (*s->update_timeout)(s);
+       if (s) {
+               s->timeout = secs;
+               if (s->update_timeout)
+                       (*s->update_timeout)(s);
+       }
 }
 
 void
@@ -308,7 +316,7 @@ mnstr_destroy(stream *s)
 char *
 mnstr_error(stream *s)
 {
-       if (s == 0)
+       if (s == NULL)
                return "Connection terminated";
        return (*s->error) (s);
 }
@@ -317,7 +325,7 @@ mnstr_error(stream *s)
 int
 mnstr_flush(stream *s)
 {
-       if (!s)
+       if (s == NULL)
                return -1;
 #ifdef STREAM_DEBUG
        printf("flush %s\n", s->name ? s->name : "<unnamed>");
@@ -334,7 +342,7 @@ mnstr_flush(stream *s)
 int
 mnstr_fsync(stream *s)
 {
-       if (!s)
+       if (s == NULL)
                return -1;
 #ifdef STREAM_DEBUG
        printf("fsync %s (%d)\n", s->name ? s->name : "<unnamed>", s->errnr);
@@ -350,7 +358,7 @@ mnstr_fsync(stream *s)
 int
 mnstr_fgetpos(stream *s, lng *p)
 {
-       if (!s)
+       if (s == NULL)
                return -1;
 #ifdef STREAM_DEBUG
        printf("fgetpos %s\n", s->name ? s->name : "<unnamed>");
@@ -365,7 +373,7 @@ mnstr_fgetpos(stream *s, lng *p)
 int
 mnstr_fsetpos(stream *s, lng p)
 {
-       if (!s)
+       if (s == NULL)
                return -1;
 #ifdef STREAM_DEBUG
        printf("fsetpos %s\n", s->name ? s->name : "<unnamed>");
@@ -381,7 +389,7 @@ mnstr_fsetpos(stream *s, lng p)
 char *
 mnstr_name(stream *s)
 {
-       if (s == 0)
+       if (s == NULL)
                return "connection terminated";
        return s->name;
 }
@@ -389,7 +397,7 @@ mnstr_name(stream *s)
 int
 mnstr_errnr(stream *s)
 {
-       if (s == 0)
+       if (s == NULL)
                return MNSTR_READ_ERROR;
        return s->errnr;
 }
@@ -407,7 +415,7 @@ mnstr_clearerr(stream *s)
 int
 mnstr_type(stream *s)
 {
-       if (s == 0)
+       if (s == NULL)
                return 0;
        return s->type;
 }
@@ -415,7 +423,7 @@ mnstr_type(stream *s)
 int
 mnstr_byteorder(stream *s)
 {
-       if (s == 0)
+       if (s == NULL)
                return 0;
        return s->byteorder;
 }
@@ -423,6 +431,8 @@ mnstr_byteorder(stream *s)
 void
 mnstr_set_byteorder(stream *s, char bigendian)
 {
+       if (s == NULL)
+               return;
 #ifdef STREAM_DEBUG
        printf("mnstr_set_byteorder %s\n", s->name ? s->name : "<unnamed>");
 #endif
@@ -439,13 +449,17 @@ mnstr_set_byteorder(stream *s, char bige
 void
 close_stream(stream *s)
 {
-       s->close(s);
-       s->destroy(s);
+       if (s) {
+               s->close(s);
+               s->destroy(s);
+       }
 }
 
 stream *
 mnstr_rstream(stream *s)
 {
+       if (s == NULL)
+               return NULL;
 #ifdef STREAM_DEBUG
        printf("mnstr_rstream %s\n", s->name ? s->name : "<unnamed>");
 #endif
@@ -459,6 +473,8 @@ mnstr_rstream(stream *s)
 stream *
 mnstr_wstream(stream *s)
 {
+       if (s == NULL)
+               return NULL;
 #ifdef STREAM_DEBUG
        printf("mnstr_wstream %s\n", s->name ? s->name : "<unnamed>");
 #endif
@@ -511,6 +527,8 @@ create_stream(const char *name)
 {
        stream *s;
 
+       if (name == NULL)
+               return NULL;
        if ((s = (stream *) malloc(sizeof(*s))) == NULL)
                return NULL;
        s->byteorder = 1234;
@@ -581,7 +599,7 @@ file_close(stream *s)
 {
        FILE *fp = (FILE *) s->stream_data.p;
 
-       if (!fp)
+       if (fp == NULL)
                return;
        if (fp != stdin && fp != stdout && fp != stderr) {
                if (s->name && *s->name == '|')
@@ -694,8 +712,10 @@ open_stream(const char *filename, const 
 
        if ((s = create_stream(filename)) == NULL)
                return NULL;
-       if ((fp = fopen(filename, flags)) == NULL)
-               s->errnr = MNSTR_OPEN_ERROR;
+       if ((fp = fopen(filename, flags)) == NULL) {
+               destroy(s);
+               return NULL;
+       }
        s->read = file_read;
        s->write = file_write;
        s->close = file_close;
@@ -708,8 +728,7 @@ open_stream(const char *filename, const 
        /* if file is opened for reading, and it starts with the UTF-8
         * encoding of the Unicode Byte Order Mark, skip the mark, and
         * mark the stream as being a UTF-8 stream */
-       if (fp != NULL &&
-           flags[0] == 'r' &&
+       if (flags[0] == 'r' &&
            file_fgetpos(s, &pos) == 0) {
                if (file_read(s, buf, 1, UTF8BOMLENGTH) == 3 &&
                    strncmp(buf, UTF8BOM, UTF8BOMLENGTH) == 0)
@@ -785,8 +804,10 @@ open_gzstream(const char *filename, cons
 
        if ((s = create_stream(filename)) == NULL)
                return NULL;
-       if ((fp = gzopen(filename, flags)) == NULL)
-               s->errnr = MNSTR_OPEN_ERROR;
+       if ((fp = gzopen(filename, flags)) == NULL) {
+               destroy(s);
+               return NULL;
+       }
        s->read = stream_gzread;
        s->write = stream_gzwrite;
        s->close = stream_gzclose;
@@ -806,7 +827,8 @@ open_gzrstream(const char *filename)
        if (s->errnr == MNSTR_NO__ERROR &&
            gzread((gzFile) s->stream_data.p, (void *) &s->byteorder, 
sizeof(s->byteorder)) < (int) sizeof(s->byteorder)) {
                stream_gzclose(s);
-               s->errnr = MNSTR_OPEN_ERROR;
+               destroy(s);
+               return NULL;
        }
        return s;
 }
@@ -823,7 +845,8 @@ open_gzwstream_(const char *filename, co
        if (s->errnr == MNSTR_NO__ERROR &&
            gzwrite((gzFile) s->stream_data.p, (void *) &s->byteorder, 
sizeof(s->byteorder)) < (int) sizeof(s->byteorder)) {
                stream_gzclose(s);
-               s->errnr = MNSTR_OPEN_ERROR;
+               destroy(s);
+               return NULL;
        }
        return s;
 }
@@ -972,8 +995,11 @@ open_bzstream(const char *filename, cons
                free(bzp);
                return NULL;
        }
-       if ((bzp->f = fopen(filename, flags)) == NULL)
-               s->errnr = MNSTR_OPEN_ERROR;
+       if ((bzp->f = fopen(filename, flags)) == NULL) {
+               destroy(s);
+               free(bzp);
+               return NULL;
+       }
        if (strchr(flags, 'r') != NULL) {
                bzp->b = BZ2_bzReadOpen(&err, bzp->f, 0, 0, NULL, 0);
                s->access = ST_READ;
@@ -985,8 +1011,11 @@ open_bzstream(const char *filename, cons
                bzp->b = BZ2_bzWriteOpen(&err, bzp->f, 9, 0, 30);
                s->access = ST_WRITE;
        }
-       if (err != BZ_OK)
-               s->errnr = MNSTR_OPEN_ERROR;
+       if (err != BZ_OK) {
+               stream_bzclose(s);
+               destroy(s);
+               return NULL;
+       }
        s->read = stream_bzread;
        s->write = stream_bzwrite;
        s->close = stream_bzclose;
@@ -1006,7 +1035,8 @@ open_bzrstream(const char *filename)
        if (s->errnr == MNSTR_NO__ERROR &&
            stream_bzread(s, (void *) &s->byteorder, sizeof(s->byteorder), 1) 
!= 1) {
                stream_bzclose(s);
-               s->errnr = MNSTR_OPEN_ERROR;
+               destroy(s);
+               return NULL;
        }
        return s;
 }
@@ -1023,7 +1053,8 @@ open_bzwstream_(const char *filename, co
        if (s->errnr == MNSTR_NO__ERROR &&
            stream_bzwrite(s, (void *) &s->byteorder, sizeof(s->byteorder), 1) 
!= 1) {
                stream_bzclose(s);
-               s->errnr = MNSTR_OPEN_ERROR;
+               destroy(s);
+               return NULL;
        }
        return s;
 }
@@ -1090,6 +1121,8 @@ open_rstream(const char *filename)
        stream *s;
        const char *ext;
 
+       if (filename == NULL)
+               return NULL;
 #ifdef STREAM_DEBUG
        printf("open_rstream %s\n", filename);
 #endif
@@ -1113,11 +1146,12 @@ open_rstream(const char *filename)
                return NULL;
        s->type = ST_BIN;
        if (s->errnr == MNSTR_NO__ERROR) {
-               if (fread((void *) &s->byteorder, sizeof(s->byteorder), 1, 
(FILE *) s->stream_data.p) < 1 ||
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to