]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
cleanup: Fix fread() and fwrite() error handling
authorSean Bright <sean.bright@gmail.com>
Fri, 21 Apr 2017 17:04:44 +0000 (13:04 -0400)
committerSean Bright <sean.bright@gmail.com>
Tue, 25 Apr 2017 21:24:37 +0000 (16:24 -0500)
Cleaned up some of the incorrect uses of fread() and fwrite(), mostly in
the format modules. Neither of these functions will ever return a value
less than 0, which we were checking for in some cases.

I've introduced a fair amount of duplication in the format modules, but
I plan to change how format modules work internally in a subsequent
patch set, so this is simply a stop-gap.

Change-Id: I8ca1cd47c20b2c0b72088bd13b9046f6977aa872

21 files changed:
addons/format_mp3.c
apps/app_minivm.c
apps/app_voicemail.c
formats/format_g719.c
formats/format_g723.c
formats/format_g726.c
formats/format_g729.c
formats/format_gsm.c
formats/format_h263.c
formats/format_h264.c
formats/format_ilbc.c
formats/format_ogg_vorbis.c
formats/format_pcm.c
formats/format_siren14.c
formats/format_siren7.c
formats/format_sln.c
formats/format_vox.c
formats/format_wav.c
formats/format_wav_gsm.c
main/http.c
main/manager.c

index e354148f09695841cfba4abf2b8b13eec3eb9cc1..1373f3e92b9f494c79ec3a0ff12a2478a6e9c545 100644 (file)
@@ -120,9 +120,11 @@ static int mp3_squeue(struct ast_filestream *s)
 
        res = ftell(s->f);
        p->sbuflen = fread(p->sbuf, 1, MP3_SCACHE, s->f);
-       if(p->sbuflen < 0) {
-               ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", p->sbuflen, strerror(errno));
-               return -1;
+       if (p->sbuflen < MP3_SCACHE) {
+               if (ferror(s->f)) {
+                       ast_log(LOG_WARNING, "Error while reading MP3 file: %s\n", strerror(errno));
+                       return -1;
+               }
        }
        res = decodeMP3(&p->mp,p->sbuf,p->sbuflen,p->dbuf,MP3_DCACHE,&p->dbuflen);
        if(res != MP3_OK)
index da2956f52635b16610351170a97b5cd862d27c4f..1bfcfbbf4f18b08293295081ce609be3d1480e4e 100644 (file)
@@ -856,16 +856,16 @@ static int b64_inbuf(struct b64_baseio *bio, FILE *fi)
        if (bio->ateof)
                return 0;
 
-       if ((l = fread(bio->iobuf, 1, B64_BASEMAXINLINE,fi)) <= 0) {
-               if (ferror(fi))
-                       return -1;
-
+       if ((l = fread(bio->iobuf, 1, B64_BASEMAXINLINE, fi)) != B64_BASEMAXINLINE) {
                bio->ateof = 1;
-               return 0;
+               if (l == 0) {
+                       /* Assume EOF */
+                       return 0;
+               }
        }
 
-       bio->iolen= l;
-       bio->iocp= 0;
+       bio->iolen = l;
+       bio->iocp = 0;
 
        return 1;
 }
index b2375341a492a36eca0770c71fa7e85913dfb493..f62c7d8c9d64d8dfe56b91cb09477b1006a7e71c 100644 (file)
@@ -2730,9 +2730,9 @@ static int imap_store_file(const char *dir, const char *mailboxuser, const char
                        *(vmu->email) = '\0';
                return -1;
        }
-       if (fread(buf, len, 1, p) < len) {
+       if (fread(buf, 1, len, p) != len) {
                if (ferror(p)) {
-                       ast_log(LOG_ERROR, "Short read while reading in mail file.\n");
+                       ast_log(LOG_ERROR, "Error while reading mail file: %s\n");
                        return -1;
                }
        }
@@ -4745,12 +4745,12 @@ static int inbuf(struct baseio *bio, FILE *fi)
        if (bio->ateof)
                return 0;
 
-       if ((l = fread(bio->iobuf, 1, BASEMAXINLINE, fi)) <= 0) {
-               if (ferror(fi))
-                       return -1;
-
+       if ((l = fread(bio->iobuf, 1, BASEMAXINLINE, fi)) != BASEMAXINLINE) {
                bio->ateof = 1;
-               return 0;
+               if (l == 0) {
+                       /* Assume EOF */
+                       return 0;
+               }
        }
 
        bio->iolen = l;
index f58286290bea543e408441bd24391ad942bd88f8..b93cf0be6d761695f62c19f71e677e61d9565a43 100644 (file)
@@ -47,8 +47,16 @@ static struct ast_frame *g719read(struct ast_filestream *s, int *whennext)
 
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
        if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-               if (res)
-                       ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+               if (feof(s->f)) {
+                       if (res) {
+                               ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+                                               "(expected %d bytes, read %d)\n",
+                                               ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+                       }
+               } else {
+                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+               }
                return NULL;
        }
        *whennext = s->fr.samples = BYTES_TO_SAMPLES(res);
index 926c65b74f9799e79dc924401d76aa4dce2d14de..5eed3f5c822a473fb48a1e4f4f6f33a2b7fcf209 100644 (file)
@@ -66,8 +66,17 @@ static struct ast_frame *g723_read(struct ast_filestream *s, int *whennext)
        }
        /* Read the data into the buffer */
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, size);
-       if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != size) {
-               ast_log(LOG_WARNING, "Short read (%d of %d bytes) (%s)!\n", res, size, strerror(errno));
+       if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
+               if (feof(s->f)) {
+                       if (res) {
+                               ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+                                               "(expected %d bytes, read %d)\n",
+                                               ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+                       }
+               } else {
+                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+               }
                return NULL;
        }
        *whennext = s->fr.samples = 240;
