diff --git a/addons/format_mp3.c b/addons/format_mp3.c index e0f57b86b4f96a25aad6d393804525b3c0567e82..bb0b20850da47612c8fb16fe0869f6123794a729 100644 --- a/addons/format_mp3.c +++ b/addons/format_mp3.c @@ -118,9 +118,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) diff --git a/apps/app_minivm.c b/apps/app_minivm.c index 4cc2f47965c38c67fc99c6cafda4be556a96ad0f..ff9ab340a639804ce8f1d4f046c8ff7bb46b1513 100644 --- a/apps/app_minivm.c +++ b/apps/app_minivm.c @@ -854,16 +854,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; } diff --git a/apps/app_voicemail.c b/apps/app_voicemail.c index de826704a76fb958d024e77425b6f027cf30bb07..06f4830faec531712a6ce1b416dbdb5eaed60bb1 100644 --- a/apps/app_voicemail.c +++ b/apps/app_voicemail.c @@ -2728,9 +2728,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; } } @@ -4743,12 +4743,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; diff --git a/formats/format_g719.c b/formats/format_g719.c index 8cc942717a84877c973997a31f8c63655affb5cd..572b88f5f7c753a5e542e5cb2069da9192f97a2f 100644 --- a/formats/format_g719.c +++ b/formats/format_g719.c @@ -45,8 +45,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); diff --git a/formats/format_g723.c b/formats/format_g723.c index 04e03b6086f12a34d9d526f6d967011c4bfa538d..d4c4d4b1c9e7c677ee68a7736dc7cedf2ff211bf 100644 --- a/formats/format_g723.c +++ b/formats/format_g723.c @@ -64,8 +64,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; diff --git a/formats/format_g726.c b/formats/format_g726.c index 08e669e264fe62fb830014642b2777567e305854..7da6d1ecbb3ab15822be39b09c6ebaa17124f790 100644 --- a/formats/format_g726.c +++ b/formats/format_g726.c @@ -124,8 +124,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; diff --git a/formats/format_g729.c b/formats/format_g729.c index 49e58025f88db18752c1be1f26289f247b82ba55..4cefc0401c3730ce16216d0e0af8bccf0bf66f5b 100644 --- a/formats/format_g729.c +++ b/formats/format_g729.c @@ -51,8 +51,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; diff --git a/formats/format_gsm.c b/formats/format_gsm.c index a2b6d365616352a3861e833d7b5d351402c1b44a..39deb983e376c87c7703ddfbdcfe1e177d44bcee 100644 --- a/formats/format_gsm.c +++ b/formats/format_gsm.c @@ -55,10 +55,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; @@ -131,7 +139,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)); } } diff --git a/formats/format_h263.c b/formats/format_h263.c index 4cc3db54272bb6652582dd5131dc5eae4bb6abd3..d05e598254a93fb43b45e1f9fa824fdbe14e20bf 100644 --- a/formats/format_h263.c +++ b/formats/format_h263.c @@ -58,7 +58,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; } @@ -74,7 +74,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; @@ -85,8 +85,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 ? */ diff --git a/formats/format_h264.c b/formats/format_h264.c index 60b0902111cb3f7437a51e0e64648a2e226a9403..47f71ae6cc2f333f567649560a159f3ee18ca427 100644 --- a/formats/format_h264.c +++ b/formats/format_h264.c @@ -50,7 +50,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; } @@ -66,7 +66,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; @@ -77,8 +77,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; diff --git a/formats/format_ilbc.c b/formats/format_ilbc.c index eab465d8871f629a8c246a04aa1623b969a85951..ec8ad0ffa6fffcaf9d9c1a6bdee338755e87ab3b 100644 --- a/formats/format_ilbc.c +++ b/formats/format_ilbc.c @@ -49,8 +49,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; diff --git a/formats/format_ogg_vorbis.c b/formats/format_ogg_vorbis.c index c0f8c197dfe76b1160538d3390aa65c77601b961..4fdd1c4117411218b9ac961c4b14b654b1e2d86c 100644 --- a/formats/format_ogg_vorbis.c +++ b/formats/format_ogg_vorbis.c @@ -181,10 +181,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)) @@ -211,10 +211,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)) { diff --git a/formats/format_pcm.c b/formats/format_pcm.c index 97af0e9a3a10c8ad282ac2a4bbcee3ce7684a520..f5ddda9c61aaa0a1866b9a95041afa541e91f618 100644 --- a/formats/format_pcm.c +++ b/formats/format_pcm.c @@ -83,9 +83,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; @@ -140,9 +148,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 */ @@ -210,7 +219,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; } } diff --git a/formats/format_siren14.c b/formats/format_siren14.c index 1ce7d18ad299b0c2934cd79e332599e68dcdd981..d54ed993bc30d928ccb784229960f6a6e56b2eec 100644 --- a/formats/format_siren14.c +++ b/formats/format_siren14.c @@ -45,8 +45,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); diff --git a/formats/format_siren7.c b/formats/format_siren7.c index d2059844571c133f4e8ce88faf032d6b7c8763ae..f3b4b42b3c12e1618879416d23c3d8072229c371 100644 --- a/formats/format_siren7.c +++ b/formats/format_siren7.c @@ -45,8 +45,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); diff --git a/formats/format_sln.c b/formats/format_sln.c index af3f691c81c253f7e0f4ce7028f87c1dfb1f8669..1977f7dc06d46bd8d83ef9863f0095cf7a9c3193 100644 --- a/formats/format_sln.c +++ b/formats/format_sln.c @@ -38,9 +38,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; diff --git a/formats/format_vox.c b/formats/format_vox.c index 5a70c34b140a7c4ab1f39236f007b6fac9043621..195714c6f007092e1f0e577a56dc21b27450427d 100644 --- a/formats/format_vox.c +++ b/formats/format_vox.c @@ -44,9 +44,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; diff --git a/formats/format_wav.c b/formats/format_wav.c index 8316c3530276301e3baf97ff0a40342eb3310907..09e6a5313fdd4fe9abcb122683aba6ad6b5318d0 100644 --- a/formats/format_wav.c +++ b/formats/format_wav.c @@ -361,7 +361,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)); } } @@ -385,14 +385,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; diff --git a/formats/format_wav_gsm.c b/formats/format_wav_gsm.c index eef06cef5b72d5c3bee62866c62f58aff131aa31..bfec903d07760f7f7cb903919d148af7be854334 100644 --- a/formats/format_wav_gsm.c +++ b/formats/format_wav_gsm.c @@ -422,8 +422,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 */ @@ -511,7 +519,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)); } }