mirror of
https://github.com/asterisk/asterisk.git
synced 2025-09-05 12:16:00 +00:00
cleanup: Fix fread() and fwrite() error handling
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
This commit is contained in:
@@ -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;
|
||||
|
Reference in New Issue
Block a user