index cadb2d96a8bb267b35d3b1dd826652acf53afc68..0455d6dff19a18a4226293f4b86c696954c65d69 100644 (file)
@@ -126,8 +126,16 @@ static struct ast_frame *g726_read(struct ast_filestream *s, int *whennext)
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, frame_size[fs->rate]);
        s->fr.samples = 8 * FRAME_TIME;
        if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-               if (res)
-                       ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+               if (feof(s->f)) {
+                       if (res) {
+                               ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+                                               "(expected %d bytes, read %d)\n",
+                                               ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+                       }
+               } else {
+                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+               }
                return NULL;
        }
        *whennext = s->fr.samples;
index 4133546267d937d57a31704ee378d04df3791c22..fa122d33bd8ab437d51514b7356df981368687a1 100644 (file)
@@ -53,8 +53,16 @@ static struct ast_frame *g729_read(struct ast_filestream *s, int *whennext)
        s->fr.samples = G729A_SAMPLES;
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
        if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-               if (res && (res != 10)) /* XXX what for ? */
-                       ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+               if (feof(s->f)) {
+                       if (res) {
+                               ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+                                               "(expected %d bytes, read %d)\n",
+                                               ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+                       }
+               } else {
+                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+               }
                return NULL;
        }
        *whennext = s->fr.samples;
index 54c4b8ffa51f9c1b4b5dca2b57281ae31d6eb8e3..07aba9f19aafc71787e6254c5dbf07e86e404c75 100644 (file)
@@ -57,10 +57,18 @@ static struct ast_frame *gsm_read(struct ast_filestream *s, int *whennext)
 {
        int res;
 
-       AST_FRAME_SET_BUFFER(&(s->fr), s->buf, AST_FRIENDLY_OFFSET, GSM_FRAME_SIZE)
+       AST_FRAME_SET_BUFFER(&(s->fr), s->buf, AST_FRIENDLY_OFFSET, GSM_FRAME_SIZE);
        if ((res = fread(s->fr.data.ptr, 1, GSM_FRAME_SIZE, s->f)) != GSM_FRAME_SIZE) {
-               if (res)
-                       ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+               if (feof(s->f)) {
+                       if (res) {
+                               ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+                                               "(expected %d bytes, read %d)\n",
+                                               ast_format_get_name(s->fr.subclass.format), GSM_FRAME_SIZE, res);
+                       }
+               } else {
+                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+               }
                return NULL;
        }
        *whennext = s->fr.samples = GSM_SAMPLES;
@@ -133,7 +141,7 @@ static int gsm_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
                int i;
                fseeko(fs->f, 0, SEEK_END);
                for (i=0; i< (offset - max) / GSM_FRAME_SIZE; i++) {
-                       if (!fwrite(gsm_silence, 1, GSM_FRAME_SIZE, fs->f)) {
+                       if (fwrite(gsm_silence, 1, GSM_FRAME_SIZE, fs->f) != GSM_FRAME_SIZE) {
                                ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
                        }
                }
index 67f126927094f7b1bfaeb504d58939b74457e9d2..5c23923b75206371d3e8cb574f1a1cae2016762c 100644 (file)
@@ -60,7 +60,7 @@ static int h263_open(struct ast_filestream *s)
 {
        unsigned int ts;
 
-       if (fread(&ts, 1, sizeof(ts), s->f) < sizeof(ts)) {
+       if (fread(&ts, 1, sizeof(ts), s->f) != sizeof(ts)) {
                ast_log(LOG_WARNING, "Empty file!\n");
                return -1;
        }
@@ -76,7 +76,7 @@ static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext)
        struct h263_desc *fs = (struct h263_desc *)s->_private;
 
        /* Send a frame from the file to the appropriate channel */
-       if ((res = fread(&len, 1, sizeof(len), s->f)) < 1)
+       if ((res = fread(&len, 1, sizeof(len), s->f)) != sizeof(len))
                return NULL;
        len = ntohs(len);
        mark = (len & FRAME_ENDED) ? 1 : 0;
@@ -87,8 +87,16 @@ static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext)
        }
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len);
        if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-               if (res)
-                       ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+               if (feof(s->f)) {
+                       if (res) {
+                               ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+                                               "(expected %d bytes, read %d)\n",
+                                               ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+                       }
+               } else {
+                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+               }
                return NULL;
        }
        s->fr.samples = fs->lastts;     /* XXX what ? */
index 2ce7a593d8b698a426a0a402abf11d9821549c95..5051fd450970af1f6a29944bcd07cc607f1736d1 100644 (file)
@@ -52,7 +52,7 @@ struct h264_desc {
 static int h264_open(struct ast_filestream *s)
 {
        unsigned int ts;
-       if (fread(&ts, 1, sizeof(ts), s->f) < sizeof(ts)) {
+       if (fread(&ts, 1, sizeof(ts), s->f) != sizeof(ts)) {
                ast_log(LOG_WARNING, "Empty file!\n");
                return -1;
        }
@@ -68,7 +68,7 @@ static struct ast_frame *h264_read(struct ast_filestream *s, int *whennext)
        struct h264_desc *fs = (struct h264_desc *)s->_private;
 
        /* Send a frame from the file to the appropriate channel */
-       if ((res = fread(&len, 1, sizeof(len), s->f)) < 1)
+       if ((res = fread(&len, 1, sizeof(len), s->f)) != sizeof(len))
                return NULL;
        len = ntohs(len);
        mark = (len & FRAME_ENDED) ? 1 : 0;
@@ -79,8 +79,16 @@ static struct ast_frame *h264_read(struct ast_filestream *s, int *whennext)
        }
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len);
        if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-               if (res)
-                       ast_log(LOG_WARNING, "Short read (%d of %d) (%s)!\n", res, len, strerror(errno));
+               if (feof(s->f)) {
+                       if (res) {
+                               ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+                                               "(expected %d bytes, read %d)\n",
+                                               ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+                       }
+               } else {
+                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+               }
                return NULL;
        }
        s->fr.samples = fs->lastts;
index 0e2a92d14f9ac761ce30e967b365ee82598b2ab8..2d67ab244866457c7cd5ed8650f6981d2c3652bb 100644 (file)
@@ -51,8 +51,16 @@ static struct ast_frame *ilbc_read(struct ast_filestream *s, int *whennext)
        /* Send a frame from the file to the appropriate channel */
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, ILBC_BUF_SIZE);
        if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-               if (res)
-                       ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+               if (feof(s->f)) {
+                       if (res) {
+                               ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+                                               "(expected %d bytes, read %d)\n",
+                                               ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+                       }
+               } else {
+                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+               }
                return NULL;
        }
        *whennext = s->fr.samples = ILBC_SAMPLES;
index cf3b1c0e9abb767b98499bd8575b8b7dfa475a5c..a7a9447e7b3bd157809407d1099d2e4d65856bb9 100644 (file)
@@ -183,10 +183,10 @@ static int ogg_vorbis_rewrite(struct ast_filestream *s,
        while (!tmp->eos) {
                if (ogg_stream_flush(&tmp->os, &tmp->og) == 0)
                        break;
-               if (!fwrite(tmp->og.header, 1, tmp->og.header_len, s->f)) {
+               if (fwrite(tmp->og.header, 1, tmp->og.header_len, s->f) != tmp->og.header_len) {
                        ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
                }
-               if (!fwrite(tmp->og.body, 1, tmp->og.body_len, s->f)) {
+               if (fwrite(tmp->og.body, 1, tmp->og.body_len, s->f) != tmp->og.body_len) {
                        ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
                }
                if (ogg_page_eos(&tmp->og))
@@ -213,10 +213,10 @@ static void write_stream(struct ogg_vorbis_desc *s, FILE *f)
                                if (ogg_stream_pageout(&s->os, &s->og) == 0) {
                                        break;
                                }
-                               if (!fwrite(s->og.header, 1, s->og.header_len, f)) {
-                               ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
+                               if (fwrite(s->og.header, 1, s->og.header_len, f) != s->og.header_len) {
+                                       ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
                                }
-                               if (!fwrite(s->og.body, 1, s->og.body_len, f)) {
+                               if (fwrite(s->og.body, 1, s->og.body_len, f) != s->og.body_len) {
                                        ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
                                }
                                if (ogg_page_eos(&s->og)) {
index 05a7eabc51a91d381af3edb568565e72416346e4..cc32ca12ff948e0b71cb94a70f5b89c613a50d72 100644 (file)
@@ -85,9 +85,17 @@ static struct ast_frame *pcm_read(struct ast_filestream *s, int *whennext)
        /* Send a frame from the file to the appropriate channel */
 
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
-       if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) {
-               if (res)
-                       ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+       if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
+               if (feof(s->f)) {
+                       if (res) {
+                               ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+                                               "(expected %d bytes, read %d)\n",
+                                               ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+                       }
+               } else {
+                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+               }
                return NULL;
        }
        s->fr.datalen = res;
@@ -142,9 +150,10 @@ static int pcm_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
                const char *src = (ast_format_cmp(fs->fmt->format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL) ? alaw_silence : ulaw_silence;
 
                while (left) {
-                       size_t written = fwrite(src, 1, (left > BUF_SIZE) ? BUF_SIZE : left, fs->f);
-                       if (written == -1)
+                       size_t written = fwrite(src, 1, MIN(left, BUF_SIZE), fs->f);
+                       if (written < MIN(left, BUF_SIZE)) {
                                break;  /* error */
+                       }
                        left -= written;
                }
                ret = 0; /* successful */
@@ -212,7 +221,10 @@ static int pcm_write(struct ast_filestream *fs, struct ast_frame *f)
                                to_write = fpos - cur;
                                if (to_write > sizeof(buf))
                                        to_write = sizeof(buf);
-                               fwrite(buf, 1, to_write, fs->f);
+                               if (fwrite(buf, 1, to_write, fs->f) != to_write) {
+                                       ast_log(LOG_ERROR, "Failed to write to file: %s\n", strerror(errno));
+                                       return -1;
+                               }
                                cur += to_write;
                        }
                }
index d0a16c4a2ca108a0fa2666c1572b03325d5e61e7..b35ebce148a405e84a2fc8c1bc350c556a0aff89 100644 (file)
@@ -47,8 +47,16 @@ static struct ast_frame *siren14read(struct ast_filestream *s, int *whennext)
 
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
        if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-               if (res)
-                       ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+               if (feof(s->f)) {
+                       if (res) {
+                               ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+                                               "(expected %d bytes, read %d)\n",
+                                               ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+                       }
+               } else {
+                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+               }
                return NULL;
        }
        *whennext = s->fr.samples = BYTES_TO_SAMPLES(res);
index acfde420c407a86724305a4db0965e251aa0a52c..c7856b2a7558a2fd849cdc46ec4fc456e6a7cdc1 100644 (file)
@@ -47,8 +47,16 @@ static struct ast_frame *siren7read(struct ast_filestream *s, int *whennext)
 
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
        if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
-               if (res)
-                       ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+               if (feof(s->f)) {
+                       if (res) {
+                               ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+                                               "(expected %d bytes, read %d)\n",
+                                               ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+                       }
+               } else {
+                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+               }
                return NULL;
        }
        *whennext = s->fr.samples = BYTES_TO_SAMPLES(res);
index 67e5cd5ed2a36af1654c8c946087b865c06a040d..aa47bde565e674f39b09800c81c2492a116335a1 100644 (file)
@@ -40,9 +40,17 @@ static struct ast_frame *generic_read(struct ast_filestream *s, int *whennext, u
        /* Send a frame from the file to the appropriate channel */
 
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, buf_size);
-       if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) {
-               if (res)
-                       ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+       if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
+               if (feof(s->f)) {
+                       if (res) {
+                               ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+                                               "(expected %d bytes, read %d)\n",
+                                               ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+                       }
+               } else {
+                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+               }
                return NULL;
        }
        *whennext = s->fr.samples = res/2;
index 6cef72dfa0e4264ca72d1b3f49c64880aa84f3d7..e4d1e378bdd29f9bb3f07a375fa909dadedabee7 100644 (file)
@@ -46,9 +46,17 @@ static struct ast_frame *vox_read(struct ast_filestream *s, int *whennext)
 
        /* Send a frame from the file to the appropriate channel */
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
-       if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) {
-               if (res)
-                       ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+       if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
+               if (feof(s->f)) {
+                       if (res) {
+                               ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+                                               "(expected %d bytes, read %d)\n",
+                                               ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+                       }
+               } else {
+                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+               }
                return NULL;
        }
        *whennext = s->fr.samples = res * 2;
index 90095eb35bcf27f881daec9db9ca0302ab4f0225..81807c11ad138f9f5ffdbd01fd0e1acf2a3f1f8b 100644 (file)
@@ -363,7 +363,7 @@ static void wav_close(struct ast_filestream *s)
 
        /* Pad to even length */
        if (fs->bytes & 0x1) {
-               if (!fwrite(&zero, 1, 1, s->f)) {
+               if (fwrite(&zero, 1, 1, s->f) != 1) {
                        ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
                }
        }
@@ -387,14 +387,23 @@ static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
        here = ftello(s->f);
        if (fs->maxlen - here < bytes)          /* truncate if necessary */
                bytes = fs->maxlen - here;
-       if (bytes < 0)
-               bytes = 0;
+       if (bytes <= 0) {
+               return NULL;
+       }
 /*     ast_debug(1, "here: %d, maxlen: %d, bytes: %d\n", here, s->maxlen, bytes); */
        AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, bytes);
-       
-       if ( (res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) <= 0 ) {
-               if (res)
-                       ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+
+       if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) != s->fr.datalen) {
+               if (feof(s->f)) {
+                       if (res) {
+                               ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+                                               "(expected %d bytes, read %d)\n",
+                                               ast_format_get_name(s->fr.subclass.format), s->fr.datalen, res);
+                       }
+               } else {
+                       ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+                                       ast_format_get_name(s->fr.subclass.format), strerror(errno));
+               }
                return NULL;
        }
        s->fr.datalen = res;
index 5b3853ef73abe1904f117ffd805bb67a96929c85..f11b27277dc7ca00c02393c61848d7225e5f26e3 100644 (file)
@@ -424,8 +424,16 @@ static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
                int res;
                
                if ((res = fread(msdata, 1, MSGSM_FRAME_SIZE, s->f)) != MSGSM_FRAME_SIZE) {
-                       if (res && (res != 1))
-                               ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
+                       if (feof(s->f)) {
+                               if (res) {
+                                       ast_log(LOG_WARNING, "Incomplete frame data at end of %s file "
+                                                       "(expected %d bytes, read %d)\n",
+                                                       ast_format_get_name(s->fr.subclass.format), MSGSM_FRAME_SIZE, res);
+                               }
+                       } else {
+                               ast_log(LOG_ERROR, "Error while reading %s file: %s\n",
+                                               ast_format_get_name(s->fr.subclass.format), strerror(errno));
+                       }
                        return NULL;
                }
                /* Convert from MS format to two real GSM frames */
@@ -513,7 +521,7 @@ static int wav_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
                int i;
                fseek(fs->f, 0, SEEK_END);
                for (i=0; i< (offset - max) / MSGSM_FRAME_SIZE; i++) {
-                       if (!fwrite(msgsm_silence, 1, MSGSM_FRAME_SIZE, fs->f)) {
+                       if (fwrite(msgsm_silence, 1, MSGSM_FRAME_SIZE, fs->f) != MSGSM_FRAME_SIZE) {
                                ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
                        }
                }
index 80c7b3cb40201db61083d216b66d1841502cd511..cccc60b81ec932299c7c023476fab5d6e324b05c 100644 (file)
@@ -526,6 +526,10 @@ void ast_http_send(struct ast_tcptls_session_instance *ser,
        /* send content */
        if (method != AST_HTTP_HEAD || status_code >= 400) {
                if (out && ast_str_strlen(out)) {
+                       /*
+                        * NOTE: Because ser->f is a non-standard FILE *, fwrite() will probably not
+                        * behave exactly as documented.
+                        */
                        if (fwrite(ast_str_buffer(out), ast_str_strlen(out), 1, ser->f) != 1) {
                                ast_log(LOG_ERROR, "fwrite() failed: %s\n", strerror(errno));
                                close_connection = 1;
@@ -537,6 +541,10 @@ void ast_http_send(struct ast_tcptls_session_instance *ser,
                        int len;
 
                        while ((len = read(fd, buf, sizeof(buf))) > 0) {
+                               /*
+                                * NOTE: Because ser->f is a non-standard FILE *, fwrite() will probably not
+                                * behave exactly as documented.
+                                */
                                if (fwrite(buf, len, 1, ser->f) != 1) {
                                        ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
                                        close_connection = 1;
@@ -923,6 +931,11 @@ static int http_body_read_contents(struct ast_tcptls_session_instance *ser, char
 {
        int res;
 
+       /*
+        * NOTE: Because ser->f is a non-standard FILE *, fread() does not behave as
+        * documented.
+        */
+
        /* Stay in fread until get all the expected data or timeout. */
        res = fread(buf, length, 1, ser->f);
        if (res < 1) {
@@ -950,6 +963,11 @@ static int http_body_discard_contents(struct ast_tcptls_session_instance *ser, i
        int res;
        char buf[MAX_HTTP_LINE_LENGTH];/* Discard buffer */
 
+       /*
+        * NOTE: Because ser->f is a non-standard FILE *, fread() does not behave as
+        * documented.
+        */
+
        /* Stay in fread until get all the expected data or timeout. */
        while (sizeof(buf) < length) {
                res = fread(buf, sizeof(buf), 1, ser->f);
@@ -1066,6 +1084,11 @@ static int http_body_check_chunk_sync(struct ast_tcptls_session_instance *ser)
        int res;
        char chunk_sync[2];
 
+       /*
+        * NOTE: Because ser->f is a non-standard FILE *, fread() does not behave as
+        * documented.
+        */
+
        /* Stay in fread until get the expected CRLF or timeout. */
        res = fread(chunk_sync, sizeof(chunk_sync), 1, ser->f);
        if (res < 1) {
index 0f7adf0c8ef3e2f45a0fe433c88643f9601379ee..0ea6bfae457d1e0fdaaa5682f52aedfb57cc5294 100644 (file)
@@ -6458,6 +6458,12 @@ static int get_input(struct mansession *s, char *output)
        }
 
        ao2_lock(s->session);
+       /*
+        * It is worth noting here that you can all but ignore fread()'s documentation
+        * for the purposes of this call. The FILE * we are working with here was created
+        * as a result of a call to fopencookie() (or equivalent) in tcptls.c, and as such
+        * the behavior of fread() is not as documented. Frankly, I think this is gross.
+        */
        res = fread(src + s->session->inlen, 1, maxlen - s->session->inlen, s->session->f);
        if (res < 1) {
                res = -1;       /* error return */